HOMEWORK #22 – Statistical Functions Write a program that prompts the user to en
ID: 675045 • Letter: H
Question
HOMEWORK #22 – Statistical Functions
Write a program that prompts the user to enter a set of N double precision floating point numbers, scans them into a 1-D array, and calculates various statistical properties of the array of numbers, including the maximum, minimum, average, variance, standard deviation, standard error, and median.
Below are the mathematical definitions of interest. If you have an array x[] with N elements:
Maximum – The maximum finds the maximum of the array elements x[i], where i ranges from
0 to N-1.
Minimum – The minimum finds the minimum of the array elements x[i], where i ranges from 0
Variance – To find the population variance, sum the square of the difference between each
variance:
Standard Error – To find the standard error, divide the standard deviation by the square root of the number of elements in the array N:
Media – The median finds the middle value of an array of elements. For an odd number of elements, the median is the central array element, after the array has been numerically sorted. For an even number of elements, the median is the average of the two central elements, after the array has been numerically sorted.
NOTE: For this function to work, you will also need a sort() function that sorts the elements in ascending order.
Specifications:
Use the following function prototypes:
Prompt the user to enter the number of elements for the array and use that information to create a variable length array. Then use a for loop to prompt the user to enter in a number for each element of the array, and scan in the appropriate numbers. Your program should then call the appropriate functions and print the results to the screen using a %.2f format. Finally,use the exit function to end the program. You must use functions to solve this problem! If you execute the program, the following information should be displayed:
~> hw22.o
Enter the number of elements in the array: 3
Enter element 0: 3
Enter element 1: -1
Enter element 2: 4
~> hw22.o
Enter the number of elements in the array: 4
Enter element 0: -5.2
Enter element 1: 2.6
Enter element 2: 6.3
Enter element 3: 19.3
HINTS: For the maximum and minimum, initially set the maximum/minimum to the 0th element of
the array. Then compare that value against the other elements and store find the true maximum/minimum. Use the math library and the pow function for the variance. Use the math library and the sqrt function for the standard deviation and the standard error. The median function must call the sort function.
Explanation / Answer
Program code:
#include "stdafx.h"
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
double maximum(int n,double data[]);
double minimum(int n,double data[]);
double average(int n,double data[]);
double variance(int n,double data[]);
double std_dev(int n,double data[]);
double std_err(int n,double data[]);
double median(int n,double data[]);
double sort(int n,double data[]);
int main()
{
int n, i;
double data[10];
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0; i<n; i++)
scanf("%lf",&data[i]);
printf("Standard Deviation = %.2e ", std_dev(n,data));
printf("medain = %.2f ", median(n,data));
printf("Standard Error = %.2e ", std_err(n,data));
printf("average = %.2f ", average(n,data));
printf("maximum = %.2f ", maximum(n,data));
printf("minimum = %.2f ", minimum(n,data));
system("pause");
return 0;
}
double std_dev(int n,double data[])
{
double mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return sqrt(sum_deviation/n);
}
double variance(int n,double data[]){
double mean=0.0, sum_var=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_var+=(data[i]-mean)*(data[i]-mean);
return(sum_var/n);
}
double maximum(int n,double data[])
{
double max=data[0];
int i;
for(i=0; i<n;++i)
{
if(data[i]>max)
{
max=data[i];
}
}
return max;
}
double minimum(int n,double data[])
{
double min=data[0];
int i;
for(i=0; i<n;++i)
{
if(data[i]<min)
{
min=data[i];
}
}
return min;
}
double average(int n,double data[])
{
double mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
return mean;
}
double std_err(int n,double data[])
{
double mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return (sqrt(sum_deviation)/n);
}
double median(int n,double data[])
{
double median=0.0;
sort(n,data);
if(n%2==0)
{
if ( n % 2 == 0)
median = (data[n/2] + data[n/2-1])/2.0 ;
else
median = data[n/2 + 1];
}
return median;
}
double sort(int n,double data[])
{
double a;
int i=0,j;
for (j = i + 1; j < n; ++j)
{
if (data[i] > data[j])
{
a = data[i];
data[i] = data[j];
data[j] = a;
}
}
return 0;
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.