Write a function stats that takes an array and the number of elements in the array. Then, it computes and prints the minimum value, maximum value, and the average value of all the values in the array. The output should be formatted with a two-digit precision. The function name: stats The function parameters (in this order): an array, double The number of elements in the array, int The function should not return anything.

Respuesta :

Answer:

#include<iostream>

using namespace std;

void stats(double [],int);  

int main()

{

int totalElements,i;

 

cout<<"Enter total elements:"<<endl;

cin>>totalElements;

double array[totalElements];

cout<<"Enter the elements in array:"<<endl;

for(i=0;i<totalElements;i++)

cin>>array[i];

stats(array,totalElements);  

}

void stats(double array[],int totalElements)

{

int i;

double minimum,maximum;

double Sum=0.0,average=0.0;

minimum=array[0],maximum=array[0];

for(i=0;i<totalElements;i++)

{

if(array[i]>maximum)

maximum=array[i];

if(array[i]<minimum)

minimum=array[i];

Sum+=array[i];

}

average=Sum/totalElements;

cout<<"Test: ";

for(i=0;i<totalElements;i++)

cout << fixed << setprecision(2) <<array[i]<<" ";

cout<<endl;

cout <<"minimum:"<< fixed << setprecision(2) <<minimum<<endl;

cout <<"maximum:"<< fixed << setprecision(2) <<maximum<<endl;

cout <<"average:"<< fixed << setprecision(2) <<average<<endl;

 

}

Explanation:

  • Loop through the total elements to get the input from user and call the stats function.
  • In the stats function check whether a number is maximum, minimum or average.
  • Calculate the average by finding the sum of all the numbers in array  and dividing it by total numbers.
  • Finally display the results.