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

In C++ Write a program that uses a two-dimensional array to store the highest an

ID: 3762397 • Letter: I

Question

In C++ Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year for a city of your choice. You, as the programmer, must create an input text file that contains the name of the city as well as the high/low temperature data for each month for that city. You can find this information by googling "average monthly high and low temperatures for 'city of your choice'". Using functions as described below, the program must retrieve and process the data from the file. The main program opens the file, calls the functions, and then outputs the name of the city, the average high, the average low, and the month that has the highest and lowest temperatures for the year, in a user friendly way. Don't forget to close the file before ending the main program. Your program must consist of the following functions:

Function getData: this function reads and stores the city name and 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

Complete Program:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int MONTHS = 12; // number of months in the year
const int TYPES = 2; // high and low temperatures

void getData(string &cityName, double temperatures[MONTHS][TYPES], int MONTHS);
double averageHigh(double temperatures[MONTHS][TYPES], int MONTHS);
double averageLow(double temperatures[MONTHS][TYPES], int MONTHS);
int indexHighTemp(double temperatures[MONTHS][TYPES], int MONTHS);
int indexLowTemp(double temperatures[MONTHS][TYPES], int MONTHS);
string getMonth(int index);

int main()
{
double temperatures[MONTHS][TYPES];
string cityName;
double averageHighTemperature;
double averageLowTemperature;
int indexOfHighestHighTemperature;
int indexOfLowestLowTemperature;

getData(cityName, temperatures, MONTHS);
averageHighTemperature = averageHigh(temperatures, MONTHS);
averageLowTemperature = averageLow(temperatures, MONTHS);
indexOfHighestHighTemperature = indexHighTemp(temperatures, MONTHS);
indexOfLowestLowTemperature = indexLowTemp(temperatures, MONTHS);
  
cout << "Average monthly high and low temperatures for the city: " << cityName << endl;
cout << left << setprecision(1) << fixed;
cout << setw(12) << "Month" << setw(8) << "High" << setw(8) << "Low" << endl;
cout << "-------------------------" << endl;
for(int i = 0; i < MONTHS; i++)
{
  cout << setw(12) << getMonth(i) << setw(8) << temperatures[i][0] << setw(8) << temperatures[i][1] << endl;
}
cout << endl;
cout << "Average High Temperature: " << averageHighTemperature << endl;
cout << "Average Low Temperature : " << averageLowTemperature << endl;
cout << "Month of Highest High Temperature: " << getMonth(indexOfHighestHighTemperature) << endl;
cout << "Month of Lowest Low Temperature : " << getMonth(indexOfLowestLowTemperature) << endl;
cout << endl;

system("pause");
return 0;
}

void getData(string &cityName, double temperatures[MONTHS][TYPES], int MONTHS)
{
ifstream infile;
infile.open("temperatures.txt");

if(!infile)
{
  cout << "Cannot open the input file." << endl;
  system("pause");
  exit(EXIT_FAILURE);
}

int i = 0;
infile >> cityName;
infile >> temperatures[i][0];
while(infile && i < MONTHS)
{  
  infile >> temperatures[i][1];
  i++;

  infile >> temperatures[i][0];
}

infile.close();
}

double averageHigh(double temperatures[MONTHS][TYPES], int MONTHS)
{
double totalHigh = 0;

for(int i = 0; i < MONTHS; i++)
{
  totalHigh = totalHigh + temperatures[i][0];
}

double avgHigh = totalHigh / MONTHS;

return avgHigh;
}

double averageLow(double temperatures[MONTHS][TYPES], int MONTHS)
{
double totalLow = 0;

for(int i = 0; i < MONTHS; i++)
{
  totalLow = totalLow + temperatures[i][1];
}

double avgLow = totalLow / MONTHS;

return avgLow;
}

int indexHighTemp(double temperatures[MONTHS][TYPES], int MONTHS)
{
int index = 0;
double highesHigh = temperatures[index][0];

for(int i = 0; i < MONTHS; i++)
{
  if(temperatures[i][0] > highesHigh)
  {
   highesHigh = temperatures[i][0];
   index = i;
  }
}

return index;
}

int indexLowTemp(double temperatures[MONTHS][TYPES], int MONTHS)
{
int index = 0;
double lowestLow = temperatures[index][1];

for(int i = 0; i < MONTHS; i++)
{
  if(temperatures[i][1] < lowestLow)
  {
   lowestLow = temperatures[i][1];
   index = i;
  }
}

return index;
}

string getMonth(int index)
{
if(index == 0)
  return "January";
else if(index == 1)
  return "February";
else if(index == 2)
  return "March";
else if(index == 3)
  return "April";
else if(index == 4)
  return "May";
else if(index == 5)
  return "June";
else if(index == 6)
  return "July";
else if(index == 7)
  return "August";
else if(index == 8)
  return "September";
else if(index == 9)
  return "October";
else if(index == 10)
  return "November";
else if(index == 11)
  return "December";
else
  return "UNKNOWN";
}

Input file: temperatures.txt


Sample Output: