Sample Solutions to first Assignment. the C++/iostream
way
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
const int MAX = 9000;
int year[MAX], month[MAX], day[MAX], time[MAX], temp[MAX], hum[MAX],
wspd[MAX], wdir[MAX], pres[MAX];
int nrecords=0;
int StringToInt(string s)
{ int len=s.length();
int value=0;
char sign='+';
for (int i=0; i<len; i+=1)
{ char c=s[i];
if ((c=='+' || c=='-') && i==0)
sign=c;
else if (c<'0' || c>'9')
break;
else
value=value*10+(c-'0'); }
if (sign=='-') value=-value;
return value; }
void main(void)
{ ifstream infile("weather.dat");
if (infile.bad())
{ cout << "Could not read weather.dat" << endl;
exit(1); }
while (nrecords<MAX)
{ string yr, mn, dy, tim, tmp, hmd, ws, wd, pr;
infile >> setw(5) >> yr >> setw(3) >> mn >> setw(3) >> dy
>> setw(5) >> tim >> setw(5) >> tmp >> setw(4) >> hmd
>> setw(4) >> ws >> setw(4) >> wd >> setw(4) >> pr;
if (infile.eof()) break;
year[nrecords]=StringToInt(yr);
month[nrecords]=StringToInt(mn);
day[nrecords]=StringToInt(dy);
time[nrecords]=StringToInt(tim);
temp[nrecords]=StringToInt(tmp);
hum[nrecords]=StringToInt(hmd);
wspd[nrecords]=StringToInt(ws);
wdir[nrecords]=StringToInt(wd);
pres[nrecords]=StringToInt(pr);
nrecords+=1; }
infile.close();
cout << "1 = average temperature throughout year" << endl;
cout << "2 = average temperature for a particular month" << endl;
cout << "3 = average temperature for a particular time of day" << endl;
cout << "4 = find windiest moment" << endl;
cout << "5 = full report on a particular moment" << endl;
cout << "6 = exit" << endl;
while (1)
{ cout << "option: ";
int option;
cin >> option;
if (option==1) avg_temp_all_year();
else if (option==2) avg_temp_all_month();
else if (option==3) avg_temp_for_time();
else if (option==4) find_windiest();
else if (option==5) report_on_moment();
else if (option==6) break;
else cout << "What are you talking about?" << endl; } }
// The following functions would of course be defined before main:
void avg_temp_all_year(void)
{ float total=0.0;
for (int i=0; i<nrecords; i+=1)
total+=temp[i];
float avg=total/nrecords;
cout << "Average temperature over all records is "
<< setprecision(3) << avg << " degrees Fahrenheit" << endl; }
void avg_temp_all_month(void)
{ cout << "which month (number 1-12): ";
int mth;
cin >> mth;
float total=0.0, count=0.0;
for (int i=0; i<nrecords; i+=1)
if (month[i]==mth)
{ total+=temp[i];
count+=1.0; }
float avg=total/count;
if (count==0.0)
cout << "No records have month = " << mth << endl;
else
cout << "Average temperature in month " << mth << " is "
<< setprecision(3) << avg << " degrees Fahrenheit" << endl; }
void avg_temp_for_time(void)
{ cout << "what time (number 0000-2359): ";
int tim;
cin >> tim;
float total=0.0, count=0.0;
for (int i=0; i<nrecords; i+=1)
if (time[i]==tim)
{ total+=temp[i];
count+=1.0; }
float avg=total/count;
if (count==0.0)
cout << "No records have time = " << tim << endl;
else
cout << "Average temperature at time " << setw(4) << setfill('0') << tim << " is "
<< setprecision(3) << avg << " degrees Fahrenheit" << endl; }
void find_windiest(void)
{ int windex=0, wmax=-999999;
for (int i=0; i<nrecords; i+=1)
if (wspd[i]>wmax)
{ wmax=wspd[i];
windex=i; }
cout << "Windiest moment is at " << setw(4) << setfill('0') << time[windex] << " on "
<< setw(4) << setfill('0') << year[windex] << "/"
<< setw(2) << setfill('0') << month[windex] << "/"
<< setw(2) << setfill('0') << day[windex]
<< ", speed was " << wmax << " mph" << endl; }
void report_on_moment(void)
{ int yr, mn, dy, tm;
cout << "enter year month date time: ";
cin >> yr >> mn >> dy >> tm;
int found=0;
for (int i=0; i<nrecords; i+=1)
if (year[i]==yr && month[i]==mn && day[i]==dy && time[i]==tm)
{ found=1;
cout << "on " << setw(4) << setfill('0') << year[i] << "/"
<< setw(2) << setfill('0') << month[i] << "/"
<< setw(2) << setfill('0') << day[i]
<< " at " << setw(4) << setfill('0') << time[i] << endl;
cout << " temperature " << (temp[i]>=0 ? "+" : "-")
<< setw(3) << temp[i] << " degrees fahrenheit" << endl;
cout << " humidity " << setw(3) << hum[i] << " %" << endl;
cout << " wind speed " << setw(3) << wspd[i] << " mph" << endl;
cout << " w direction " << setw(3) << wdir[i]
<< " degrees East from North" << endl; }
if (found==0)
cout << "no records for " << setw(4) << setfill('0') << yr << "/"
<< setw(2) << setfill('0') << mn << "/"
<< setw(2) << setfill('0') << dy
<< " at " << setw(4) << setfill('0') << tm << endl; }
So don't ever let anyone tell you the C++ way is easier.