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

For this assignment, you will write a program where you read a set of high and l

ID: 3852013 • Letter: F

Question

For this assignment, you will write a program where you read a set of high and low temperatures from a file and perform various statistics on the data. You will create two filestream variables, one for reading and one for writing. You will create an array with 12 rows (one for each month of the year) and 2 columns where the first column will store that month's high temperature and the second column stores that month's low temperature. The data file will have 12 lines and each line will have the month's high temperature and low temperature with a blank space in between them and each line will be terminated with an end of line character. Your program will consist of the following: A named constant that holds 12, for the months of the year. A 12 by 2 array to hold the temperatures (the array type will be an integer) A set of functions void getData(int [] [2], ifstream&)-will read the data from the file and insert them into the array (that's passed as a parameter) double averageHigh(int[] [2])- will return the average of all the high temperatures for the year. double averageLow (int [] [2])-will return the average of all the low temperatures for the year int indexHighTemp (int [] [2])-will return the index (of the array) that contains the maximum high temperature int indexLowTemp (int [] [2])-will return the index (of the array) that contains the minimum low temperature string getMonth (int)-will return the month name for a given index so getMonth(0) will return the string "January" etc. As always create variables with meaningful names and comment all your variables, constants and all of your functions, also format your output numbers to two decimal places Contents of main What your main will do is prompt the user for an input file name and an output file name, if the input file name was not found, report the error and terminate the program. Otherwise call the function getData and pass the appropriate parameters and this function will basically return the array with all necessary data. Then you will call the other functions to get the average high and low, and the index with the high temperature etc. and output that to the output file.

Explanation / Answer

The answer is as follows:

The code is as follows:

#include<iostream.h>

#define MONTHS 12

void getData(int data[][2], ifstream &fin){ //Fills the array with the data in the file

   int high,low;

   for (int i = 0; i<MONTHS; i++){
       if (fin >> high >> low){  
          data[i][0] = high;
          data[i][1] = low;
       }
   }
}

double averageHigh(int data[][2]){ // Calculates the average high tempearture value of all 12 months

   int total;

   for (int i = 0; i<MONTHS; i++){
       total = total + data[i][0];
   }
   return total/12;
}

double averageLow(int data[][2]){ // Calculates the average low tempearture value of all 12 months

   int total;

   for (int i = 0; i<MONTHS; i++){
       total = total + data[i][1];
   }
   return total/12;
}

int indexHighTemp(int data[][2]){ //Return the index of the array having the highest high temperature value

   int index;
   int max; // holding the maximum high temperature value

   max = 0;
  
   for (int i = 0; i<MONTHS; i++){
       if (data[i][0] > max){
          max = data[i][0];
          index = i;
       }
   }
   return index;
}

int indexLowTemp(int data[][2]){ //Return the index of the array having the lowest low temperature value

   int index;
   int min; // holding the minimum low temperature value

   min = data[0][1];
   index = 0;
  
   for (int i = 0; i<MONTHS; i++){
       if (data[i][1] < min){
          min = data[i][1];
          index = i;
       }
   }
   return index;
}

string getMonth(int month){ // Returns the name of the month given the number

    switch (month) {
        case 1 : return "January";
        case 2 : return "February";
        case 3 : return "March";
        case 4 : return "April";
        case 5 : return "May";
        case 6 : return "June";
        case 7 : return "July";
        case 8 : return "August";
        case 9 : return "September";
        case 10 : return "October";
        case 11 : return "Novemeber";
        case 12 : return "December";
    }
}


void main() {

    string infilename, outfilename; // Storing input filename and output filename
    ifstream infile; // for input file
    ofstream outfile; // for output file
    int index; // used to store index of the array having maximum high temperature or minimum low temperature
    int data[MONTHS][2]; // Array containg the high and low temperature data

    cout << "Enter input filename:";
    cin >> infilename;
    cout << "Enter output filename:";
    cin >> outfilename;

    infile.open(infilename);
   
    if (!infile){
       cout << "Error opening input file";
       return;
    }
    else {
       getdata(data, infile);
       infile.close();
       outfile.open(outfilename);
       outfile << "The average of all high temperature values :" << setprecision(2) << averageHigh(data) << endl;
       outfile << "The average of all low temperature values :" << setprecision(2) << averageLow(data) << endl;
       index = indexHighTemp(data);
       outfile << "The index having the maximum high temperature :" << index << " Month name:" << getMonth(index) << endl;
       index = indexLowTemp(data);
       outfile << "The index having the minimum low temperature :" << index << " Month name:" << getMonth(index) << endl;
       outfile.close();
    }
}

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