I have encountered a problem where I must take the average value of the sum of a line of numbers, ending with a zero, which must not be counted in the equation. However, while my first value of output is quite near or sometimes correct, my other values are completely off. I'd be glad if anyone can figure out what I did wrong with my code.
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int a, b, sum, average;
int numb = 0;
cin>>a;
for (int i=0;i<a;i++) {
do {
cin>>b;
sum= b+sum;
numb++;
} while (b!=0);
average = sum/(numb-1);
cout<<round(average)<<" ";
}
}
Input:
11
780 1610 565 799 1664 431 0
12848 10728 4091 12286 8035 0
959 418 171 255 694 78 393 917 119 1016 929 761 363 0
14930 11543 11508 3062 1545 8434 6504 2631 0
418 359 477 157 224 170 124 433 255 0
1175 1789 853 1411 1772 661 884 449 1324 713 0
52 325 456 579 732 621 0
6898 11736 13531 11906 2502 0
16334 10736 7506 8493 3749 5434 0
3221 4212 5720 6807 14802 11421 8939 4167 12245 14132 11460 5120 9445 5000 5379 0
366 2435 3709 1616 3725 3449 1591 901 7202 5837 0
Output:
974 4486 2342 3459 2748 2405 2182 2667 3109 3895 3778
Expected:
975 9598 544 7520 291 1103 461 9315 8709 8138 3083
But what went wrong?
sumcontains before you enter any numbers?