In: Computer Science
An amateur meteorologist wants to keep track of weather conditions during the past year's three -month summer season and has designated each day as either rainy ('R'), cloudy ('C'), or sunny ('S'). Write a modu lar program that stores this information in a 3 x 30 array of characters, where the row indicates the month (0 = June, 1 = July, 2 = August) and the column indicates the day of the month. Note that data is not being collected for the 31st of any month . The program should begin by calling a function to read the weather data in from a file. Then it 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. To help it do this, it should use a value -returning function that is passed the array, the number of the month to examine, and the character to look for ('R', 'C', or 'S') . This function shou ld return the number of days the indicated month had the requested weather. Data for the program can be found in the Rai nOrShi ne. da t file located in the Chapter 8 programs folder on this book's companion website. please do this program in c++ and please add comments.
C++ Program :
#include <iostream>
using namespace std;
//This function helps in reading the weather data from the file
RainOrShine.dat
//I have assumed that the file contains data in the following
format as it was not specified
//in the question:
//RRRRSSSS...CCR ---> representing days of
june
//RRRRSSSS...CCR ---> representing days of
july
//RRRRSSSS...CCR ---> representing days of
august
void readFromFile(char *weatherData[3]){
string line;
ifstream myfile("RainOrShine.dat");
if(myfile.is_open()){
int month = 0;
while(getline(myfile,line)){
for(int i=0;i<30;i++){
weatherData[month][i] = line[i];
}
month++;
}
myfile.close();
}else{
cout<<"Unable to
open data file"<<endl;
}
}
//This is the helper function which accepts the monthNumber and
the condition of the
//day to be looked for i.e. whether rainy,cloudy or sunny and
returns the number of
//days in that month having that condition.
int analysis(char *weatherData[3],int monthNumber,char
condition){
int ans = 0;
for(int i=0;i<30;i++){
if(weatherData[monthNumber][i]
== condition)ans++;
}
return ans;
}
int main() {
char *weatherData[3];
for(int i=0;i<3;i++){
weatherData[i] = new
char[30];
}
//Now we have a 2d character array of size
3x30
//This reads the file and stores the data into
the char array
readFromFile(weatherData);
//Output for june
cout<<"FOR JUNE:\n";
cout<<"Number of rainy days:
"<<analysis(weatherData,0,'R')<<endl;
cout<<"Number of sunny days:
"<<analysis(weatherData,0,'S')<<endl;
cout<<"Number of cloudy days:
"<<analysis(weatherData,0,'C')<<endl;
cout<<endl;
//Output for july
cout<<"FOR JULY:\n";
cout<<"Number of rainy days:
"<<analysis(weatherData,1,'R')<<endl;
cout<<"Number of sunny days:
"<<analysis(weatherData,1,'S')<<endl;
cout<<"Number of cloudy days:
"<<analysis(weatherData,1,'C')<<endl;
cout<<endl;
//Output for august
cout<<"FOR AUGUST:\n";
cout<<"Number of rainy days:
"<<analysis(weatherData,2,'R')<<endl;
cout<<"Number of sunny days:
"<<analysis(weatherData,2,'S')<<endl;
cout<<"Number of cloudy days:
"<<analysis(weatherData,2,'C')<<endl;
return 0;
}