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...

  • 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...

  • 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...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • can someone please comment through this code to explain me specifically how the variables and arrays...

    can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays code is below assignment C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed. Programming Exercise 12 on page 607 Lab9_data.txt Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the...

  • How can I modify my program to return the option with the highest amount ? //A...

    How can I modify my program to return the option with the highest amount ? //A new author is in the process of negotiating a contract for a new romance novel. //The publisher is offering three options. //1. The author is paid $5,000 upon delivery of the final manuscript and $20,000 when the novel is published. //2. In the second option, the author is paid 12.5% of the net price of the novel for each copy of the novel sold....

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
ADVERTISEMENT