# getting inf when calculating standard devation in c

asdf Source

Attempting to write a program in c that intakes in an amount of numbers. ie `1, 2, 3, 4`, and returns size, max, min, average and standard deviation.

While trying to calculate standard deviation when calculating `sumx` ( line 51) it begins returng inf. not sure why I would be getting inf with an addition function.

I have all the print methods to try identify where it breaks.

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {

setvbuf(stdout, NULL, _IONBF, 0); // Turn off output buffering. Required for automated testing.     return EXIT_SUCCESS;

int temp;
float sum;
float sumx = 0.0;
int counter = 0;
int max;
int min;
float average;
char buffer[BUFSIZ+1];

printf("Enter scores, one per line.  Press <ENTER> on a blank line to end.\n"); // NOTE: The \n is required for automated testing.

do {

//sets max and min to grade

//lets loop iterate once to establish initial comparable value - breaks
if(counter > 0){
if(max > temp){
}

else{
}
}
//average
average = (float)(sum/counter);
counter++;
//standard deviation
sdPartTwo = sdPartOne * sdPartOne;
//breaks
sumx = sumx + sdPartTwo;
sdPartThree = sumx / counter;
standardD = sqrt(sdPartThree);

}

while(buffer[0] != '\n');

printf("\t%f", sdPartOne);
printf("\t%f", sdPartTwo);
printf("\t%f", sumx);
printf("\t%f", sdPartThree);
printf ("\n%d", counter - 1);
printf("\t%d", max);
printf("\t%d", min);
printf("\t%f", average);
printf("\t%f", standardD);
}
``````
cloopsfloating-pointdo-whilestandard-deviation

answered 6 months ago 4386427 #1

You initialize

`````` int counter = 0;
``````

and in your first loop do

`````` average = (float)(sum/counter);    // In the first loop count is zero, i.e. divide by zero
counter++;
``````

so you divide by zero which leads to `inf`

Just switch the lines like:

`````` counter++;
average = (float)(sum/counter);
``````