Hey guys,

I'm having a little trouble putting this together. I believe I have the formula correct on the bottom, but I can't seem get the program to work. The idea is that it needs to be able to run like this:

./sdev < input.file

I have a simple understanding of how to up it together in concept, but not entirely sure on how to actually put it together in C.

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

void statistics (const int *arr, int n, float *average, float *std_dev)

int main ()
{
    while(fscanf(input, "%d", &i) !=EOF); 
    {
        n++;
    }
    int sum = 0;
    int sum_squares = 0;
    float variance;
    float average;
    float std_dev;
    int i;
    
    
    
    for (i = 0; i < n; i++)
    {
        sum += aar[i];
        sum_squares += arr[i]*arr[i];
    }
    
    *average = sum / n;
    
    variance = sum_squares / n - (*average) * (*average);
    
    *std_dev = sqrt (variance);
    
    printf("The average is %d\n" , *average);
    printf("The standard deviation is %d\n" , *std_dev); 
    
    return(0);
}

I've found a few example of standard deviations in C online, but many of those are done with a fixed number of array. I was hoping someone can walk me through his.

The only problem you have is reading the file. The algorithm for calculating standard deviation is sound. Take a look at this instead:

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

int main(void)
{
    FILE *in = fopen("test.txt", "r");
    
    if (in != NULL) {
        int sum = 0, sum_squares = 0, n = 0;
        int val;
        
        while (fscanf(in, "%d", &val) == 1) {
            sum += val;
            sum_squares += val * val;
            ++n;
        }

        fclose(in);
        
        if (n > 0) {
            double mean = (double)sum / n;
            double variance = (double)sum_squares / n - (mean * mean);
            double std_dev = sqrt(variance);
            
            printf("Mean: %f\nStdDev: %f", mean, std_dev);
        }
    }
    
    return 0;
}

Just a little bit curious.

The file I am having the program read is a list of numbers like:

1
2
5
6
7
8

and so fourth.

if (in != NULL) 
{
       int sum = 0, sum_squares = 0, n = 0;
       int val;

Something very similar to this (sorry on a mobile right now, or I would've posted what I have). I'm a little confused as to how to set up an array or to compile the data into an array.

I've included the commands to read the file, but the results I'm generating are "average is 0 and standard deviation is 0". Which leads me to believe my program isn't reading the values from the input file I've submitted.
I've been shown how to use pointers, but I'm not 100% on how to apply it. I can only find information on individual uses. Like say read file, and assign pointers. However I've never seen them used simultaneously. Was hoping to see if anyone had some example of that too.

Your post has been very helpful as well. I finally was able to compile it with your help.

#include <stdio.h>
#include <math.h>
#define EOF (-1)

int main()
{
    int file, number, sum = 0, sum_squares = 0, n = 0;
    file = scanf("%d", &number);

    if(file != EOF)
    {
        while(file != EOF)
        {
            sum += number;
            sum_squares += number * number;
            ++n;
        }

        if (n > 0)
        {
            float average = (float)sum / n;
            float variance = (float)sum_squares / n - (average * average);
            float std_dev = sqrt(variance);
            printf("The average is %d\n", average);
            printf("The standard deviation is %d\n", std_dev);
        }
    }
    return 0;
}

So here's what I have so far. I've been able to compile it, but the issue it it's still not generating a result.
This code comes up blank, for example

$ .sdev < input.dat

Would result in blank spaces.

I can't remember what I did previously, but I was able to generate a result even though the answer was incorrect. Something like:

$ ./sdev < input.dat

Would generate:

The average is 0
The standard deviation is 0

I got the statement printed, but I assumed that the program wasn't reading the file correctly therefore just generating the result as 0.

#define EOF (-1)

You don't need to redefine EOF(already #defined in "stdio.h").

file = scanf("%d", &number);

You need to put this line in a loop to take consecutive inputs.

while((file = scanf("%d", &number)) == 1 && file!=EOF){
    sum += number;
    sum_squares += number * number;
    ++n;
}//repalces line 8 to 17

input:

sdev < input.dat 
where input.dat contains
1
2
3
4

output:

The average is 2.500000
The standard deviation is 1.118034
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.