/* STEM-AND-LEAF: This C++ program makes a quick stem-and-leaf plot of data contained a user-specified file, a required command-line argument. The data are grouped in horizontal bins of default width 10. The user may provide her own integer bin width with an optional second command-line argument. shinnerl@ucla.edu. Last modified: 4 Feb. 2002 */ #include #include #include #include #include #include #include const int MAX = 500; // Maximum number of data values in the file histdat const int DEFAULT_BIN_WIDTH = 10; double square( double x ) { return x*x; } double mean (int a[], int n) { int i=0, sum=0, j=0; double average=0.0; for(i=0;i < n; ++i) sum += a[i]; average = (double)sum/(double)n; return (average); } double stdev(int a[], int n) // Standard Deviation. { int i=0, j=0; double sd=0.0, m=0.0, sum=0.0, diff=0.0, total=0.; m = mean(a,n); sum=0.0; for(i=0; i= 2 ) { infile.open(argv[1]); fail = !infile; if (!fail && argc >=3 ){ binWidth = atoi( argv[2] ); if (argc >=4 ){ outfile.open( argv[3] ); fail = !outfile; osPtr = &outfile; } } } else fail = true; if (fail) { cout << "Calling Syntax: hist [bin width [output file]]\n"; exit(1); } return binWidth; } int main(int argc, char *argv[]) { ifstream infile; ofstream outfile; ostream *osPtr = &cout; int binWidth = getCommandLine( argc, argv, infile, outfile, osPtr ); // Create a static array to support large data sets; // initialize entire array to 0. static int scores[MAX] = {0}; // Data input from file. int count =0; while ( infile >> scores[count] ) ++count; infile.close(); // Display the stem-and-leaf plot and statistics // to the specified output stream: std::sort ( scores, scores+count); printHist ( scores, count, binWidth, *osPtr ); printStats( scores, count, binWidth, *osPtr ); if (outfile) outfile.close(); return EXIT_SUCCESS; }