Here is my code, //intermediate22.cpp #include<iostream> #include<iomanip> using
ID: 3809922 • Letter: H
Question
Here is my code,
//intermediate22.cpp
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//declaration of array
int temperatures[7][2] = { 0 };
//declare the local variables
double avgHigh = 0.0;
double avgLow = 0.0;
int total = 0;
int high = 0;
int low = 0;
//receive input from user
cout << "Enter highest and lowest temperatures for 7 days" << endl;
for (int row = 0; row < 7; row += 1)
{
for (int col = 0; col < 2; col += 1)
{
if (col == 0)
{
cout << "Day" << row + 1
<< "high temp:";
cin >> temperatures[row][col];
}
else
{
cout << "Day" << row + 1
<< "low temp:";
cin >> temperatures[row][col];
}//end if
}//end for
cout << endl;
}//end for
//calculate and display the average of high //temperature
//and average of lowest temperature along with highest
//temperature and lowest temperature
high = temperatures[0][0];
low = temperatures[0][1];
for (int col = 0; col < 2; col += 1)
{
for (int row = 0; row < 7; row += 1)
{
total += temperatures[row][col];
if (col == 0)
{
if (temperatures[row][col] > high)
high = temperatures[row][col];
}
else
{
if (temperatures[row][col] < low)
low = temperatures[row][col];
}//end if
}//end for
if (col == 0)
{
avgHigh = total / 7.0;
cout << fixed << setprecision(1);
cout << "Average of the Highest temperature is:" << avgHigh << endl;
cout << "Highest temperature is" << high <
}
else
{
avgLow = total / 7.0;
cout << fixed << setprecision(1);
cout << "Average of the lowest temperature is:" <
cout << "Lowest temperature is" << low << endl;
}// end if
total = 0;
}//end for
system("pause");
return 0;
} //end of main function
Explanation / Answer
Hi
I have fixed the issue and highlighted the code changes below
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//declaration of array
int temperatures[7][2] = { 0 };
//declare the local variables
double avgHigh = 0.0;
double avgLow = 0.0;
int total = 0;
int high = 0;
int low = 0;
//receive input from user
cout << "Enter highest and lowest temperatures for 7 days" << endl;
for (int row = 0; row < 7; row += 1)
{
for (int col = 0; col < 2; col += 1)
{
if (col == 0)
{
cout << "Day" << row + 1
<< "high temp:";
cin >> temperatures[row][col];
}
else
{
cout << "Day" << row + 1
<< "low temp:";
cin >> temperatures[row][col];
}//end if
}//end for
cout << endl;
}//end for
//calculate and display the average of high //temperature
//and average of lowest temperature along with highest
//temperature and lowest temperature
high = temperatures[0][0];
low = temperatures[0][1];
for (int col = 0; col < 2; col += 1)
{
for (int row = 0; row < 7; row += 1)
{
total += temperatures[row][col];
if (col == 0)
{
if (temperatures[row][col] > high)
high = temperatures[row][col];
}
else
{
if (temperatures[row][col] < low)
low = temperatures[row][col];
}//end if
}//end for
if (col == 0)
{
avgHigh = total / 7.0;
cout << fixed << setprecision(1);
cout << "Average of the Highest temperature is:" << avgHigh << endl;
cout << "Highest temperature is" << high <<endl;
}
else
{
avgLow = total / 7.0;
cout << fixed << setprecision(1);
cout << "Average of the lowest temperature is:" <
cout << "Lowest temperature is" << low << endl;
}// end if
total = 0;
}//end for
// system("pause");
return 0;
} //end of main function
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter highest and lowest temperatures for 7 days
Day1high temp:7
Day1low temp:1
Day2high temp:23
Day2low temp:21
Day3high temp:34
Day3low temp:33
Day4high temp:44
Day4low temp:41
Day5high temp:22
Day5low temp:21
Day6high temp:77
Day6low temp:66
Day7high temp:44
Day7low temp:22
Average of the Highest temperature is:35.9
Highest temperature is 77
Average of the lowest temperature is:Lowest temperature is 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.