Question

C++: modify the following code so the names of the month are listed to the left...

C++:

modify the following code so the names of the month are listed to the left of the high and low temperature readings and label the two columns of numbers as high (the one on the left) and low (the one on the right). Numbers are already in order so all that is needed is the months' names in order from top (jan.) to bottom (dec.)

code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int NO_OF_MONTHS = 12;

void getData(int twoDim[NO_OF_MONTHS][2], int rows);
int averageHigh(int twoDim[][2], int rows);
int averageLow(int twoDim[][2], int rows);
int indexHighTemp(int twoDim[][2], int rows);
int indexLowTemp(int twoDim[][2], int rows);

void displayMatrix(int twoDim[][2], int);

ifstream inFile("weather.txt");
ofstream outFile("weather.out");


int main()
{

int hiLowArray[NO_OF_MONTHS][2];

int indexHigh;
int indexLow;

getData(hiLowArray, NO_OF_MONTHS);

displayMatrix(hiLowArray,NO_OF_MONTHS);

cout << "\nAverage high temperature: "
<< averageHigh(hiLowArray, NO_OF_MONTHS) << endl;
outFile << "\nAverage high temperature: "
<< averageHigh(hiLowArray, NO_OF_MONTHS) << endl;

cout << "\nAverage low temperature: "
<< averageLow(hiLowArray, NO_OF_MONTHS) << endl;
outFile << "\nAverage low temperature: "
<< averageLow(hiLowArray, NO_OF_MONTHS) << endl;

indexHigh = indexHighTemp(hiLowArray, NO_OF_MONTHS);
cout << "\nHighest temperature: " << indexHigh << endl;
outFile << "\nHighest temperature: " << indexHigh << endl;

indexLow = indexLowTemp(hiLowArray, NO_OF_MONTHS);
cout << "\nLowest temperature: " << indexLow << endl;
outFile << "\nLowest temperature: " << indexLow << endl;

return 0;
}

