Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to add the sum of five numbers using a for loop... all goes well as

ID: 3565912 • Letter: I

Question

I am trying to add the sum of five numbers using a for loop... all goes well as long as I'm working with positive integers, but for whatever reason, it will not display negative values. Please review my code and write notes so I can understand whats going on.

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{   
float avg = 0;   
float x;
  
   for (int i = 1; i <= 5; i++)
   {
cout << "Enter a number: "<< endl;
cin >> x;   
avg = avg + i;
   }   
  
   cout << endl << avg/5 << endl;
  
return 0;
}

Explanation / Answer

avg= avg+x instead of avg = avg+i;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote