Answer:
#include <iostream>
using namespace std;
// average function
void average(double arr[], int n) {
double total = 0;
double avgTemp;
for (int i = 0; i < n; i++)
{
// find temperatures total
total = total + arr[i];
}
// find average
avgTemp = total / n;
cout << "\nAverage Temperature = " << avgTemp << endl;
}
// print temps[] array
void printArray(double arr[], int n) {
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}
int main() {
int const k = 5;
double temps[k];
// temperature value
double tempValue;
// Enter 5 temperatures of your choice
for (int i = 0; i < k; i++)
{
cout << "Enter Temperature value " << i+1 << ": ";
cin >> tempValue;
temps[i] = tempValue;
}
// Function call to print temps[] array
printArray(temps, k);
// Function call to print average of given teperatures
average(temps, k);
return 0;
}
Explanation:
• Inside main(), int constant k is created to set the maximum size of array temps[].
• tempValue of type double takes value at a given index of our temps[] array.
• the for loop inside main() is used to get tempValue from the user and store those values in our array temps[].
• after that printArray() function is called. to pass an array to a function it requires to perimeters 1) name of the array which is temps in our case. 2) maximum number of the index which is k.
• note the syntax of void average(double arr[], int n). Since we are passing an array to the function so its formal parameters would be arr[] and an int n which specifies the maximum size of our array temps[].