//Listing 14.03 Processing Enviromental Data
#include <fstream> // ifstream, ofstream
#include <cassert> // assert()
#include <cfloat> // DBL_MIN and DBL_MAX
#include <iostream>
using namespace std;
int main() {
char input_file_name[80] = "";
char output_file_name[80] = "";
int count = 0, // number of values
min_day = 0, // day min. reading occurred
max_day = 0; // day max. reading occurred
double reading, // value being processed
minimum = DBL_MAX, // largest seen so far
maximum = DBL_MIN, // smallest seen so far
sum = 0.0; // running total
cout<<"Enter the name of the input file: ";
cin >> input_file_name; // get name of input file
ifstream fin(input_file_name, ios::in); // establish connection,
assert( fin.is_open() ); // and check for success
while(true){
fin >> reading; // read a value
if( fin.eof() ) // if eof, quit
break;
++count; // update: count,
sum += reading; // sum
if(reading < minimum){
minimum = reading;
min_day = count;
}
if(reading > maximum){
maximum = reading;
max_day = count;
}
}
fin.close(); // close the connection
// PARTE 2
cout<<"Enter the name of the output file: ";
cin >> output_file_name; // get name of output file
ofstream fout(output_file_name, ios::out); // establish connection,
assert( fout.is_open() ); // and check for success
if (count == 0)
fout<<"No values were read\n";
else
fout<<"Average temperature reading\t:\t"<<sum/count<<endl
<<"Minimum temperature reading\t:\t"<<minimum
<<"\t\ton day "<<min_day<<endl
<<"Maximum temperature reading\t:\t"<<maximum
<<"\t\ton day "<<max_day<<endl;
fout.close(); // close the connection
cout<<"Processing complete.\n";
return 0;
}
xd