C++ Two- Dimensional Arrays: If necessary, create a new project named Intermedia
ID: 3721414 • Letter: C
Question
C++ Two- Dimensional Arrays: If necessary, create a new project named Intermediate22 Project and save it to the CppChap12 folder. Also create a new source file named Intermediate.cpp. Declare a seven-row, two-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 first column of the 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. Save and then run the program. I found the answer on here, and it only asks for the seven day temps but I need to create code without using void functions help please, plus the code already provided for this question does not display the average high and low temps! please help!
Explanation / Answer
C++ program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i=0;
//2D std::array<T, N> ;
int temperatures[7][2];
int highestTempValue,lowestTempValue;
//loop to read values
for(i=0;i<7;i++)
{
cout<<"Enter highest temperature for day "<<(i+1)<<":";
cin>>highestTempValue;
temperatures[i][0]=highestTempValue;
cout<<"Enter lowest temperature for day "<<(i+1)<<":";
cin>>lowestTempValue;
temperatures[0][i]=lowestTempValue;
}
int hightTempSum=0,lowestTempSum=0;
double avgLowestTemp=0.0,avgHighestTemp=0.0;
//calcualting sum of temperatures
for(i=0;i<7;i++)
{
hightTempSum=hightTempSum+temperatures[i][0];
lowestTempSum=lowestTempSum+temperatures[0][i];
}
//calcualting averages
avgHighestTemp=(double)hightTempSum/7;
avgLowestTemp=(double)lowestTempSum/7;
//displaying outputs
cout<<" Average Highest Temperature: ";
cout << setprecision (1) << fixed << avgHighestTemp;
cout<<" Average Lowest Temperature: ";
cout << setprecision (1) << fixed << avgLowestTemp;
return 0;
}
Output:
Enter highest temperature for day 1:35
Enter lowest temperature for day 1:23
Enter highest temperature for day 2:40
Enter lowest temperature for day 2:25
Enter highest temperature for day 3:38
Enter lowest temperature for day 3:21
Enter highest temperature for day 4:36
Enter lowest temperature for day 4:24
Enter highest temperature for day 5:39
Enter lowest temperature for day 5:26
Enter highest temperature for day 6:41
Enter lowest temperature for day 6:25
Enter highest temperature for day 7:40
Enter lowest temperature for day 7:26
Average Highest Temperature: 30.9
Average Lowest Temperature: 24.3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.