Assignment #1 Introduction to C Programming – COP 3223 Objectives 1. To give stu
ID: 3876300 • Letter: A
Question
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.)
Sample Run #1
How much does your dragon weigh?
50
You will need 6 sheep for your new dragon!
Sample Run #2
How much does your dragon weigh?
22
You will need 3 sheep for your new dragon!
Deliverables
One source file – dragonfeeding.c – is to be submitted over WebCourses.
Restrictions
Although you may use other compilers, your program must compile and run using Code::Blocks. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.
Grading Details
Your programs will be graded upon the following criteria:
1) Your correctness
2) Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 10% or 15% deducted from your grade.
3) Compatibility – You must submit C source files that can be compiled and executed in a standard C Development Environment. If your program does not compile, you will get a sizable deduction from your grade.
Please show coding. thanks
Explanation / Answer
/*******************************************************
NAME:
COURSE NUMBER:
SECTION NUMBER:
ASSIGNMENT TITLE:
DATE:
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PROTEIN_PER_SHEEP 200
#define NUM_OF_DAYS 30
/* Values less than 0 and greater than 4294967295 will wrap
around because of unsigned int's maximum size which is 4294967295*/
unsigned int read_dragon_weight()
{
unsigned int weight_of_dragon;
printf("How much does your dragon weigh? ");
scanf("%lu", &weight_of_dragon);
return weight_of_dragon;
}
unsigned int calculate_number_of_sheep(unsigned int weight_of_dragon)
{
unsigned int number_of_sheep;
double daily_protein_req = 0.0;
double monthly_protein_req = 0.0;
/* Total protein required per day */
daily_protein_req = weight_of_dragon / 2.2 * 1.5;
/* Total protein required per month */
monthly_protein_req = daily_protein_req * NUM_OF_DAYS;
/* Number of sheep required to fulfill monthly protein quota
ceil() math function will return a whole number for sheep.
For example if number of sheep required is 2.2 it will return
3.0 since that is what we need as answer*/
number_of_sheep = ceil(monthly_protein_req / PROTEIN_PER_SHEEP);
return number_of_sheep;
}
int main()
{
unsigned int weight_of_dragon;
unsigned int number_of_sheep;
/* User Input */
weight_of_dragon = read_dragon_weight();
/* Processing for number of sheep */
number_of_sheep = calculate_number_of_sheep(weight_of_dragon);
/* Output */
printf("You will need %lu sheep for your dragon!", number_of_sheep);
return 0;
}
OUTPUT
How much does your dragon weigh?
50
You will need 6 sheep for your dragon!
How much does your dragon weigh?
22
You will need 3 sheep for your dragon!
How much does your dragon weigh?
4294967295
You will need 439258019 sheep for your dragon!
How much does your dragon weigh?
100
You will need 11 sheep for your dragon!
How much does your dragon weigh?
0
You will need 0 sheep for your dragon!