Average of any 150 numbers as input with loop ?

Hello guys ,
i have a homework (which i will not ask for solution) just asking for 1 problem hint.

The homework is to get the Max , Min , Sum and Avg for any 150 numbers you input
i figure all out except for the Avg formula.

like if you input 40 as first input
u get :
minimum value:40
maximum value:40
Sum: 40
Average: 40

second input: 100
min:40
max:100
sum:140
average:70

and so on...i just cant figure out how to do the average for it :S
any help with be appreciated !
you just need to divide the sum by the number of inputs
how can i know the number of inputs at the time
like in the second input how can i know the number of inputs is 2 by that time and the third input is 3 xD thats a problem for me xD sorry but im new to C++
Perhaps try incrementing a variable when you get each input.
Use a counter variable int counter = 0;. Each time the input loop completes, increment counter by 1: counter++;. Each time you output the average you can simply cout << "average:" << sum/count;
thanks that helped alot =) reallly !
now im facing somthin i didnt expect !!
i cant do the minimum value all other operations are working well only minimum value
i cant figure it out too

i figured out the maximum value
int max=0;
and i use if(x>max)
max=x
cout<<"maximum value:"<<max<<endl;

but how can i do the minimum?
If you are trying to find the minimum value of input, try something like:

1
2
3
int min = 0;
if (x < min)
   min = x;


Assuming x is the input.
Last edited on
what if i didnt even input anythin less than 0

then the minimum value is gona still be zero while it might be 20 or 10 do u get me thats the problem that im facing here
Instead of initializing at 0, initialize them to the first input value.
My apologies - Zhuge's method will solve this problem.
Last edited on
Yes, exactly. You can use your counter variable to know whether or not to initialize min with the input or test it against the input.

For example, where input_value is the value you got from the user:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (counter == 0)
  {
  min = input_value;
  max = input_value;
  ...
  }
else
  {
  if (input_value < min)
    min = input_value;
  ...
  }

counter++;

Hope this helps.
Topic archived. No new replies allowed.