I am having issues with this C++ program. any help i have been able to find was
ID: 3678968 • Letter: I
Question
I am having issues with this C++ program. any help i have been able to find was using files. We are not using files. I am having issues with the getData function as well as the average functions. I am able to get all the input info to show up but not using the getData function. I also can get the indexes as well. Please help as I know it is simple but for some reason I am stumped on it. Assignment is below. 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 must consist of the following functions: a. Function getData: This function reads and stores data in the two-dimensional array. b. Function averageHigh: This function calculates and returns the average high temperature for the year. c. Function averageLow: This function calculates and returns the average low temperature for the year. d. Function indexHighTemp: This function returns the index of the highest high temperature in the array. e. Function indexLowTemp: This function returns the index of the lowest low temperature in the array. (These functions must all have the appropriate parameters.)
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes.
void fixInputStream();
void getData(double dataArray[12][2]) ;
double averageHigh(double dataArray[12][2]);
double averageLow(double dataArray[12][2]);
int indexHighTemp(double dataArray[12][2]);
int indexLowTemp(double dataArray[12][2]);
void printData(double dataArray[12][2]);
int main() {
// The array to store the highest and lowest temperatures for each month of the year.
double temperatures[12][2] = {}; // Initialize all elements to 0.
getData(temperatures);
printData(temperatures);
return 0;
}
/**
* Restores the input stream.
*/
void fixInputStream() {
cin.clear(); // Restore input stream
cin.ignore(100, ' '); // Clear the buffer
}
/**
* Reads and stores data in the two-dimensional array.
* @param dataArray An double array for us to write data to.
*/
void getData(double dataArray[12][2]) {
// Print explanation.
cout << "Enter the highest and lowest temperatures for each month of the year:" << endl;
// For each month...
for (int month = 0; month < 12; month += 1) {
// Print the month number.
cout << "Month " << setw(2) << month + 1 << endl;
// Get the highest.
cout << " highest temperature: ";
cin >> dataArray[ month ][ 0 ];
fixInputStream();
// Get the lowest.
cout << " lowest temperature: ";
cin >> dataArray[ month ][ 1 ];
fixInputStream();
cout << endl; // Insert a blank line.
}
}
/**
* Calculates and returns the average high temperature for the year.
* @param dataArray An double array.
* @return
*/
double averageHigh(double dataArray[12][2]) {
double sum = 0.0;
// For each month...
for (int month = 0; month < 12; month += 1) {
// Add the high temperature to the sum.
sum += dataArray[ month ][ 0 ];
}
return sum / 12;
}
/**
* Calculates and returns the average low temperature for the year.
* @param dataArray An double array.
* @return
*/
double averageLow(double dataArray[12][2]) {
double sum = 0.0;
// For each month...
for (int month = 0; month < 12; month += 1) {
// Add the high temperature to the sum.
sum += dataArray[ month ][ 1 ];
}
return sum / 12;
}
/**
* @return the index of the highest high temperature in the array.
*/
int indexHighTemp(double dataArray[12][2]) {
int highestIndex = 0;
// For each month...
for (int month = 1; month < 12; month += 1) {
if (dataArray[ month ][ 0 ] > highestIndex) {
highestIndex = dataArray[ month ][ 0 ];
}
}
return highestIndex;
}
/**
* @return the index of the lowest temperature in the array.
*/
int indexLowTemp(double dataArray[12][2]) {
int lowestIndex = 0;
// For each month...
for (int month = 1; month < 12; month += 1) {
if (dataArray[ month ][ 1 ] > lowestIndex) {
lowestIndex = dataArray[ month ][ 1 ];
}
}
return lowestIndex;
}
/**
* Prints all the the highest and lowest temperature values in the 2-D array.
*/
void printData(double dataArray[12][2]) {
// For each month...
for (int month = 0; month < 12; month += 1) {
// Print the month number.
cout << "Month " << setw(2) << month + 1 << endl;
// Pritn the highest.
cout << " highest temperature: " << dataArray[ month ][ 0 ] << endl;
cout << " lowest temperature: " << dataArray[ month ][ 1 ] << endl;
cout << endl; // Insert a blank line.
}
cout << endl;
}
sample output
Month 1
highest temperature: 102
lowest temperature: 45
Month 2
highest temperature: 103
lowest temperature: 40
Month 3
highest temperature: 105
lowest temperature: 40
Month 4
highest temperature: 106
lowest temperature: 55
Month 5
highest temperature: 107
lowest temperature: 45
Month 6
highest temperature: 108
lowest temperature: 55
Month 7
highest temperature: 109
lowest temperature: 56
Month 8
highest temperature: 110
lowest temperature: 59
Month 10
highest temperature: 131
lowest temperature: 35
Month 11
highest temperature: 141
lowest temperature: 36
Month 12
highest temperature: 145
lowest temperature: 32
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.