/*** This program takes arguments from the command line. This means it expects some input from the user when the program is executed. In this case the name of the file containing the data is read in at run-time. A typical command would look like > ./stats tmp.dat The program reads the data from the a file (called tmp.dat) to a variable, x. You don't need to know the number of data points in the file a priori. The integer 'i' counts them in the while/for loop. ***/ #include <stdio.h> #include <math.h> main(int argc, char *argv[]) /* this is no longer a set of empty () as the program expects some input arguments. The format is always the same. */ { double x,sigma,x_m; int i; FILE *ifp; /* as in the other program data is read from a file so these lines are required. Note that fopen has "argv[1]" instead of the usual file name. This stands for "argument value number 1. argv[0] is always the program name and argv[i] is whatever comes after it. In this case there's just one argument - tmp.dat. */ ifp = fopen(argv[1], "r"); i = 0; sigma = 0.0; x_m = 0.0; /* while( fscanf(ifp, "%lf", &x) ==1 ) { x_m += x; i++; } x_m /= (double)i; */ for ( i=0; fscanf(ifp, "%lf", &x) ==1; i++) { x_m += x; } x_m /= (double)i; ifp = fopen(argv[1], "r"); while ( fscanf(ifp, "%lf", &x ) ==1 ) { sigma += (x -x_m)*(x -x_m); } sigma = sqrt(sigma/N); printf("the mean is %lf \n the standard deviation is %lf\n", x_m, sigma); }