In: Computer Science
In C++ using a single dimensional array
Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array, a for loop will be used to pull out the high and low temps and total them. After all temps have been totaled, the average high and low will be calculated. The totals and averages will be placed into the array. The output will display the following;
The total high temps is "total high"
The total low temps is "total low"
The average high temps is "average high"
The average low temps is "average low"
Code is Given Below:
===========================
#include <iostream>
using namespace std;
int main()
{
//declaring array to hold day, low Temperature and high
Temperature
float temperature[21];
int day=1;
//asking user to enter data of 7 days
for(int i=0;i<21;i+=3){
temperature[i]=day;
cout<<"Enter High Temperature of Day "<<day<<":
";
cin>>temperature[i+1];
cout<<"Enter Low Temperature of Day "<<day<<":
";
cin>>temperature[i+2];
day++;
}
//finding highToatal and lowToatl
double highTotal,lowTotal;
highTotal=0;
lowTotal=0;
for(int i=0;i<21;i+=3){
highTotal=highTotal+temperature[i+1];
lowTotal=lowTotal+temperature[i+2];
}
//finding average
double averageHigh;
double averageLow;
averageHigh=highTotal/7;
averageLow=lowTotal/7;
//displaying result
cout<<"The total high temps is
"<<highTotal<<endl;
cout<<"The total low temps is
"<<lowTotal<<endl;
cout<<"The average high temps is
"<<averageHigh<<endl;
cout<<"The average low temps is
"<<averageLow<<endl;
return 0;
}
Output:
===========
Code Snapshot:
==============