Can't get standard deviation to work in C

425 views Asked by At

I tried everything to get standard deviation to work. I'm trying to make a program that gets grades of students (count decided by user) and it calculates the mean of the grades and also the standard deviation. It calculates the mean fine but for standard deviation it gives "7712160851427328.00"

#include <stdio.h>

int main(){
    int i;
    int j;
    int count;
    int grade = 0;
    int grades[5] = {0}; //Init
    int sum = 0;
    float deviation_sum;
    float mean;
    float standard_deviation;

    printf("Give Count: ");
    scanf("%d", &count);

    for(i = 0; i < count; i++){
        printf("Give %d grade: ", (i+1));
        scanf("%d", &grade);

        switch(grade){
        case 0:
            grades[0]++;
            break;
        case 1:
            grades[1]++;
            break;
        case 2:
            grades[2]++;
            break;
        case 3:
            grades[3]++;
            break;
        case 4:
            grades[4]++;
            break;
        case 5:
            grades[5]++;                                   
        }
        sum += grade;
    }

    mean = sum/count;
    printf("mean: %.2f \n", mean);

    for(i = 0; i <= 5; i++){
        while(grades[i] == 0){
            i++;                
        }
        for(j = 0; j < grades[i]; j++){
            deviation_sum += (i-mean)*(i-mean);
            printf("%d,%d\n",i,j);
        }   
    }

    standard_deviation = sqrt(deviation_sum /count - 1);
    printf("deviation: %.2f\n", standard_deviation);
}

I think the problem is in the last for loop just can't figure it out.

4

There are 4 answers

0
Sumedh On BEST ANSWER

You'll have to initilize deviation_sum to zero. Otherwise it takes garbage value as its initial value

0
0xF1 On

First of all, avoid this type of construct (i could cause array access out-of-bounds):

for(i = 0; i <= 5; i++){
      while(grades[i] == 0){
         i++;                
      }

Use if instead of while and use continue to loop over again.

For correcting the error you are getting, do following changes in your code (I am just giving you hints)
1. Use typecasting where necessary
2. Monitor variables (especially array indices) for proper bounds
3. When using functions like sqrt, do check for positivity of the argument otherwise you may face Domain ERROR.
4. Always remember to initialize variables when required.

I would also suggest a task for you:

Try removing your switch with a simpler logic.

0
Paul R On

Your mean calculation will fail as you are doing integer division, and this will then make the subsequent std dev calculation incorrect.

Change:

mean = sum/count;

to

mean = (float)sum/count;

so that the division is performed using floating point arithmetic. You might also want to print the value of mean at this point to check that it looks reasonable.

0
alk On

Also this

standard_deviation = sqrt(deviation_sum /count - 1);

shall be

standard_deviation = sqrt(deviation_sum / (count - 1));

Please see here for the background.