-2

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.

Puck
  • 2,080
  • 4
  • 19
  • 30
HyperParameter
  • 274
  • 3
  • 16
  • 5
    First tip: you can replace the entire `switch` statement with `grades[grade]++`. Second tip: Don't use two variables named `grades` and `grade` unless you want to make yourself cry. – Carl Norum Aug 28 '13 at 16:54
  • What will be output of your code, after correcting all the logical errors pointed out in different answers, if I give input `0` to `count`? – 0xF1 Aug 28 '13 at 17:12
  • 1
    Even entering `1` might be interesting. – alk Aug 28 '13 at 17:15
  • 1
    Wow, 4 answers each pointing out 4 different issues that are needed. – chux - Reinstate Monica Aug 28 '13 at 20:37

4 Answers4

4

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

Sumedh
  • 404
  • 3
  • 11
3

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
2

Also this

standard_deviation = sqrt(deviation_sum /count - 1);

shall be

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

Please see here for the background.

alk
  • 69,737
  • 10
  • 105
  • 255
1

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.

0xF1
  • 6,046
  • 2
  • 27
  • 50