Please write a C++ program by Visual Studio. Instructions Instructions Write a p
ID: 3727924 • Letter: P
Question
Please write a C++ program by Visual Studio.
Instructions Instructions Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. 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 unctions: Function getdata: This function reads and stores data in the two- dimensional arrav. Function averageHigh: This function calculates and returns the average high temperature for the year. Function averageLow:This function calculates and returns the average temperature for the Function indexHighTemp: This function returns the index of the highest high temperature in the array. ow year. Function indexLowTenp: This function returns the index of the lowest in the array. These functions must all have the appropriate parametersExplanation / Answer
#include <iostream>
using namespace std;
void getData(double temperatures[12][2])
{
int i,j;
cout<<"Enter the highest and lowest temperatures for 12 months : ";
for(i=0;i<12;i++) // input temperatures
{
for(j=0;j<2;j++)
cin>>temperatures[i][j];
}
}
double averageHigh(double temperatures[12][2])
{
int i,j = 0; // highest temperature is at index 1
double avgHigh = 0;
for(i=0;i<12;i++)
{
avgHigh = avgHigh + temperatures[i][j];
}
return avgHigh/12; // average for 12 months
}
double averageLow(double temperatures[12][2])
{
int i,j = 1; // lowest temperature is at index 2
double avgLow = 0;
for(i=0;i<12;i++)
{
avgLow = avgLow + temperatures[i][j];
}
return avgLow/12; // average for 12 months
}
int indexHighTemp(double temperatures[12][2])
{
int indexHigh,i; // highest temperature is at index 1
double highest =temperatures[0][0];
indexHigh = 0;
for(i=1;i<12;i++)
{
if(temperatures[i][0]> highest)
{
indexHigh = i;
highest = temperatures[i][0];
}
//cout<<" "<<highest <<" "<<indexHigh;
}
return (indexHigh+1); // month with highest temperature
}
int indexLowTemp(double temperatures[12][2])
{
int indexLow,i; // lowest temperature is at index 2
double lowest =temperatures[0][1];
indexLow = 0;
for(i=1;i<12;i++)
{
if(temperatures[i][1]< lowest)
{
indexLow = i;
lowest = temperatures[i][1];
}
//cout<<" "<<lowest <<" "<<indexLow;
}
return (indexLow+1); // month with lowest temperature
}
int main(void) {
int i,j;
double temperatures[12][2];
getData(temperatures);
cout<<" Highest and lowest temperatures for 12 months : ";
for(i=0;i<12;i++)
{
cout<<" Month "<<(i+1)<<" : ";
for(j=0;j<2;j++)
{
cout<<temperatures[i][j]<<" ";
}
}
cout<<" Average high Temperature : "<<averageHigh(temperatures);
cout<<" Average Low Temperature : "<<averageLow(temperatures);
cout<<" The month with the highest temperature : "<<indexHighTemp(temperatures);
cout<<" The month with the Lowest temperature : "<<indexLowTemp(temperatures);
return 0;
}
output:
Enter the highest and lowest temperatures for 12 months :
Highest and lowest temperatures for 12 months :
Month 1 : 12.3 -9.5
Month 2 : 16.4 3.6
Month 3 : 20.8 6.7
Month 4 : 24.3 10.9
Month 5 : 30.6 12.6
Month 6 : 35.4 17.4
Month 7 : 36.6 18.1
Month 8 : 37.5 19.7
Month 9 : 35.8 20.2
Month 10 : 27.6 16.5
Month 11 : 14.5 15.6
Month 12 : 10.2 -6.7
Average high Temperature : 25.1667
Average Low Temperature : 10.425
The month with the highest temperature : 8
The month with the Lowest temperature : 1
If its helpful, please upvote. Do ask if there are any queries.