Write a C program that uses a two dimensional array to store
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


