Write a program using functions where a function collects 8 numbers from the use
ID: 3664907 • Letter: W
Question
Write a program using functions where a function collects 8 numbers from the user, finds the largest, smallest of those 8 numbers and uses passing by reference to update the variables belonging to the main program. Then the main program calls a function called CollectNumbers() and passes the variables largest, smallest BYREF(by reference). The CollectNumbers() return the average back to the main. Here is what the functions should accomplish. 'main program. Creates variables largest, smallest, average. Calls CollectNumbers() function and sends variables largest, smallest BYREF(by reference) The CollectNumbers() function should return the average. The main displays the average returned by the function and, smallest and largest variables updated by the function. Function CollectNumbersO receives largest and smallest by reference. The purpose of this function is to take 8 numbers from the user. Find the largest, smallest and the average of those numbers. The function puts the largest of the 8 numbers in received parameter largest and the smallest of those 8 numbers in the received parameter smallest. Then, Send the average back to the main. End functionExplanation / Answer
I am assuming language is c++.
#include<iostream>
#include<climits>
using namespace std;
float CollectNumbers(float &smallest, float &largest)
{
cout<<"enter 8 numbers ";
int arr[8];
int i=0;
float avg=0;
smallest=INT_MAX;
largest=INT_MIN;
for(i=0;i<8;i++)
{
cin>>arr[i];
avg=avg+arr[i];
if(arr[i]>largest)
largest=arr[i];
if(arr[i]<smallest)
smallest=arr[i];
}
avg=avg/8;
return avg;
}
int main()
{
float smallest,largest;
cout<<"avg is :"<<CollectNumbers(smallest,largest)<<endl;
cout<<"smallest number is:"<<smallest<<endl;
cout<<"largest number is:"<<largest<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.