Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses a two-dimensional array called hiLowTemperatures to st

ID: 642591 • Letter: W

Question

Write a program that uses a two-dimensional array called hiLowTemperatures to store the highest and lowest temperatures for 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][2];

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 index of the lowest temperature in the array.

Before you implement your functions, make sure that you come up with a correct function prototype (that is the return type + name of the function + formal parameter list) per function

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote