0
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 6

int main(int argc, char** argv) {

    float stddeviation, deviation, sumsqr, variance, mean, x, t, m, sum=0, max, min;
    int numdatapts, k;
    FILE * inFile;

    inFile = fopen("Uniform93.data", "r");

    if(inFile == NULL){
        printf("\nError opening file. Abort program.\n");
        exit(1); }

    for (k = 1; k <= MAXSIZE; k++){
        fscanf(inFile, "%f", &m);
        if (k == 1)
            max = min = m;
        sum += m;
        if (m > max)
            max = m;
        if (m < min)
            min = m;
        if (k == 1)
            mean = sum / MAXSIZE;
        deviation = m - mean;
        sumsqr += deviation * deviation;
        variance = sumsqr / (MAXSIZE - 1);
        stddeviation = sqrt(variance);




    }

    printf("Mean:  %4.4f \n", mean);
    printf("Maximum:  %4.4f \n", max);
    printf("Minimum:  %4.4f \n", min);
    printf("Standard Deviation:  %4.4f \n", stddeviation);





    return (EXIT_SUCCESS);

Hi I need help with this code that imports a file with a list of numbers and then outputs the maximum, minimum, average, and standard deviation. So far everything works except for the standard deviation part. Here's the code

2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by Gonbe
1

An easy approach would be to determine the mean in the first pass through the file, then go through the file again to determine the standard deviation by squaring the differences of every number with the mean and then take the square root of the average of those values.

There are also algorithms which calculate the standard deviation in a single pass ("on the fly") but those are more complicated and shouldn't be that difficult to find by a simple google search.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.