+ 2

Why the sum(result) stop summing when I add a variable(b) to do an average. -_-

#include <iostream> using namespace std; int main() { cout << "AVERADGE OF NUMBERS: " << endl; double result(0); int b(1); int a(0); for(int x=1; x<100; x++) { cin >> a; result=(result+a)/b; cout << result << endl << endl; b++; } }

1st Feb 2017, 8:13 PM
solosubmit
3 Réponses
+ 7
It is still summing but you are using result everytime in the for loop. Example: if result = 100, a = 100 and B = 3 the new result will become 100+100=200 200/3 is something like 66,..... The second time this value will be used in the calculation ex: 67 + 100 = 167 167/4 (new B value) = 43,...
1st Feb 2017, 8:23 PM
Jeroen
Jeroen - avatar
0
waw thx m8... am i fully retarded? as they say answers bring more questions ;;_;;
1st Feb 2017, 11:44 PM
solosubmit
0
#include <iostream> using namespace std; int main() { cout << "averadge: "<< endl << endl; double total=0, averadge=0, entry=0; for (int x=1; x>0; x++) { cout << "entry" << x <<": "; cin >> entry; total+=entry; cout << "total: " << total << endl; averadge=total/x; cout << "averadge: " << averadge << endl << endl; } }
2nd Feb 2017, 12:32 AM
solosubmit