A three-dimensional weather array for the months July and August 2011 has column
ID: 3730253 • Letter: A
Question
A three-dimensional weather array for the months July and August 2011 has columns labeled by the month 7 and 8. In each column, there are rows numbered 1 through 31, representing the days, and for each day, there are two ranks labeled H and L, representing the day's high and low temperature. Use this information to write a C++ program that assigns the high and low temperature for each element of the arrays. Then allow the user to request the following: Any day's high and low temperature. Average high and low temperature for a given month Month and day with the highest temperature Month and day with the lowest temperatureExplanation / Answer
#include <stdlib.h>
#include <iostream>
#define MONTHS 2
#define DAYS 31
#define RANKS 2
#define HIGH 0
#define LOW 1
#define MIN_TEMP -500
#define MAX_TEMP 500
using namespace std;
int record[MONTHS][DAYS][RANKS];
void input_temperature()
{
int i, j, k;
for(i = 0; i < MONTHS; i++)
{
for(j = 0; j <= DAYS; j++)
{
for(k = 0; k <= RANKS; k++)
{
cin >> record[i][j][k];
}
}
}
return;
}
void print_high_low_temp()
{
int month, day;
cout << "Enter month" << endl;
cin >> month;
cout << "Enter day" << endl;
cin >> day;
cout<< "High temperature for the day is "<< record[month][day][HIGH];
cout<< "Low temperature for the day is "<< record[month][day][LOW];
return;
}
void calculate_avg_high_low_temperature()
{
int month, sum = 0, i;
float avg;
cout << "Enter month" << endl;
cin >> month;
for(i = 0; i < DAYS; i++)
{
sum += record[month][i][HIGH];
}
avg = sum/DAYS;
cout << "Average high temperature is "<< avg << endl;
sum = 0;
for(i = 0; i < DAYS; i++)
{
sum += record[month][i][LOW];
}
avg = sum/DAYS;
cout << "Average low temperature is "<< avg << endl;
return;
}
void search_highest_temperature_day()
{
int max_temp, max_month, max_day, i, j;
max_temp = record[0][0][HIGH];
max_day = 0;
max_month = 0;
for(i = 0; i < MONTHS; i++)
{
for(j = 0; j < DAYS; j++)
{
if(max_temp < record[i][j][HIGH])
{
max_temp = record[i][j][HIGH];
max_month = i + 7;
max_day = j + 1;
}
}
}
cout << "Highest temperature "<< max_temp << endl;
cout << "Month "<< max_month << endl;
cout << "Day "<< max_day << endl;
return;
}
void search_lowest_temperature_day()
{
int min_temp, min_month, min_day, i, j;
min_temp = record[0][0][LOW];
min_day = 0;
min_month = 0;
for(i = 0; i < MONTHS; i++)
{
for(j = 0; j < DAYS; j++)
{
if(min_temp > record[i][j][LOW])
{
min_temp = record[i][j][LOW];
min_month = i + 7;
min_day = j + 1;
}
}
}
cout << "Lowest temperature "<< min_temp << endl;
cout << "Month "<< min_month << endl;
cout << "Day "<< min_day << endl;
return;
}
int main()
{
int choice;
input_temperature();
cin >> choice;
switch(choice)
{
case 1 :
{
print_high_low_temp();
break;
}
case 2 :
{
calculate_avg_high_low_temperature();
break;
}
case 3 :
{
search_highest_temperature_day();
break;
}
case 4:
{
search_lowest_temperature_day();
break;
}
default :
{
cout << "Wrong choice" << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.