C++ Write a program that uses a two-dimensional array to store the highest and l
ID: 3812437 • Letter: C
Question
C++ Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year.
Have users enter the data.
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 - dimensional array.
b. Function averageHigh: This function calculates and returns the average high temperature for the year.
c. Function averageLow: This function calculates and returns the average low temperature for the year.
d. Function indexHighTemp: This function returns the index of the highest high temperature in the array.
e. Function indexLowTemp: This function returns the index of the lowest low temperature in the array.
(These functions must all have the appropriate parameters.)
Explanation / Answer
Here is the code for you:
#include <iostream>
using namespace std;
// getData: This function reads and stores data in the twodimensional array.
void getData(double temperatures[][2])
{
for(int i = 0; i < 12; i++)
{
cout<<"Enter the highest temperature for month "<<i+1<<": ";
cin>>temperatures[i][0];
cout<<"Enter the lowest temperature for month "<<i+1<<": ";
cin>>temperatures[i][1];
}
}
void displayData(double temperatures[][2])
{
cout<<"Month# High Low"<<endl;
for(int i = 0; i < 12; i++)
cout<<i+1<<" "<<temperatures[i][0]<<" "<<temperatures[i][1]<<endl;
}
//Function indexHighTemp: This function returns the index of the highest high temperature in the array.
int indexHighTemp(double temperatures[][2])
{
int indexHigh = 0;
for(int i = 0; i < 12; i++)
if(temperatures[i][0] > temperatures[indexHigh][0])
indexHigh = i;
return indexHigh;
}
//Function indexLowTemp: This function returns the index of the lowest low temperature in the array.
int indexLowTemp(double temperatures[][2])
{
int indexLow = 0;
for(int i = 0; i < 12; i++)
if(temperatures[i][1] < temperatures[indexLow][1])
indexLow = i;
return indexLow;
}
//averageHigh: This function calculates and returns the average high temperature for the year.
double averageHigh(double temperatures[][2])
{
double sumHigh = 0;
for(int i = 0; i < 12; i++)
sumHigh += temperatures[i][0];
return sumHigh / 12;
}
//averageLow: This function calculates and returns the average low temperature for the year.
double averageLow(double temperatures[][2])
{
double sumLow = 0;
for(int i = 0; i < 12; i++)
sumLow += temperatures[i][1];
return sumLow / 12;
}
int main()
{
//uses a two-dimensional array to store the
//highest and lowest temperatures for each month of the year
double temperatures[12][2];
getData(temperatures); //getData: Read values into the array
displayData(temperatures); //displayData: Print out the highs and lows for each month
cout<<"The average of the highest temperatures is: "<<averageHigh(temperatures)<<endl;
cout<<"The average of the lowest temperatures is: "<<averageLow(temperatures)<<endl;
cout<<"The index of the highest temperatures is: "<<indexHighTemp(temperatures)<<endl;
cout<<"The index of the lowest temperatures is: "<<indexLowTemp(temperatures)<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.