day 22 hw
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
/* Declare variables. */
int se=0,s=0,e=0, r, c, k, maxk=0;
int grid[5][5], maxsum[8]={0,0,0,0,0,0,0,0};
double perc;
char* direction[8]={"N ","NE ","E ","SE ","S ",
"SW ","W ","NW "};
ifstream winds;
/* Read and print information from the file. */
winds.open("winds1.txt");
if (winds.fail())
cout << "Error opening file." << endl;
else
{
for (r=0; r<=4; r++)
winds >> grid[r][0] >> grid[r][1] >> grid[r][2]
>> grid[r][3] >> grid[r][4];
// Determine sums for the direction categories.
for (r=0; r<=4; r++)
{
for (c=0; c<=4; c++)
{
k = grid[r][c];
maxsum[k]++;
if(grid[r][c]==3)
{
e++;
}
if(grid[r][c]==4)
{
se++;
}
if(grid[r][c]==5)
{
s++;
}
}
}
// Determine category with maximum sum.
for (k=0; k<=7; k++)
if (maxsum[k] > maxsum[maxk])
{
maxk = k;
}
// Print report.
cout.setf(ios::fixed);
cout.precision(1);
perc = (double)maxsum[maxk]/25*100;
cout << "The wind is blowing from "<< direction[maxk-1] << perc << "% of the time." << endl;
cout << "Numbers of times of SE:" << e << endl;
cout << "Numbers of times of SE:" << se << endl;
cout << "Numbers of times of S:" << s << endl;
cout << "The direction closest to the maximum value" << endl << "(since there are no values that happen 19 times) is :" << direction[maxk] ;
cout << ", occuring: " << s << " times" << endl;
// Close file.
winds.close();
}
// Exit program.
return 0;
}
#include <fstream>
using namespace std;
int main(void)
{
/* Declare variables. */
int se=0,s=0,e=0, r, c, k, maxk=0;
int grid[5][5], maxsum[8]={0,0,0,0,0,0,0,0};
double perc;
char* direction[8]={"N ","NE ","E ","SE ","S ",
"SW ","W ","NW "};
ifstream winds;
/* Read and print information from the file. */
winds.open("winds1.txt");
if (winds.fail())
cout << "Error opening file." << endl;
else
{
for (r=0; r<=4; r++)
winds >> grid[r][0] >> grid[r][1] >> grid[r][2]
>> grid[r][3] >> grid[r][4];
// Determine sums for the direction categories.
for (r=0; r<=4; r++)
{
for (c=0; c<=4; c++)
{
k = grid[r][c];
maxsum[k]++;
if(grid[r][c]==3)
{
e++;
}
if(grid[r][c]==4)
{
se++;
}
if(grid[r][c]==5)
{
s++;
}
}
}
// Determine category with maximum sum.
for (k=0; k<=7; k++)
if (maxsum[k] > maxsum[maxk])
{
maxk = k;
}
// Print report.
cout.setf(ios::fixed);
cout.precision(1);
perc = (double)maxsum[maxk]/25*100;
cout << "The wind is blowing from "<< direction[maxk-1] << perc << "% of the time." << endl;
cout << "Numbers of times of SE:" << e << endl;
cout << "Numbers of times of SE:" << se << endl;
cout << "Numbers of times of S:" << s << endl;
cout << "The direction closest to the maximum value" << endl << "(since there are no values that happen 19 times) is :" << direction[maxk] ;
cout << ", occuring: " << s << " times" << endl;
// Close file.
winds.close();
}
// Exit program.
return 0;
}
Comments
Post a Comment