-4
#include <stdio.h>
#include <math.h>
int main( void )
{
    float values[999999];
    int number = 0;
    float max = -100000, min = 100000, total = 0, mean = 0;

    while ((scanf("%f", &values[number++])) != EOF )
    for(int a = 0; a < number; a++) {
        total = total + values[a];
        if (values[a] > max) { max = values[a]; }
        if (values[a] < min) { min = values[a]; }
    }
    mean = total / (float)(number-1);
    printf("%.2f %.2f %.2f\n", min, max, mean);
    printf("%f", total);
    printf("%d", number);
}

and i got result like that:

5.6
6
7
5.60 7.00 11.93
35.800003
Program ended with exit code: 0

I expected the mean to be 6.2, not 11.93.

sokkyoku
  • 2,161
  • 1
  • 20
  • 22
BEI XIN
  • 3
  • 2
  • What is your input? What is your output? What is your expected output? – John3136 Oct 04 '16 at 05:27
  • 1
    `float values[999999];` that's a rather big array to place on stack. maybe you should start with a smaller array – Support Ukraine Oct 04 '16 at 05:30
  • how to fix the big array problem? – BEI XIN Oct 04 '16 at 05:40
  • @BEIXIN - Changing code in a question **after** you have received answers are not the way to do it on SO. Now the answers looks stupid even though they were initially correct. If you want to add a code update, you should leave the original code as it was and then add the updated code below and explain that it is an update. Please revert. – Support Ukraine Oct 04 '16 at 05:58
  • @BEIXIN Either reduce the number of elements or allocate it on the heap using `malloc` or by making it a global variable (by declaring it outside any function). – Klas Lindbäck Oct 04 '16 at 07:24

3 Answers3

2

This for-loop

for( int a=0;a<number;a++){

seems wrong. It makes you recalculate total for each new input number. Try to remove the for-loop. Like:

while ((scanf("%f",&values[number])) != EOF )
{
    total=total+values[number];
    if(values[number]>max) max=values[number];
    if(values[number]<min) min=values[number];
    number++; 
}

Also, this line seems wrong:

mean=total/(float)(number-1);

I think you need

mean=total/number;

but don't do this calculation if number is zero, i.e. do:

mean = (number != 0) ? mean=total/number : 0;

Finally, this line:

float values[999999];

It is a huge array as a local variable. Maybe you'll have stack overflow. Try a smaller array size or use dynamic memory allocation. Notice: For a program like this, you don't even need the array. Just a single float and then do the calculations for each new input.

Finally, you should check that scanf returns 1. If it doesn't values[number] is uninitialized and should not be used. Try:

while ((scanf("%f",&values[number])) == 1 )
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

The while loop needs to either have an empty set of braces, like this:

while ((scanf("%f",&values[number++])) != EOF )
{
}

or it needs a null statement, like this:

while ((scanf("%f",&values[number++])) != EOF )
    ;

Otherwise, the compiler thinks that the for loop is the body of the while loop. Note that the compiler doesn't care about white space, so it ignores the fact that the for loop is not indented.

user3386109
  • 34,287
  • 7
  • 49
  • 68
0
#include <stdio.h>
#include <math.h>
int main( void )
{
    float values;
    int number=0;
    float max=-100000,min=100000,total=0,mean=0;

    while ((scanf("%f",&values)) != EOF ){
        number++;
        total=total+values;
        if(values>max){max=values;}
        if(values<min){min=values;}
        }
    mean=total/(float)number;
    printf("%.2f %.2f %.2f\n",min,max,mean);

}
BEI XIN
  • 3
  • 2