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

[C++] My code requires that I show both the outputs: Highest temperature recorde

ID: 3853291 • Letter: #

Question

[C++]

My code requires that I show both the outputs:

Highest temperature recorded in the month of (Month goes here)

Lowest temperature recorded in the month of (Month goes here)

can anyone edit my code, and add this piece of code so that the months show as well????

Also, why can't my code read the outputfile with decimal answers???

#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<<"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< //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;
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
}
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.0;
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
}

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>
#include<cstring>


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
char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
ifstream infile;
ofstream outfile;
int main() {
   int listTemp[rows][columns]; //2-D array
   // double listTemp[rows][columns]; //this will allow you to read fractional data aswell
   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< //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;
   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
}

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.0;
   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;
   string month;
   for(int x = 0; x < rows; x++) //calculates highest in the high column
   {
       if(listTemp[0][x] > highestIndex) {
           highestIndex = listTemp[0][x];
           month = months[x];
       }
   }
   outfile << "The index high temperature is " << highestIndex << endl; //outputs data to Data1_Output.txt
   cout << "Highest temperature recorded in the month of " << month <<endl;
}

void indexLowTemp ( int listTemp[rows][columns], ofstream& outfile) { //find the lowest in the low column
   int lowestIndex = 0;
   string month;
   for(int x = 0; x < rows; x++) { //calculates lowest in the low column
       if(lowestIndex > listTemp[0][x]) {
           lowestIndex = listTemp[0][x];
           month = months[x];
       }
   }
   outfile << "The index low temperature is " << lowestIndex << endl; //outputs data to Data1_Output.txt
   cout << "lowest temperature recorded in the month of " << month <<endl;
}

Changes have been highlighted. I hope you like the answer

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