#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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.