question archive Declare a seven-row, Unto-column int array named temperatures
Subject:Computer SciencePrice:4.86 Bought12
Declare a seven-row, Unto-column int array named temperatures. The program should prompt the user to enter the highest and lowest temperatures for seven days. Store the highest temperatures in the ?rst column in an array. Store the lowest temperatures in the second column. The program should display the average high temperature and the average low temperature. Display the average temperatures with one decimal place. Sample Screen Output Day Highest Lowest 1 95 B? 2 98 54 3 36 T0 4 99 56 5 33 34 6 7'5 68 7 BD 45 Average high temperature: __ Average low temperature:
#include <iostream> #include <iomanip> using namespace std; int main() { // create arrays int temperatures[7][2]; float averages[2] = {0.0, 0.0}; // enter temperatures for (int i = 0; i < 7; i++) { // enter temperatures cout << "Enter Highest And Lowest Temperature For Day " << (i+1) << ": "; cin >> temperatures[i][0] >> temperatures[i][1]; // store for averages averages[0] += temperatures[i][0]; averages[1] += temperatures[i][1]; } // calculate averages averages[0] /= 7; averages[1] /= 7; // display results cout << endl << string(40, '=') << endl; cout << setw(10) << "Day"; cout << setw(10) << "Highest"; cout << setw(10) << "Lowest"; cout << endl << string(40, '=') << endl; for (int i = 0; i < 7; i++) { cout << setw(10) << (i+1); cout << setw(10) << temperatures[i][0]; cout << setw(10) << temperatures[i][1] << endl; } cout << "Average high temperature: " << fixed << setprecision(1) << averages[0] << endl; cout << "Average low temperature : " << fixed << setprecision(1) << averages[1] << endl; }
Please see attached screenshot and explanation.
Step-by-step explanation
The above program performs the following tasks:
Here is a sample input and output display of the above program:
Here is a breakdown of the code:
First, include the needed libraries:
#include <iostream> #include <iomanip> using namespace std;
Here is the start of the main function:
int main() {
Create arrays for storage as needed:
// create arrays int temperatures[7][2]; float averages[2] = {0.0, 0.0};
Enter temperatures:
// enter temperatures for (int i = 0; i < 7; i++) { // enter temperatures cout << "Enter Highest And Lowest Temperature For Day " << (i+1) << ": "; cin >> temperatures[i][0] >> temperatures[i][1]; // store for averages averages[0] += temperatures[i][0]; averages[1] += temperatures[i][1]; }
Calculate the averages:
// calculate averages averages[0] /= 7; averages[1] /= 7;
Display the formatted results:
// display results cout << endl << string(40, '=') << endl; cout << setw(10) << "Day"; cout << setw(10) << "Highest"; cout << setw(10) << "Lowest"; cout << endl << string(40, '=') << endl; for (int i = 0; i < 7; i++) { cout << setw(10) << (i+1); cout << setw(10) << temperatures[i][0]; cout << setw(10) << temperatures[i][1] << endl; } cout << "Average high temperature: " << fixed << setprecision(1) << averages[0] << endl; cout << "Average low temperature : " << fixed << setprecision(1) << averages[1] << endl; }
The above program is already working as expected.
Inline comments are included in the code for your reference.
Please see the attached file for the complete solution