Question: How can I make this code look like the assignment requirements? I need the program to prompt the user for an input file and output
How can I make this code look like the assignment requirements? I need the program to prompt the user for an input file and output file name.
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::
~ If the input file was not found, report the error and terminate the program. Otherwise call the getData and pass the appropriate parameters, so that it will pass the appropriate parameters and return the array with all the necessary data.
~Call the other function to get the average high and low, and also the index with the high temperature etc.
~Output that to the output file.
#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
infile.open("data1.txt"); //open infile
outfile.open("data1_output.txt");
//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
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 wll create an array with 12 rows (one for each month of the year) and 2 columns where the first colum 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 maximurn 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 iput 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 yul 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 {
if(lowestIndex > listTemp[0][x])
lowestIndex = listTemp[0][x];
}
outfile
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
