Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
Assignment #1
Introduction to C Programming – COP 3223

Objectives
1. To give students practice at typing in, compiling and running simple programs.
2. To learn how to read in input from the user.
3. To learn how to use assignment statements and arithmetic expressions to make calculations

Introduction: Who doesn’t love dragons?
Movies about dragons and dragon training were very popular this summer. Your friend has not stopped talking about how awesome dragons are and how cool it would be to train them. To amuse your friend, you have decided to create a series of programs about dragons.

Problem: Dragon Feeding (dragonfeeding.c)
Everyone knows that dragons hatch from eggs and need lots of meat to grow into full sized riding dragons. In this program, we will calculate how many sheep we will need for our new dragon for this month.

You will need to ask the user for the weight of the dragon in pounds. You can then determine the number of grams of protein he or she will need each day using the following formula:

Weight in pounds / 2.2 * 1.5

If each sheep on Dragon Island has 200g of protein, determine how many sheep will be necessary for the month (30 days) and print this information to the user.

Input Specification
1. The weight will be a positive integer.

Output Specification
Output the number of sheep as a whole number using the format below:

You will need X sheep for your new dragon!

Output Sample
Below are some sample outputs of running the program. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above. In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: When you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity’s sake.)

What I have tried:

C++
/* Ashley Prieto
 * This program prints the number of sheep you will need for a new dragon
 */

 #include <stdio.h>

 int main() {
    //variable declaration
    int pounds;

    //prompt user for input information
    printf("How much does your dragon weigh?\n");
    scanf("%d", £s);

    //calculations
    Weight in pounds = pounds / 2.2 * 1.5;


    //output results
    printf("Your dragon weighs %d pounds! \n", pounds);

    return 0;
 }
Posted
Updated 19-Jan-18 17:04pm
v3
Comments
Patrice T 19-Jan-18 21:59pm    
And the question is ?
Jim Meadors 19-Jan-18 23:26pm    
I think we would be taking your class if we wanted to do your homework...

1 solution

The first problem is in your calculations, you never set the result to a variable. To fix that, you would need to add another variable and set the value of it to the results of the calculation like so:
#include <stdio.h>

int main() {
   //variable declaration
   int weight, sheep;

   //prompt user for input information
   printf("How much does your dragon weigh?\n");
   scanf("%d", &weight);

   //calculations
   sheep = pounds / 2.2 * 1.5;


   //output results
   printf("Your dragon weighs %d pounds! \n", pounds);

   return 0;
}

Next thing is that in the assignment prompt, it states that the calculation is per day while the results must be per month (30 days). So your calculation should actually be
sheep = (weight/ 2.2 * 1.5 * 30) / 200;

The 200 is the grams of protein each sheep provides. You outputted the results for how much the dragon weighs when the prompt asks for how many sheep the dragon needs per month. To fix that, you would need to change your output to
printf("You will need %d sheep for your new dragon!\n", sheep);
After all that is fixed, it should look like this:
#include <stdio.h>

int main() {
   //variable declaration
   int weight, sheep;

   //prompt user for input information
   printf("How much does your dragon weigh?\n");
   scanf("%d", &weight);

   //calculations
   sheep = weight / 2.2 * 1.5 * 30 / 200;


   //output results
   printf("Your dragon weighs %d pounds!\n", weight);

   return 0;
}

Hope this helped.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900