Question: I am having difficulties with this code. How do I get the code to cout { Highest temperature recorded in the month of: June Lowest
I am having difficulties with this code. How do I get the code to cout
{
Highest temperature recorded in the month of: June
Lowest temperature recorded in the month of: February
}
Also, how do I get the output
"The average high of all the temperatures" and "The average low of all the temperatures" in decimal form. Right now the output is correct, but it is just missing the decimal part.
#include
#include
#include
#include
#include
const int rows = 12; //to make the rows constant
const int columns = 2; //to make the columns constant
using namespace std;
void getData (int listTemp[rows][columns], ifstream& infile); //reads and stores data into the two dimensional array
void averageHigh ( int listTemp[rows][columns]); //calculates and returns the average high temperature for the year
void averageLow (int listTemp[rows][columns]); //calculates and returns the average low temperature for the year
void indexHighTemp (int listTemp[rows][columns], ofstream& outfile);//returns the index of the highest high temperature in the array
void indexLowTemp (int listTemp[columns][columns], ofstream& outfile); //returns the index of the lowest low temperature in the array
ifstream infile;
ofstream outfile;
int main()
{
int listTemp[rows][columns]; //2-D array
string inputFileName;
string outputFileName;
cout
"or file must be in current workspace folder to avoid inputting the full path: ";
getline(cin, inputFileName);
infile.open(inputFileName.c_str()); //open infile
if(!infile) // checking whether the input file exist or not
{
cout
return -1;
}
cout
getline(cin, outputFileName);
outfile.open(outputFileName.c_str());
//set showpoint so result will be able to show decimal points
outfile
//calls functions
getData(listTemp, infile);
averageHigh(listTemp);
averageLow(listTemp);
indexHighTemp(listTemp, outfile);
indexLowTemp(listTemp, outfile);
infile.close();
outfile.close();
return 0;
}
void getData(int listTemp[rows][columns], ifstream& infile)
{
int x;
int y;
for (x=0; x
{
for( y=0; y
{
infile >> listTemp[x][y]>>ws;
}
}
}
void averageHigh ( int listTemp[rows][columns])
{
int x=1;
int sum = 0;
double avg; //average of the highs
for (x=0; x
{
sum = listTemp[x][0] + sum;
}
avg = sum/x;
outfile
}
void averageLow ( int listTemp[rows][columns])
{
int sum = 0;
double avg; //average of the lows
for (int x=0; x
{
sum = listTemp [x][1] + sum;
}
avg = sum/12;
outfile
}
void indexHighTemp ( int listTemp[rows][columns], ofstream& outfile) //find highest in the high column
{
int highestIndex = 0;
for(int x = 0; x
{
if(listTemp[0][x] > highestIndex)
highestIndex = listTemp[0][x];
}
outfile
}
void indexLowTemp ( int listTemp[rows][columns], ofstream& outfile) //find the lowest in the low column
{
int lowestIndex = 0;
for(int x = 0; x
{
if(lowestIndex > listTemp[0][x])
lowestIndex = listTemp[0][x];
}
outfile
}
Description 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 e wi be terminated with an end of line ter. 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) [2]) w [2]) will return the average of all the low temperatures for the year double averageHigh(int return the average of all the high temperatures for the year . double averageLow(int . int indexHighTemp(int[] [2])-will return the index (of the array) that contains the maximum high . int indexLowTemp(int[] [2]) -will return the index (of the array) that contains the minimum low string getMonth(int) wil return the month name for a given index so getMonth (0) will return As always create variables with meaningful names and comment all your variables, constants, temperature temperature the string "January" etc 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 w basically return the array with all the necessary data. Then you wll cl the other functions to get the average high and low, and the ndex with the high temperature etc and output that to the output file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