/***********************************************************************/
void getData(int twoDim[NO_OF_MONTHS][2], int rows){
for (int row =0;row<12;row++ )
for(int column=0;column<2; column++)
{
inFile>>twoDim[row][column];
}
}
/***********************************************************************/
void displayMatrix (int twoDim[NO_OF_MONTHS][2], int rows )
{
for (int row =0;row<12;row++ )
{
for(int column=0;column<2; column++)
{
cout<<setw(5)<<twoDim[row][column] ;
}
cout<<endl;
}
}
/***********************************************************************/
int averageHigh(int twoDim[NO_OF_MONTHS][2], int rows )
{
int highAverage = 0;
for (int row =0;row<12;row++ )
{
for(int column=0;column<1; column++)
{
highAverage = twoDim[row][column] + highAverage;
}
}
highAverage = highAverage/NO_OF_MONTHS;
return highAverage;
}
/***********************************************************************/
int averageLow(int twoDim[NO_OF_MONTHS][2], int rows )
{
int lowAverage = 0;
for (int row =0;row<12;row++ )
{
for(int column=1;column<2; column++)
{
lowAverage = twoDim[row][column] + lowAverage;
}
}
lowAverage = lowAverage/NO_OF_MONTHS;
return lowAverage;
}
/*************************************************************************************/
int indexHighTemp(int twoDim[][2], int rows){
int indexHigh = 0;
for (int row =0;row<12;row++ )
{
for(int column=0;column<1; column++)
{
if(indexHigh<twoDim[row][column])
indexHigh = twoDim[row][column];
}
}
return indexHigh;
}
/************************************************************************************/
int indexLowTemp(int twoDim[][2], int rows){
int indexLow = twoDim[0][0];
for (int row =0;row<12;row++ )
{
for(int column=1;column<2; column++)
{
if(indexLow>twoDim[row][column])
indexLow = twoDim[row][column] ;
}
}
return indexLow;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

const int NO_OF_MONTHS = 12;

const string MONTHS[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

void getData(int twoDim[NO_OF_MONTHS][2], int rows);

int averageHigh(int twoDim[][2], int rows);

int averageLow(int twoDim[][2], int rows);

int indexHighTemp(int twoDim[][2], int rows);

int indexLowTemp(int twoDim[][2], int rows);

void displayMatrix(int twoDim[][2], int);

ifstream inFile("weather.txt");

ofstream outFile("weather.out");

int main()

{

    int hiLowArray[NO_OF_MONTHS][2];

    int indexHigh;

    int indexLow;

    getData(hiLowArray, NO_OF_MONTHS);

    displayMatrix(hiLowArray, NO_OF_MONTHS);

    cout << "\nAverage high temperature: "

         << averageHigh(hiLowArray, NO_OF_MONTHS) << endl;

    outFile << "\nAverage high temperature: "

            << averageHigh(hiLowArray, NO_OF_MONTHS) << endl;

    cout << "\nAverage low temperature: "

         << averageLow(hiLowArray, NO_OF_MONTHS) << endl;

    outFile << "\nAverage low temperature: "

            << averageLow(hiLowArray, NO_OF_MONTHS) << endl;

    indexHigh = indexHighTemp(hiLowArray, NO_OF_MONTHS);

    cout << "\nHighest temperature: " << indexHigh << endl;

    outFile << "\nHighest temperature: " << indexHigh << endl;

    indexLow = indexLowTemp(hiLowArray, NO_OF_MONTHS);

    cout << "\nLowest temperature: " << indexLow << endl;

    outFile << "\nLowest temperature: " << indexLow << endl;

    return 0;

}

/***********************************************************************/

void getData(int twoDim[NO_OF_MONTHS][2], int rows)

{

    for (int row = 0; row < 12; row++)

        for (int column = 0; column < 2; column++)

        {

            inFile >> twoDim[row][column];

        }

}

/***********************************************************************/

void displayMatrix(int twoDim[NO_OF_MONTHS][2], int rows)

{

    cout<<setw(5)<<"S.No"<<setw(6)<<"Month"<<setw(5)<<"High"<<setw(5)<<"Low"<<endl;

    for (int row = 0; row < 12; row++)

    {

        cout << setw(5) << left << (row + 1) << setw(6) << MONTHS[row];

        for (int column = 0; column < 2; column++)

        {

            cout << setw(5) << right << twoDim[row][column];

        }

        cout << endl;

    }

}

/***********************************************************************/

int averageHigh(int twoDim[NO_OF_MONTHS][2], int rows)

{

    int highAverage = 0;

    for (int row = 0; row < 12; row++)

    {

        for (int column = 0; column < 1; column++)

        {

            highAverage = twoDim[row][column] + highAverage;

        }

    }

    highAverage = highAverage / NO_OF_MONTHS;

    return highAverage;

}

/***********************************************************************/

int averageLow(int twoDim[NO_OF_MONTHS][2], int rows)

{

    int lowAverage = 0;

    for (int row = 0; row < 12; row++)

    {

        for (int column = 1; column < 2; column++)

        {

            lowAverage = twoDim[row][column] + lowAverage;

        }

    }

    lowAverage = lowAverage / NO_OF_MONTHS;

    return lowAverage;

}

/*************************************************************************************/

int indexHighTemp(int twoDim[][2], int rows)

{

    int indexHigh = 0;

    for (int row = 0; row < 12; row++)

    {

        for (int column = 0; column < 1; column++)

        {

            if (indexHigh < twoDim[row][column])

                indexHigh = twoDim[row][column];

        }

    }

    return indexHigh;

}

/************************************************************************************/

int indexLowTemp(int twoDim[][2], int rows)

{

    int indexLow = twoDim[0][0];

    for (int row = 0; row < 12; row++)

    {

        for (int column = 1; column < 2; column++)

        {

            if (indexLow > twoDim[row][column])

                indexLow = twoDim[row][column];

        }

    }

    return indexLow;

}

Add a comment
Know the answer?
Add Answer to:
C++: modify the following code so the names of the month are listed to the left...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this asap. C++ please A A Ap Aa Consolas 14 AaBbCcDd AaBbCcDd AaBbCc Editing...

    I need this asap. C++ please A A Ap Aa Consolas 14 AaBbCcDd AaBbCcDd AaBbCc Editing ste 1 No Spac.. Heading 1 В I 1 Normal x A U A v ab х. Dictate ipboard Font Paragraph Styles Voice Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • Can I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • I am trying to modify this program so that it stores the employee's name in a...

    I am trying to modify this program so that it stores the employee's name in a c-string and can handle up to 100 employees (so it would mostly be a partially filled array), but I cannot get it to work. It works with two files that it is suppose to read input from. Then it prints output to an output file. Employee Info File (first file read, M/F should be ignored): 5    Christine Kim 30.00   2       1    F 15    Ray...

  • CODE ERROR Please help to fix the error. I don't know why I can not output...

    CODE ERROR Please help to fix the error. I don't know why I can not output to file. when I open the output file there is an error. here is the input data: Princeton University NJ Princeton 41820    8014   0.0740   0.98   0.97 Harvard University MA Cambridge 43838    19882   0.0580   0.97   0.97 Yale University CT New Haven 45800    12109   0.0690   0.99   0.98 Columbia University NY New York           51008    23606   0.0690   0.99   0.96 Stanford University CA...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications...

    C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications Included is an input file. Included is a function to aide, if needed Use the functions, enum, and namespace defined in Assignment 6. You will need to use a loop to read each student’s data You will need to load every homework grade into an array Calculate the homework average The homework average can be calculated while the array is loaded Move the printing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT