Answer to Question #179757 in C++ for John

Question #179757

Create 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 highestavg, and numTempsFreezing.  In the body, this function will calculate the three values based on the array

1)    The highest temperature

2)    The average temperature

3)    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.


1
Expert's answer
2021-04-09T12:28:28-0400
#include <iostream>
void calculateTemps(int arr[7] ,double &high, double &avg,double &freeze)
{
     high = arr[0];
     avg = 0;
     freeze = 0;
    for (int i = 0; i < 7;i++)
    {
        avg += arr[i];
        if (arr[i] > high) 
        {
            high = arr[i];
        }
        if (arr[i] < 32) {
            freeze++;
        }
        avg /= 7;
    }
}
int main()
{
    int temperatures[7] = { 30, 31, 60, 65, 75, 71, 112 };
    double  highest ;
    double avg ;
    double numTempsFreezing ;
    calculateTemps(temperatures,highest,avg,numTempsFreezing);
    std::cout << "Highest " << highest << std::endl;
    std::cout << "Average " << avg << std::endl;
    std::cout<<"The number of temperatures at or below 32: "<< numTempsFreezing<< std::endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS