-1

Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the max and average. A negative integer ends the input and is not included in the statistics.

Ex: When the input is:

15 20 0 5 -1

the output is:

20 10

You can assume that at least one non-negative integer is input.

#include <iostream>
using namespace std;

int main() {
   int count = 0;
   int max = 0;
   int total = 0;
   int num;
   int avg;
   
   cin >> num;
   
   while (num >= 0) {
      count++;
      total += num;
      
      if (num > max){
         max = num;
      }
      cin >> num;
   }
   
   avg = total / count;
   cout << avg << " " << max;
   
   return 0;
}

I have been working on this problem for a while and I am currently stuck why there isn't any output. I feel like I didn't write the while loop correctly but can't seem to figure out how to fix it. Any help is appreciated.

  • `avg + " " + max` is not what you think it is. Try putting in a statement like `auto x=avg + " " + max;` in front of the cout and looking at what x is and what x is autodeclared as. – doug Sep 23 '21 at 03:50
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Sep 23 '21 at 03:50
  • @doug Sorry I meant to put `avg << " " << max` in the code above –  Sep 23 '21 at 04:02
  • @AndreasWenzel I did try running a debugger and it would just stop after reading the input as mentioned in one of the replies. That is what I am trying to fix atm –  Sep 23 '21 at 04:07
  • With your fix on the `cout` line, your code works as expected when I tried it. – Adrian Mole Sep 30 '21 at 09:40

3 Answers3

0

After start, program immediately stops waiting for user input. In order to see an output user has to enter at least one positive integer, press Enter key, enter negative integer and press Enter key once again

0

I rewrote your code, try with this:

int i=0, max, total = 0, avg, num, temp;
cout<<"num of elements"<<endl;
cin>>num;

while(i<num){
    cin>>temp;
    fflush(stdin); //This will solve your problem
    
    if(i==0){
        max = temp;
    }
    
    if(temp>max){
        max = temp;
    }
    total = total + temp;
    i++;
}
avg = total/num;
cout<<"avg"<<avg<<" "<<"max"<<max<<endl;

return 0;

I fixed the issue using fflush(stdin), this function cleans the buffer. It's recommended used it before or after of the data input, just to make shure that the buffer is clean.

Input:

3 -1 -2 -10

Output:

avg-4 max-1

This because -1 -2 -10 = -13 and -13/3 = 4.3. But you are using int so, it's going to be rounded, the result is 4

Also it works with positive values at beginning and the last one negative.

Input:

3 5 10 -1

Output:

avg4 max10
Dharman
  • 30,962
  • 25
  • 85
  • 135
SergioPD08
  • 43
  • 5
  • Note that OP has clearly stated that a negative number ends the input and is **not** to be included in the data set. Also, **never** use `fflush(stdin);` - it's implementation-defined (at best) and most likely undefined behaviour. [How do I flush the cin buffer?](https://stackoverflow.com/q/257091/10871073) – Adrian Mole Sep 30 '21 at 09:39
0

Here is what one of my CS friends sent me that worked.

#include <iostream>
using namespace std;

int main() {
   int temp, sum = 0, count = 0;
   cin >> temp;
   int max = temp;
   sum = temp;
   count++;
   
   while(temp >= 0){
      cin >> temp;
      
      if (temp > max){
         max = temp;
      }
      if (temp >= 0){
         sum += temp;
         count++;
      }
   }
   cout << max << " " << (double)sum / count << endl;
   return 0;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135