My numbers are not being read in to the two dimensional array correctly and i'm completely lost :confused:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int SIZE_MONTH=4;
const int SIZE_DAYS=31;
int main()
{
float temperatures[SIZE_MONTH][SIZE_DAYS];
string months[SIZE_MONTH]={"January","February","March","April"};
ifstream inFile;
inFile.open("temp1.dat");
if (!inFile)
cout<<"File not found.";
else
{
for (int i=0; i< SIZE_MONTH; i++)
{
for (int k=0; k<SIZE_DAYS; k++)
{
inFile>>temperatures[i][k];
cout<<temperatures[i][k]<<" ";
}
}
inFile.close();
float sum=0.0;
for (int k=0; k<SIZE_DAYS; k++)
{
sum+=temperatures[1][k];
}
float average=sum/28;
float lowest=temperatures[0][0];
string month;
for (int i=0; i< SIZE_MONTH; i++)
{
for (int k=0; k<SIZE_DAYS; k++)
{
if(temperatures[i][k]<lowest)
{
lowest=temperatures[i][k];
month=months[i];
}
}
}
cout<<"The average for the month of February is "<<average<<"."<<endl;
cout<<"The month with the lowest temp is "<<month<<" and the temperature is "<<lowest<<".";
}
return 0;
}
And this is the data file:
0.0 31 6 0.0 44 16 1.12 62 48
2.31 74 50 2.88 77 53 2.45 81 50 1.18 92 66 .34 98 74 .91
1.03 68 44 .78 49 31
0.0 43 26
and this is my output:
0 31 6 0 44 16 1.12 62 48 2.31 74 50 2.88 77 53 2.45 81 50 1.18 92 66 0.34 98 74
0.91 87 59 1.03 68 44 0.78 49 31 0 43 26 -1.07374e+008 -1.07374e+008 -1.07374e+
008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.073
74e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1
.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+00
8 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374
e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.0
7374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008
-1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+
008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.073
74e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1
.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+00
8 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374
e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.0
7374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008
-1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+
008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 -1.07374e+008 The av
erage for the month of February is -9.97046e+007.
The month with the lowest temp is February and the temperature is -1.07374e+008.
Press any key to continue . . .