Programming assignment: 2) Write a program that prompts the user for a variable
ID: 3678084 • Letter: P
Question
Programming assignment: 2) Write a program that prompts the user for a variable number of real numbers and computes several descriptive statistics on that set of real numbers. The real numbers should be placed in an array 3) The specific descriptive statistics you should compute are: minimum, maximum average and standard deviation. 4) Your program should adhere to the following I/O specifications: How many real numbers do you wish to enter (maximum is 20): Read number from user Enter number 1: Enter number 2: Minimum: ww.ww Maximum: xx.xx Average: vv.vv Standard Deviation: zz.zz 5) When printing floating point values, only print the first 2 decimal places 6) This is a sample output of your program How many real numbers do you wish to enter (maximum is 20): 5 Enter number 1: 5.0 Enter number 2: 4.0 Enter number 3: 5.0 Enter number 4: 0.0 Enter number 5: 6.0 Minimum: 0.00 Maximum: 6.00 Average: 4.00 Standard Deviation: 2.10Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double arr [20] ;
int n;
double avg(double arr[],int size)
{
double sum=0.0;
for(int i=0;i<size;i++) //loop to calculate sum of array elements
sum=sum+arr[i];
double avg=sum/n;
return(avg);
}
double stddev(double arr[], int n)
{
double sum=0.0, sumdev=0.0;
int i;
for(i=0; i<n;++i)
sum+=arr[i];
double mean=sum/n;
for(i=0; i<n;++i)
sumdev+=(arr[i]-mean)*(arr[i]-mean);
return sqrt(sumdev/n);
}
double max(double arr[],int n)
{
double maximum=arr[0];
for(int i=1;i<n;++i) /* Loop to store largest number to arr[0] */
{
if(maximum<arr[i])
maximum=arr[i];
}
return(maximum);
}
double min(double arr[],int n)
{
double minimum=arr[0];
for(int i=1;i<n;++i)
{
if(minimum>arr[i])
minimum=arr[i];
}
return(minimum);
}
int main ()
{
cout<<"enter how many items you want to insert"<<endl;
cin>>n;
for (int i=0 ; i<n ; i++ )//loop to read n items.
{
cout<<"enter number"<<i+1<<":"<<endl;
cin>>arr[i];
}
cout<<"Maximum:"<<fixed<<setprecision(2)<<max(arr,n)<<endl;
cout<<"Minimum:"<<setprecision(2)<<min(arr,n)<<endl;
cout<<"Average:"<<setprecision(2)<<avg(arr,n)<<endl;
cout<<"Standard Deviation"<<fixed<<setprecision(2)<<stddev(arr,n)<<endl;
return 0;
}
output:
enter how many items you want to insert
3
enter number1:
1.11
enter number2:
21.11
enter number3:
12.22
Maximum:21.11
Minimum:1.11
Average:11.48
Standard Deviation8.18
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.