简体   繁体   中英

Text file with C in terminal

So I calculated the mean and standard deviation from double values read within a file.

My file data has 1 number per line: My data in the file is the following

1

2

3

4

5

6

7

8

9

10

My code is below:

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

int main(){

    FILE *inputfile;
    char name[100];

    printf("Enter the file you want to use to calculate standard deviation:\n ");
    gets(name); 
    inputfile = fopen(name, "r");

    if (inputfile == NULL)
    {
        printf("Failed to open text file.\n");
        exit(1);
    }

    double i; 
    double j=1;
    double average; 
    double stdish=0;
    double stdreal=0; 
    double x=0;
    double sum=0;
    double stdfinal;

    while(fscanf(inputfile, "%lf", &i) != EOF){
        x=x+1;
        sum = sum + i;
        j =pow(i,2);
        stdreal +=j;
    }
        average = sum/x;
        stdish = (stdreal/x)-(pow(average,2));
        stdfinal = sqrt(stdish);

    printf("The average is %.4lf\n", average);
    printf("The standard deviation is %.4lf\n", stdfinal);


return 0;
}

My standard deviation is incorrect, and I am not sure why. In my program, I use fopen to get the text from the input file. Also, I am trying to make it so that I input the text file from the terminal instead of in the actual program itself. How to do that?

First you need to find out the average (mean) and then iterate through the loop to find out the variance. The SQRT(variance) will give you standard deviation.

double CalculateMean() 
{ 
    double sum = 0;  
    for(int i = 0; i < max; i++) 
        sum += value[i]; 
    return (sum / max); 
} 

double CalculateVariane() 
{ 
    mean = CalculateMean(); 
    double temp = 0;  

    for(int i = 0; i < max; i++) 
    { 
         temp += (value[i] - mean) * (value[i] - mean) ; 
    } 
    return temp / max; 
} 

Reference: http://www.softwareandfinance.com/CPP/MeanVarianceStdDevi.html

Declare main as int main(int argc, char *argv[]) or as int main(int argc, char **argv) . They mean the same thing. In this version, argc is the number of command line arguments (+1 because the program name is argument 0). argv is an array of char * s, each of which is a command line argument.

You can do something like this:

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

    FILE *inputfile;
    char name[100];

    if(argc == 1) {
        printf("Enter the file you want to use to calculate standard deviation:\n ");
        gets(name);
    } else {
        strcpy(name, argv[1]);
    }
    inputfile = fopen(name, "r");

    if (inputfile == NULL)
    {
        printf("Failed to open text file.\n");
        exit(1);
    }

    double i; 
    double j=1;
    double average; 
    double stdish=0;
    double stdreal=0; 
    double x=0;
    double sum=0;
    double stdfinal;

    while(fscanf(inputfile, "%lf", &i) != EOF){
        x=x+1;
        sum = sum + i;
        j =pow(i,2);
        stdreal +=j;
    }
    average = sum/x;
    stdish = (stdreal/x)-(pow(average,2));
    stdfinal = sqrt(stdish);

    printf("The average is %.4lf\n", average);
    printf("The standard deviation is %.4lf\n", stdfinal);


    return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM