142k views
1 vote
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.

User Nvrs
by
3.5k points

2 Answers

3 votes

Final answer:

The 'stats' function calculates and prints the minimum, maximum, and average values from a given array with two-digit precision.

Step-by-step explanation:

The function named stats is designed to compute statistical values such as the minimum value, maximum value, and the average value of elements in an array. The function takes two parameters: an array of type double and the number of elements in the array of type int. It then proceeds to calculate the required statistics and prints them with a precision of two decimal places. This function does not return any value as its primary purpose is to print out the calculated statistics.

User Dchhetri
by
3.6k points
1 vote

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;

}

Step-by-step 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.

User Chitresh
by
4.1k points