Empty/wrong out put in program

I am writing a program that calculates the average of sums. First input in amount of lines in the program, each line will include numbers which I will have to find the total average of the total sum, and "0" is at the end to mark the end, which I will not include in the calculation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #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++) {
        cin>>b;
       do {
        sum= b+sum;
        numb++;
       }
        while (b!=0);
        average = sum/numb;
         cout<<round(average)<<" ";}
}


I keep on getting an empty output (simply NO output!). Why is this so? How do I fix it?

(Below is an alternative code that didn't have empty outputs, BUT had wrong outputs which I couldn't figure out)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main() {
int a, b, c, average;
int numb = 0;
cin>>a;
for (int i=0;i<a;i++) {
    int sum = 0;
        cin>>b;
        c=b;
        if (c!=0) {
        sum= b+sum;
        numb++;}
        average = sum/numb;
         cout<<average<<" ";}
}
Last edited on
closed account (E0p9LyTq)
Your do...while loop runs indefinitely. Run indefinitely unless the very first entry of b is 0.
Last edited on
An edit to this program proved that the first average would be correct or very close, but everything else would be wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #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 [very close! But what went wrong?] 9598 544 7520 291 1103 461 9315 8709 8138 3083
Last edited on
Topic archived. No new replies allowed.