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: 3852432 • 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], if stream &) - 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 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 the 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

#include<iostream>
#include<fstream>
#include<iomanip>
#include <cstdlib>


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


infile.open("D:\Data1.txt"); //open infile
outfile.open("D:\Data1_Output.txt");

outfile<<fixed<<showpoint;   //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];
}
}
}
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
}

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