I am having difficulties with this code. How do I get the code to cout << the hi
ID: 3853245 • Letter: I
Question
I am having difficulties with this code. How do I get the code to cout << the highest and lowest temperatures of the month.
{
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<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include <stdlib.h>
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<<"Enter input file name with full path "
"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<<"Error! input file not found.";
return -1;
}
cout<<"Enter output file name: ";
getline(cin, outputFileName);
outfile.open(outputFileName.c_str());
//set showpoint so result will be able to show decimal points
outfile<<setprecision(2);
//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 < rows ; x++)
{
for( y=0; y < columns; y++) //gathers 2-D array data.
{
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 < rows; x++) //calculates average high temp
{
sum = listTemp[x][0] + sum;
}
avg = sum/x;
outfile << "Average high for the year: " << avg << endl; //outputs data to Data1_Output.txt
}
void averageLow ( int listTemp[rows][columns])
{
int sum = 0;
double avg; //average of the lows
for (int x=0; x < rows; x++) //calculates average low temp
{
sum = listTemp [x][1] + sum;
}
avg = sum/12;
outfile << "Average low for the year: " << avg << endl; //outputs data to Data1_Output.txt
}
void indexHighTemp ( int listTemp[rows][columns], ofstream& outfile) //find highest in the high column
{
int highestIndex = 0;
for(int x = 0; x < rows; x++) //calculates highest in the high column
{
if(listTemp[0][x] > highestIndex)
highestIndex = listTemp[0][x];
}
outfile << "The index high temperature is " << highestIndex << endl; //outputs data to Data1_Output.txt
}
void indexLowTemp ( int listTemp[rows][columns], ofstream& outfile) //find the lowest in the low column
{
int lowestIndex = 0;
for(int x = 0; x < rows; x++) //calculates lowest in the low column
{
if(lowestIndex > listTemp[0][x])
lowestIndex = listTemp[0][x];
}
outfile << "The index low temperature is " << lowestIndex << endl; //outputs data to Data1_Output.txt
}
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 fileExplanation / Answer
The problem is with the selection of datatype. When you divide an integer with another integer, the result would always be integer an will not include the decimal part. To overcome this problem, either of the numerator or the denominator needs to be float/double. In the function averageHigh both sum and x are integers, hence you need to change either of the 2 by a double as shown below.
void averageHigh ( int listTemp[rows][columns])
{
int x=1;
double sum = 0.0;
double avg; //average of the highs
for (x=0; x < rows; x++) //calculates average high temp
{
sum = listTemp[x][0] + sum;
}
avg = sum/x;
outfile << "Average high for the year: " << avg << endl; //outputs data to Data1_Output.txt
}
similarly in averagelow function, you can either change sum to be a double as shown below
void averageLow ( int listTemp[rows][columns])
{
double sum = 0.0;
double avg; //average of the lows
for (int x=0; x < rows; x++) //calculates average low temp
{
sum = listTemp [x][1] + sum;
}
avg = sum/12;
outfile << "Average low for the year: " << avg << endl; //outputs data to Data1_Output.txt
}
or change the line avg = sum/12 with the code mentioned below
avg = sum / 12.0;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.