2

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?

3
  • 5
    What do you think sum contains before you enter any numbers? Commented Dec 9, 2015 at 22:23
  • 1
    Learn to use a debugger Commented Dec 9, 2015 at 22:24
  • 2
    Also remember you are working with integers and they always round down. Commented Dec 9, 2015 at 22:27

1 Answer 1

6

There are couple of issues with your code:

  1. You haven't initialized sum so the output of this program is not deterministic. I guess in your case the compiler simply sets it to 0 which is the intended behavior anyways, so this is why your result is close to the correct one. However, don't expect that this will always be the case and always make sure that you use initialized variables.
  2. In the end of the loop you need to reset the values of numb and sum.
  3. You work with integers which always rounds the result down. You can either use doubles or cast the variables where needed.

    #include <iostream>
    #include <math.h>
    using namespace std;
    int main() {
      int a, b;
      double average, sum;
      int numb;
      cin >> a;
      for (int i = 0; i < a; i++) {
        sum = 0.0;
        numb = 0;
        do {
          cin >> b;
          sum = b+sum;
          numb++;
        } while (b != 0);
        average = sum / (numb - 1);
        cout << round(average) << " ";
      }
    }
    

Also note that if for same case you directly type in 0, the program will crash because of division by 0.

Sign up to request clarification or add additional context in comments.

3 Comments

Better style (IMHO) to initialize the variables at the beginning of the loop, so you don't have to do it twice.
I agree on that. Fixed it.
wow, thanks man! I really do need to get my doubles and ints straight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.