// kent archie #include #include using namespace std; int main(int argc, char * argv[]) { ifstream tin; // input file ofstream tout; // output file float sum=0,avg,temp; float maxtemp, mintemp; int count=1; // how many were read // the argv array contains strings of the data passed // from the command line. // argv[0] is a string that contains the name of the program. // If the program is run like this // filetemps tdata.txt tdataout.txt // then the array looks like: // argv[0] = "filetemps" // argv[1] = "tdata.txt" // argv[2] = "tdataout.txt" // and argc contains the number of arguments, in this case 3. // To use this, it is always wise to check the number of arguments // is what you expect. if(argc != 3) { cerr << "Usage: filetemps infilename outfilename" << endl; exit(1); } tin.open(argv[1]); tout.open(argv[2]); while(tin) // loops until file is empty { tin >> temp; // set the initial max and min values to // the first temp. This will work correctly // regardless of the temp values. if(count == 1) { mintemp = temp; maxtemp=temp; } if(temp > maxtemp) // new greatest temp maxtemp=temp; if(temp < mintemp) // new greatest temp mintemp=temp; sum += temp; count++; } tin.close(); // done reading the data avg=sum/count; // first write the report to the screen cout << "There were = " << count << " data points" << endl; cout << "minimum temp = " << mintemp << endl; cout << "maximum temp = " << maxtemp << endl; cout << "sum temp = " << sum << endl; cout << "avg temp = " << avg << endl; // Now write the report to the file tout << "There were = " << count << " data points" << endl; tout << "minimum temp = " << mintemp << endl; tout << "maximum temp = " << maxtemp << endl; tout << "sum temp = " << sum << endl; tout << "avg temp = " << avg << endl; tout.close(); // done writing the report return 0; } // main