The following incomplete program first asks the user to enter the number of items he/she has eaten today and then
to enter the number of calories for each item. It then calculates the number of calories he/she has eaten for the day
and displays the value.
You have to complete the code. First complete the code by using a while loop to read in the calories of all the
items. Compile and run your program and submit only the code that you added and the output. Then change you
program to use a for loop to read in the calories of all the items. Compile and run the program again and submit
only the code that you added and the output. Use the variables that have already been defined in the given program.
Test your program by entering 7 for the number of items and the following values for the calories:
20
7 120 60 150 600 1200 300 200
If your logic is correct, the following will be displayed:
Total calories eaten today = 2631
#include<iostream>
using namespace std;
int main()
{
int numberOfItems;
int count; //loop counter for the loop
int caloriesForItem;
int totalCalories;
cout << "How many items did you eat today? ";
cin >> numberOfItems;
cout << "Enter the number of calories in each of the " << numberOfItems << " items eaten: " << endl; // ------------- Your code --------------
int a[numberOfItems],i,f=0;
for(i=0;i<numberOfItems;i++)
{
cin>>a[i];
f=f+a[i];
}
totalCalories=f;
cout << "Total calories eaten today = " << totalCalories;
return 0;
}
Comments
Leave a comment