![]() |
| (A simple connection to the Arduino board for the BMP180) |
For the lecture portion of class we were given more insight on a C++ program that can determine which direction the trade winds will be blowing. This following is the program for the tradewinds:
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
/*
Declare variables. */
int r, c, k, maxk=0;
int grid[5][5],
category[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 input 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];
category[k]++;
}
//
Determine category with maximum sum.
for
(k=0; k<=7; k++)
if
(category[k] > category[maxk])
maxk
= k;
//
Print report.
cout.setf(ios::fixed);
cout.precision(1);
perc = (double)category[maxk]/25*100;
cout << "The wind is blowing from
the " << direction[maxk–1]
<< perc << "% of the
time." << endl;
//
Close file.
winds.close();
}
//
Exit program.
return 0;
}
For homework, we took this program and modified it to output a percentage and direction of the winds blowing, a possible hurricane/cyclone output, and also read from a data file of 100 rows by a 100 columns.

No comments:
Post a Comment