I\'m currently using c++ visual studio. 9. Write a program that uses a two-dimen
ID: 3708818 • Letter: I
Question
I'm currently using c++ visual studio.
9. Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the vear. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions: a. Function getData: This function reads and stores data in the two b. Function averageHigh: This function calculates and returns the aver- c. Function averageLow: This function calculates and returns the average d. Function indexHighTemp: This function returns the index of the e. Function indexLowTemp: This function returns the index of the lowest dimensional array age high temperature for the year. low temperature for the vear. highest high temperature in the array. low temperature in the array These functions must all have the appropriate parameters.)Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
using namespace std;
void getData(int temperatures[][2]);
double averageHigh(int temperatures[][2]);
double averageLow(int temperatures[][2]);
int indexHighTemp(int temperatures[][2]);
int indexLowTemp(int temperatures[][2]);
int main()
{
int temperatures[12][2]; //12 rows of 2 temperature values
double avgHigh, avgLow;
int highIdx, lowIdx;
getData(temperatures);
avgHigh = averageHigh(temperatures);
avgLow = averageLow(temperatures);
highIdx = indexHighTemp(temperatures);
lowIdx = indexLowTemp(temperatures);
cout << "The average low temperature for the year is " << avgLow << endl;
cout << "The average high temperature for the year is " << avgHigh << endl;
cout << "The lowest temperature for the year is " << temperatures[lowIdx][0]
<< " in the month " << (lowIdx + 1)<< endl;
cout << "The highest temperature for the year is " << temperatures[highIdx][1]
<< " in the month " << (highIdx + 1)<< endl;
return 0;
}
void getData(int temperatures[][2])
{
for(int i = 0; i < 12; i++)
{
cout << "Enter the temperatures for month " << (i+1) << endl;
cout << " Lowest temperature: ";
cin >> temperatures[i][0];
cout << " Highest temperature: ";
cin >> temperatures[i][1];
cout << endl;
}
}
double averageHigh(int temperatures[][2])
{
double avg = 0;
for(int i = 0; i < 12; i++)
avg += temperatures[i][1];
avg /= 12;
return avg;
}
double averageLow(int temperatures[][2])
{
double avg = 0;
for(int i = 0; i < 12; i++)
avg += temperatures[i][0];
avg /= 12;
return avg;
}
int indexHighTemp(int temperatures[][2])
{
int highIdx = 0;
for(int i = 1; i < 12; i++)
{
if(temperatures[i][1] > temperatures[highIdx][1])
highIdx = i;
}
return highIdx;
}
int indexLowTemp(int temperatures[][2])
{
int lowIdx = 0;
for(int i = 1; i < 12; i++)
{
if(temperatures[i][0] < temperatures[lowIdx][0])
lowIdx = i;
}
return lowIdx;
}
output
--------
Enter the temperatures for month 1
Lowest temperature: 30
Highest temperature: 45
Enter the temperatures for month 2
Lowest temperature: 43
Highest temperature: 52
Enter the temperatures for month 3
Lowest temperature: 39
Highest temperature: 55
Enter the temperatures for month 4
Lowest temperature: 30
Highest temperature: 49
Enter the temperatures for month 5
Lowest temperature: 35
Highest temperature: 45
Enter the temperatures for month 6
Lowest temperature: 33
Highest temperature: 54
Enter the temperatures for month 7
Lowest temperature: 40
Highest temperature: 50
Enter the temperatures for month 8
Lowest temperature: 39
Highest temperature: 49
Enter the temperatures for month 9
Lowest temperature: 38
Highest temperature: 47
Enter the temperatures for month 10
Lowest temperature: 3
Highest temperature: 45
Enter the temperatures for month 11
Lowest temperature: 34
Highest temperature: 44
Enter the temperatures for month 12
Lowest temperature: 30
Highest temperature: 50
The average low temperature for the year is 32.8333
The average high temperature for the year is 48.75
The lowest temperature for the year is 3 in the month 10
The highest temperature for the year is 55 in the month 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.