Question: #include #include / / Define the number of months and days const int MONTHS = 3 ; const int DAYS = 3 0 ; /

#include
#include
// Define the number of months and days
const int MONTHS =3;
const int DAYS =30;
// Function prototypes
void readWeatherData(char (*data)[DAYS], const char* filename);
void calculateAndPrintReport(const char (*data)[DAYS]);
int countWeather(const char *data, char type);
void findMonthWithMostRain(const char (*data)[DAYS]);
int main(){
//1. Initialize a 3x302D array of characters to store weather data
char weatherData[MONTHS][DAYS];
//2. Read weather data from the file
readWeatherData(weatherData, "RainOrShine.txt");
//3. Generate and print the weather report
calculateAndPrintReport(weatherData);
//4. Identify which month had the most rainy days
findMonthWithMostRain(weatherData);
return 0;
}
// Function to read weather data from a file
void readWeatherData(char (*data)[DAYS], const char* filename){
std::ifstream inFile(filename);
if (!inFile.is_open()){
std::cerr << "Error opening file.";
exit(1);
}
for (int month =0; month < MONTHS; ++month){
for (int day =0; day < DAYS; ++day){
inFile >>*(*(data + month)+ day);
}
}
inFile.close();
}
// Function to count the number of specific weather type days
int countWeather(const char *data, char type){
int count =0;
for (int day =0; day < DAYS; ++day){
if (*(data + day)== type){
++count;
}
}
return count;
}
// Function to calculate and print the weather report
void calculateAndPrintReport(const char (*data)[DAYS]){
const char* months[MONTHS]={"June", "July", "August"};
int totalSunny =0, totalCloudy =0, totalRainy =0;
for (int month =0; month < MONTHS; ++month){
int sunnyDays = countWeather(*(data + month),'S');
int cloudyDays = countWeather(*(data + month),'C');
int rainyDays = countWeather(*(data + month),'R');
std::cout << months[month]<<"- Sunny Days: "<< sunnyDays
<<", Cloudy Days: "<< cloudyDays <<", Rainy Days: "<< rainyDays << std::endl;
totalSunny += sunnyDays;
totalCloudy += cloudyDays;
totalRainy += rainyDays;
}
std::cout << "Total - Sunny Days: "<< totalSunny <<", Cloudy Days: "<< totalCloudy
<<", Rainy Days: "<< totalRainy << std::endl;
}
// Function to find the month with the most rainy days
void findMonthWithMostRain(const char (*data)[DAYS]){
const char* months[MONTHS]={"June", "July", "August"};
int maxRainyDays =0;
int maxRainyMonth =0;
for (int month =0; month < MONTHS; ++month){
int rainyDays = countWeather(*(data + month),'R');
if (rainyDays > maxRainyDays){
maxRainyDays = rainyDays;
maxRainyMonth = month;
}
}
std::cout << "The month with the most rainy days is "<< months[maxRainyMonth]<< std::endl;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!