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;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.