Turbozedd, I am sooo close I can taste it! Ok, first off, I have to have the input in the same loop as the output or I will be docked points. Here is my code as of now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inputfile;
ofstream outputfile;
int nmbryears;
double data[20][12];
int firstyear;
int i;
int j;
double sum;
double average;
inputfile.open ("rainfall.txt");
if (!inputfile)
cout<<"There was an error opening the file."<<endl;
else
{
outputfile.open ("output.txt");
inputfile.ignore(71);
inputfile >> nmbryears;
inputfile.ignore(75);
inputfile >> firstyear;
outputfile << "There were "<<nmbryears<<" years of data."<<endl;
outputfile << "Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Total"<<endl;
outputfile << "----------------------------------------------------------"<<endl;
outputfile << firstyear << " ";
for(i=1; i<=nmbryears; i++)
{
sum=0;
for(j=0; j<12; j++)
{
inputfile >> data[i][j];
outputfile << showpoint << setprecision(2);
outputfile << data[i][j] << " ";
sum+=data[i][j];
}
outputfile << showpoint << setprecision(3);
outputfile << sum;
outputfile << endl;
outputfile << firstyear+i << " ";
}
outputfile << endl;
}
average=(sum/(nmbryears*12));
outputfile << "----------------------------------------------------------"<<endl;
outputfile <<"The monthly average over "<<nmbryears<<" years was "<<average<<endl;
inputfile.close();
outputfile.close();
return 0;
}
|
And heres my output:
There were 10 years of data.
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Total
----------------------------------------------------------
1995 5.4 2.4 3.6 7.8 3.3 2.9 2.2 2.3 3.4 4.3 4.5 4.0 46.1
1996 2.0e+003 3.7 2.9 3.8 9.3 2.6 4.1 3.2 2.8 3.2 3.8 2.7 2.04e+003
1997 3.3 2.0e+003 4.5 3.2 3.2 5.3 4.1 3.6 3.6 2.1 3.7 4.1 2.04e+003
1998 2.0 2.8 2.0e+003 2.7 2.8 3.0 7.2 3.8 1.8 1.9 1.7 2.2 2.03e+003
1999 3.1 2.7 2.5 2.0e+003 3.0 2.1 3.3 5.5 2.7 2.1 2.5 2.7 2.03e+003
2000 2.8 3.5 3.8 4.3 2.0e+003 2.9 2.4 3.7 4.2 3.5 1.6 2.8 2.04e+003
2001 2.5 3.5 3.4 2.3 2.5 2.0e+003 4.9 3.5 2.9 3.8 2.1 2.3 2.03e+003
2002 1.5 1.5 2.1 2.8 2.5 2.1 2.0e+003 1.5 1.7 1.8 2.2 1.0 2.02e+003
2003 1.6 1.8 1.6 1.9 2.5 3.2 3.7 2.0e+003 2.1 2.8 2.9 4.8 2.03e+003
2004 5.5 6.7 5.3 4.7 4.3 3.9 4.1 3.9 2.0e+003 3.1 3.2 2.8 2.05e+003
2005
----------------------------------------------------------
The monthly average over 10 years was 17.1
Ok, so I made some minor changes to my previous code, using your help, and a little bit of my intelligence for once :). I set
i=1
and changed the condition of the outer loop to
i<=nmbryears
. So far so good. Unfortunately, in my output I'm getting very weird numbers that are shifting every other number to the right. ex: 2.0e+003. I tried fooling around with the code, but I couldn't manage to get rid of those numbers. Also, the year 2005 is lingering on the bottom, and once again, I have no idea why... Lastly :P, I know my code for calculating the sum
average=(sum/(nmbryears*12));
is wrong. Thank you soo much for your help!
Edit: Oh, and my braces are lined up in my actual code, but when I post it here they get messed up, sorry!