Answer to Question #254303 in C++ for Fisiwe Shezi

Question #254303

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


1
Expert's answer
2021-10-23T08:05:54-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS