Write a program that uses a two-dimensional array to store the highest and lowest temperatures (in
Celcius) 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 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 aver-age low temperature for
the year.
#include <iostream>
using namespace std;
const int MONTHS = 12;
void dataInput(double[][2], int);
double highestAverage(double[][2], int);
double lowestAverage(double[][2], int);
int main()
{
double temperatures[MONTHS][2];
dataInput(temperatures, MONTHS);
cout << "\n\n\t The average high temperature for the year is "
<< highestAverage(temperatures, MONTHS);
cout << "\n\n\t The average low temperature for the year is "
<< lowestAverage(temperatures, MONTHS);
return 0;
}
void dataInput(double t[][2], int m)
{
for (int i = 0; i < m; i++)
{
cout << "\n\t Enter highest temperature for the month"
<< (i + 1) << " : ";
cin >> t[i][0];
cout << "\t Enter lowest temperature for the month "
<< (i + 1) << " : ";
cin >> t[i][1];
}
}
double highestAverage(double t[][2], int m)
{
double sum = 0;
for (int i = 0; i < m; i++)
sum += t[i][0];
return (sum / m);
}
double lowestAverage(double t[][2], int m)
{
double sum = 0;
for (int i = 0; i < m; i++)
sum += t[i][1];
return (sum / m);
}
Comments
Leave a comment