Write a C++ program that does the following: Creates an array of int called temp
ID: 3699158 • Letter: W
Question
Write a C++ program that does the following:
Creates an array of int called temperatures with 7 elements.
Initialize the array with the following values:
30, 31, 60, 65, 75, 71, 112
Create a void function called calculateTemps which takes in the array of temperatures. It will also take in three variables of type double which are passed by reference and are named highest, avg, and numTempsFreezing. In the body, this function will calculate the three values based on the array
- The highest temperature
- The average temperature
- The number of temperatures at or below 32 (freezing in Fahrenheit)
And these values will be assigned to the corresponding variables.
From main, call the function calculateTemps and display the three values which are the highest and average temperatures and also the number of temperatures below freezing of the provided array as determined by the function.
Explanation / Answer
#include <iostream>
using namespace std;
void calculateTemps (int t[], int &max,int &avg, int &n) {
max = t[0];
int sum = 0;
for(int i=0;i<7;i++) {
if(max<t[i]) {
max = t[i];
}
if(t[i]<=32) {
n++;
}
sum+=t[i];
}
avg = sum/7.0;
}
int main()
{
int temp[7]={30, 31, 60, 65, 75, 71, 112};
int n = 0, max =0, avg =0;
calculateTemps (temp, max, avg, n);
cout<<"The highest temperature: "<<max<<endl;
cout<<"The average temperature: "<<avg<<endl;
cout<<"The number of temperatures at or below 32 (freezing in Fahrenheit): "<<n<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.