Write a program that uses a two-dimensional array to store the highest and lowes
ID: 669908 • Letter: W
Question
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for the month reading from a text file.
Data will be input in the text file like this:
31 14
33 19
45 25
56 36
68 45
87 54
82 59
79 58
72 51
60 41
48 36
35 22
Define enumerated type names for use as array indices.
Your code will process the temperature data using the functions as described in the following
Use the following functions
Function getData: this function reads and stores the temperature data in a two-dimensional array.
Function averageHigh: This function calculates and returns the average high temperature for the year.
Function averageLow: This function calculates and returns the average low temperature for the year.
Function indexHighTemp: This function returns the index of the highest high temperature in the array. (The main program will interpret that index to figure out the name of the month).
Function indexLowTemp: This function returns the index of the lowest low temperature in the array. (The main program will interpret that index to figure out the name of the month).
Explanation / Answer
#include<iostream>
#include <fstream>
using namespace std;
int main(){
fstream myfile("data.txt", std::ios_base::in);
int temp[12][2];
int a,b,i;
if (myfile.is_open())
{
i = 0
while ( myfile>>a>>b )
{
temp[i][0] = a;
temp[i][1] = b;
}
myfile.close();
}
//Average high temp
int sum = 0;
for( i = 0 ; i< 12; i++){
sum += temp[i][0];
}
cout<<"Average high: "<<sum/12;
//Average low temp
int sum = 0;
for( i = 0 ; i< 12; i++){
sum += temp[i][1];
}
cout<<"Average low: "<<sum/12;
int index = 0;
int max = temp[0][0];
for( i = 1 ; i< 12; i++){
if(max < temp[i][0]){
max = temp[i][0];
index = i;
}
cout<<"Index :"<<i;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.