(3) About two dimensional arrays. Modified from #9 in page 586-587 (6th edition)
ID: 641987 • Letter: #
Question
(3) About two dimensional arrays. Modified from #9 in page 586-587 (6th edition) rray called hiLowTemperatures to store the highest and lowest temperatures for Write a program that uses a two-dimensional a each month of the year. The array will have the following declaration, where the rows represent the number of months: first column is to store the highest temperature of the month and the second column is to store the lowest temperature of the month. int hiLowTemperatures [12] [21 The program should read the highest and lowest temperatures for each month of the year from the text file and print the highest temperature and the lowest temperature of the year to the screen. For instance, if the file has the following values as the highest temperatures for each month 32 40 45 60 65 77 90 100 90 91 65 55 Then,your program should print that 100 is the highest temperature of the year. The text file should have 12 values in first line for highest temperature values and another 12 values for the lowest temperature values in the second line Your program must have of the following functions Function getData: This function reads data from the a text file into the two-dimensional array Function indexHighTemp: This function returns the row index of the highest temperature in the array. Function indexLowTemp : This function returns the row indexf the lowest temperature in the array.Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
void getData(ifstream &in, int highLowTemperatures[][2]){
for(int i = 0; i < 12; ++i){
in >> highLowTemperatures[i][0];
}
for(int i = 0; i < 12; ++i){
in >> highLowTemperatures[i][1];
}
}
int indexHighTemp(int temperatures[][2]){
int val = 0;
for(int i = 1; i < 12; ++i){
if(temperatures[val][0] < temperatures[i][0]){
val = i;
}
}
return val;
}
int indexLowTemp(int temperatures[][2]){
int val = 0;
for(int i = 1; i < 12; ++i){
if(temperatures[val][1] > temperatures[i][1]){
val = i;
}
}
return val;
}
int main(){
ifstream in;
string fName;
cout << "Enter file name: ";
cin >> fName;
in.open(fName.c_str());
int highLowTemperatures[12][2];
if(in.is_open()){
getData(in, highLowTemperatures);
cout << "Highest temperature of the year is " << highLowTemperatures[indexHighTemp(highLowTemperatures)][0] << endl;
cout << "Lowest temperature of the year is " << highLowTemperatures[indexLowTemp(highLowTemperatures)][1] << endl;
}
else{
cout << "Can not open the file" << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.