Write a C program that uses a two dimensional array to store

Write a C++ program that uses a two dimensional array to store the highest and lowest temperature for each month of the year. The program should output the average high, average low, highest and lowest temperature of the year. Your program must consist of the following functions: Function getData: this function reads and store date in the two-dimensional array. Function averageHigh: this function calculates and returns the average high temperature of the year. Function averageLow: this function calculates and returns the average low temperature of the year. Function HighTemp: this function returns the highest high temperature in the array. Function LowTemp: this function returns the lowest low temperature in the array. These functions must all have the appropriate parameters (No global variables). Turn in a printout of your source code and upload a copy of your source code to Sakai (Assignment Program-4). Name your source file prog4.cpp.

Solution

#include<iostream>
using namespace std;
//Accepts the high and low temperature for given month as r
void getData(int *data[], int r)
{
cout<<\"\ Enter data \ \";
for(int i = 0; i < r; i++)
{
for(int j = 0; j < 2; j++)
cin>>*(data[i] + j);
}
}
//Display the high and low temperature for given month as r
void showData(int *data[], int r)
{
cout<<\"\ Show data \ \";
for(int i = 0; i < r; i++)
{
for(int j = 0; j < 2; j++)
cout<<*(data[i] + j)<<\" \";
cout<<endl;
}
}
//Calculates the highest average temperature for given month as r
float averageHeigh(int *data[], int r)
{
float tot = 0, avg;
for(int i = 0; i < r; i++)
{
//Calculates total temperature for high
tot += *(data[i] + 0);
}
//Calculates average temperature for high
avg = tot / r;
//returns high temperature average
return avg;
}
//Calculates the lowest average temperature for given month as r
float averageLow(int *data[], int r)
{
float tot = 0, avg;
for(int i = 0; i < r; i++)
{
//Calculates total temperature for low
tot += *(data[i] + 1);
}
//Calculates average temperature for low
avg = tot / r;
//returns low temperature average
return avg;
}
//Returns the high temperature for given months as r
int HighTemp(int *data[], int r)
{
//Assumes the first temperature as high
int h = *(data[0] + 0);
for(int i = 0; i < r; i++)
{
//Checks the existing temperature is small than the old high temperature then change the high temperature
if(h < *(data[i] + 0))
h = *(data[i] + 0);
}
//Returns the high temperature
return h;
}
//Returns the low temperature for given months as r
int LowTemp(int *data[], int r)
{
//Assumes the first temperature as low
int l = *(data[0] + 0);
for(int i = 0; i < r; i++)
{
//Checks the existing temperature is greater than the old low temperature then change the low temperature
if(l > *(data[i] + 1))
l = *(data[i] + 1);
}
return l;
}
int main()
{
int r;
cout<<\"\ Enter How many Months data you want to input: \";
cin>>r;
//Dynamically creates 2D array as per the specified size
int** arr = new int*[r];
for(int i = 0; i < r; ++i)
arr[i] = new int[2];
getData(arr, r);
showData(arr, r);
cout<<\"\ Average High Temperature of the Year: \"<<averageHeigh(arr, r);
cout<<\"\ Average Low Temperature of the Year: \"<<averageLow(arr, r);
cout<<\"\ High Temperature of the Year: \"<<HighTemp(arr, r);
cout<<\"\ Low Temperature of the Year: \"<<LowTemp(arr, r);
}

Output:

Enter How many Months data you want to input: 5

Enter data
10
11
45
55
22
65
88
12
55
23

Show data
10 11
45 55
22 65
88 12
55 23

Average High Temperature of the Year: 44
Average Low Temperature of the Year: 33.2
High Temperature of the Year: 88
Low Temperature of the Year: 11

 Write a C++ program that uses a two dimensional array to store the highest and lowest temperature for each month of the year. The program should output the ave
 Write a C++ program that uses a two dimensional array to store the highest and lowest temperature for each month of the year. The program should output the ave
 Write a C++ program that uses a two dimensional array to store the highest and lowest temperature for each month of the year. The program should output the ave

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site