In C++ We want to track weather conditions during the past year\'s three-month s
ID: 3678536 • Letter: I
Question
In C++
We want to track weather conditions during the past year's three-month summer season as has designated each day as either rainy ('R'), cloudy ('C'), or sunny ('S').
Write a program that stores this information in s 3x30 array of characters, where the row indicates the month and the column indicates the day of the month. Note no data is being captured for the 31st of any month.
This program will read the data in from a file called RainOrShine.txt. From this data it you should create a report that displays , for each month and for the whole three month period, how many days were rainy, how many were cloudy, and how many were sunny. It should also report which of the months had the largest number of rainy days.
The rain or shine text has the following information:
R C R S R S R R S R S C R C S S S S R S C S R C S S C C R S
C R R R S R S R C S R C R S S S R S C R R S C C S C S C R R
C R C C S C C R R R S S S C R C C C R C R S S S R S C R R C
The output should look like this:
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
ifstream inFile; //Input file stream object
char array[3][30],ch;
int r,c,rainy=0,sunny=0,cloudy=0,totalrainy=0,totalsunny=0,totalcloudy=0,large=0,month;
//Open the file
inFile.open ("RainOrShine.txt");
if (!inFile)
{
cout << "Error opening data file! ";
exit(102);
}
for (int r = 0; r < 3; r++)
{
//reads through columns
for (int c = 0; c < 30; c++)
{
ch=inFile.get();
//eliminating spaces
if(ch!=''||ch!=' ')
inFile >> array[r][c];
}
}
cout << "Report: ";
for (int r = 0; r < 3; r++)
{
//reads through columns
for (int c = 0; c < 30; c++)
{
//count number of rainy
if(array[r][c]=='R')
rainy++;
//count number of cloudy
else if(array[r][c]=='C')
cloudy++;
//count number of sunny
else if(array[r][c]=='S')
sunny++;
}
cout <<"month "<<r+1<<" rainy: "<<rainy<<" sunny: "<<sunny<<" cloudy: "<<cloudy<<" ";
//count total number rainy sunny and cloudy in 3 months
totalrainy = totalrainy + rainy;
totalcloudy = totalcloudy + cloudy;
totalsunny = totalsunny + sunny;
//finding month containing largest number of rainy days
if(rainy>large)
{
large=rainy;
month=r+1;
}
rainy=0;
cloudy=0;
sunny=0;
}
cout <<"total rainy: "<<totalrainy<<" ";
cout<<"total sunny : "<<totalsunny<<" ";
cout<<"total cloudy : "<<totalcloudy<<" ";
cout<<"month "<<month<<" has largest number of rainy days";
}
Note: The output image in the question cannot be viewed.