COMSC 1613 – Programming I
(McCann)
Program #0: Calories Due to Fat
Due Date: Smarch 36th, 2028, at the beginning of class
System Requirement:
You must do this assignment on the AIX1 system.
Overview: Any dietician will tell you that limiting your daily fat intake to be under 30% of your total calories is a good idea. But how many of the calories in your favorite foods are from fat? Nutrition labels will tell you how many grams of fat are in a serving, and how many total calories are in that serving, but you need to do the rest of the figuring on your own.
As it happens, a gram of fat has about 9 calories. Therefore, if you take the grams of fat in a serving of a particular food, multiply it by 9 and divide by the total calories in the serving, you'll get the fraction of the calories due to fat. To get the result as a percentage, just multiply by 100.
For example, consider a product that has 3 grams of fat and 170 calories per serving. There are 27 calories due to fat (3 * 9), and 27 / 170 = 0.1588. Multiplying by 100 gives the final answer: 15.88 percent of the calories are due to fat.
Assignment: Write a complete, well-documented C program that begins by asking the user to enter the grams of fat and the calories in a serving of a food product. The program will compute the percentage of the food's calories that are due to fat, and will display the input information and the percentage in the form of a complete English sentence.
Data: Run your program twice, once for each set of data shown in the following table:
Fat (grams) |
Total Calories |
|
1 |
1 |
66 |
2 |
21 |
480 |
Hand In: On the due date, turn in the following items: A printout of your documented C program, and a printout of the output your program produced when run on each of the sets of data given above. Be sure to write your name in the upper right hand corner of the printout; this will make it easier for you to reclaim your program when I return it to you.
/*============================================================================= | Assignment: ASSIGNMENT NUMBER AND TITLE | | Author: STUDENT'S NAME HERE | Language: NAME OF LANGUAGE IN WHICH THE PROGRAM IS WRITTEN AND THE | NAME OF THE COMPILER USED TO COMPILE IT WHEN IT | WAS TESTED | To Compile: EXPLAIN HOW TO COMPILE THIS PROGRAM | | Class: NAME AND TITLE OF THE CLASS FOR WHICH THIS PROGRAM WAS | WRITTEN | Instructor: NAME OF YOUR COURSE'S INSTRUCTOR | Due Date: DATE AND TIME THAT THIS PROGRAM IS/WAS DUE TO BE SUBMITTED | +----------------------------------------------------------------------------- | | Description: DESCRIBE THE PROBLEM THAT THIS PROGRAM WAS WRITTEN TO | SOLVE. | | Input: DESCRIBE THE INPUT THAT THE PROGRAM REQUIRES. | | Output: DESCRIBE THE OUTPUT THAT THE PROGRAM PRODUCES. | | Algorithm: OUTLINE THE APPROACH USED BY THE PROGRAM TO SOLVE THE | PROBLEM. | | Required Features Not Included: DESCRIBE HERE ANY REQUIREMENTS OF | THE ASSIGNMENT THAT THE PROGRAM DOES NOT ATTEMPT TO SOLVE. | | Known Bugs: IF THE PROGRAM DOES NOT FUNCTION CORRECTLY IN SOME | SITUATIONS, DESCRIBE THE SITUATIONS AND PROBLEMS HERE. | *===========================================================================*/Note that I've included some brief comments with each section, to let you know what you need to add. Because deleting all of those comments is a pain, I've got a comment-free version on AIX1 called ext.c that you can import instead. It's in the same directory as external.c.
/*============================================================================= | Assignment: Program #0 -- Calories Due to Fat | | Author: Reg LeCrisp | Language: AIX C (the xlc compiler) | To Compile: At the AIX prompt, type: xlc prog0.c | | Class: COMSC 1613 (Programming I) | Instructor: Dr. McCann | Due Date: Smarch 36th, 2028 | +----------------------------------------------------------------------------- | | Description: This program determines what percentage of the calories | in a food product are due to the food's fat content. | | The user is asked to input the number of grams of fat in one | serving of the food, and the total calories in one serving. | A gram of fat contains 9 calories, so the percentage of | the food's calories that are due to fat is found by using | this formula: | | # of grams of fat * 9 | percentage = ----------------------------- * 100 | calories in a serving | | Input: The user is required to enter the grams of fat in one | serving of the food in question, and the total number of | calories in that one serving. It is expected that the | user will type these two pieces of information at his or | her keyboard. | | Output: The program will display an explanation of its purpose, | will prompt the user for the input, and will display the | percentage of the food's calories that are due to fat. All | output is displayed on the user's screen. | | Algorithm: The program's steps are as follows: | | 1. The program displays its purpose | 2. The user is prompted to enter the grams of fat | and calories per serving | 3. The percentage of calories due to fat is | computed | 4. The percentage is displayed to the user, along | with the given input information. | | Required Features Not Included: | | Known Bugs: | *===========================================================================*/Please pay attention to the level of detail; the explanations are all quite detailed. Many students try to include a bare minimum of information in their documentation; that's not good at all. The documentation should help the reader understand the program, not raise more questions than it answers. This example shows an adequate amount of information. (Can you think of some additional information that you'd like to see included?) Some students think that the reader can just refer to the assignment handout to get the information. Remember two things: First, the documentation is part of the program code; the handout isn't. Second, when you get a job as a programmer, your boss is not going to go around giving you assignment handouts. You might as well get in the habit of writing good, informative documentation now.
#include <stdio.h> int main (void) { int grams_of_fat, /* number of grams of fat in one serving */ total_calories; /* number of calories in one serving */ float percent; /* percentage of total calories from fat */ scanf("%d",&grams_of_fat); scanf("%d",&total_calories); printf("%d %d %f\n",grams_of_fat, total_calories, percent); return 0; }Yes, this is very spartan. Don't worry, we'll fill in the details in due time. The point is, we can compile and run this program as-is, and be convinced that we are able to successfully read and write the data. It doesn't make much sense to write the percentage calculations until we know that the calculations will have the correct values to work with. By including a simple output statement, we can see that the values are being read correctly.
$ xlc prog0.c $ a.out 3 76 3 76 0.000000Obviously, we'll need to make these input and output actions a lot more "user friendly" before we finish.
#include <stdio.h> #define CALORIES_PER_GRAM 9.0 /* there are 9 calories per gram of fat */ int main (void) { int grams_of_fat, /* number of grams of fat in one serving */ total_calories; /* number of calories in one serving */ float fat_fraction, /* fraction of calories due to fat */ percent; /* percentage of total calories from fat */ scanf("%d",&grams_of_fat); scanf("%d",&total_calories); fat_fraction = (grams_of_fat * CALORIES_PER_GRAM) / total_calories; percent = fat_fraction * 100; printf("%d %d %f\n",grams_of_fat, total_calories, percent); return 0; }Don't forget to test the calculations to ensure that they are working correctly. Remember the example calculation given in the assignment handout? It makes for a good test case:
$ xlc prog0.c $ a.out 3 170 3 170 15.882354Eventually, you'll have added all of the functionality that is required of your program, will have tested its operation thoroughly, and will have added any remaining documentation. At this point, you may choose to add some "extras" that enhance the program but were not required by the assignment. (I never have problems when you do more than the assignment requires!) Here's what the completed program might look like:
/*============================================================================= | Assignment: Program #0 -- Calories Due to Fat | | Author: Reg LeCrisp | Language: AIX C (the xlc compiler) | To Compile: At the AIX prompt, type: xlc prog0.c | | Class: COMSC 1613 (Programming I) | Instructor: Dr. McCann | Due Date: Smarch 36th, 2028 | +----------------------------------------------------------------------------- | | Description: This program determines what percentage of the calories | in a food product are due to the food's fat content. | | The user is asked to input the number of grams of fat in one | serving of the food, and the total calories in one serving. | A gram of fat contains 9 calories, so the percentage of | the food's calories that are due to fat is found by using | this formula: | | # of grams of fat * 9 | percentage = ----------------------------- * 100 | calories in a serving | | Input: The user is required to enter the grams of fat in one | serving of the food in question, and the total number of | calories in that one serving. It is expected that the | user will type these two pieces of information at his or | her keyboard. | | Output: The program will display an explanation of its purpose, | will prompt the user for the input, and will display the | percentage of the food's calories that are due to fat. All | output is displayed on the user's screen. | | Algorithm: The program's steps are as follows: | | 1. The program displays its purpose | 2. The user is prompted to enter the grams of fat | and calories per serving | 3. The percentage of calories due to fat is | computed | 4. The percentage is displayed to the user, along | with the given input information. | | Required Features Not Included: All required features are included. | | Known Bugs: None; the program operates correctly. | *===========================================================================*/ #include <stdio.h> #define CALORIES_PER_GRAM 9.0 /* there are 9 calories per gram of fat */ int main (void) { int grams_of_fat, /* number of grams of fat in one serving */ total_calories; /* number of calories in one serving */ float fat_fraction, /* fraction of calories due to fat */ percent; /* percentage of total calories from fat */ printf("This program will tell you how much of the calories in\n"); printf("a food are from the food's fat content.\n\n"); printf("How many grams of fat are in one serving? "); scanf("%d",&grams_of_fat); printf("How many total calories are in one serving? "); scanf("%d",&total_calories); fat_fraction = (grams_of_fat * CALORIES_PER_GRAM) / total_calories; percent = fat_fraction * 100; if (grams_of_fat == 1) { printf("\nA food with 1 gram of fat "); } else { printf("\nA food with %d grams of fat ",grams_of_fat); } if (total_calories == 1) { printf("and 1 calorie per serving\n"); } else { printf("and %d calories per serving\n",total_calories); } printf("has %.2f%% of those calories from fat.\n\n",percent); return 0; }In this program, I decided that it would be nice if the output wording matched the values the user entered. The IF-ELSE statements will make the words match the quantity; for example, "1 gram" instead of "1 grams". This step wasn't required, but it does make for a more polished final program.
$ xlc prog0.c $ a.out This program will tell you how much of the calories in a food are from the food's fat content. How many grams of fat are in one serving? 3 How many total calories are in one serving? 170 A food with 3 grams of fat and 170 calories per serving has 15.88% of those calories from fat.
$ script Script command is started. The file is typescript. $ xlc prog0.c $ a.out This program will tell you how much of the calories in a food are from the food's fat content. How many grams of fat are in one serving? 1 How many total calories are in one serving? 66 A food with 1 gram of fat and 66 calories per serving has 13.64% of those calories from fat. $ a.out This program will tell you how much of the calories in a food are from the food's fat content. How many grams of fat are in one serving? 21 How many total calories are in one serving? 480 A food with 21 grams of fat and 480 calories per serving has 39.38% of those calories from fat. $ exit exit Script command is complete. The file is typescript.Clearly, the command to stop script is the exit command. The text that appeared on the screen in-between those two commands is saved in your account in a file named typescript. As you saw, the program was run twice, once for each set of data provided in the assignment handout.
$ lpr prog0.c typescriptBy listing the two files together, as shown, UNIX will print them consecutively on the printer (that is, no one else's printout will be between them).