With the aid of the functions above, write a program that
a. Gets from the user the size of a data set, N.
b. Creates an array for real numbers using the value from (a) as size.
c. Uses the function from (1) to populate the array with values from the user.
d. Uses the function from (3) to get the mean, variance, and standard deviation.
e. Uses the function from (2) to display the numbers in the array (data set).
f. Displays the mean, variance, and standard deviation of the data set.
#include <iostream>
#include <math.h>
using namespace std;
int get_n(){
cout<<"Enter data size: ";
int n; cin>>n;
return n;
}
float* fill(float* array, int n){
cout<<"Enter data items; \n";
for(int i = 0; i < n; i++) cin>>array[i];
return array;
}
float mean(float *array, int n){
float sum = 0;
for(int i = 0; i < n; i++) sum += array[i];
return sum / n;
}
float variance(float *array, int n){
float sumsq = 0, x = mean(array, n);
for(int i = 0; i < n; i++) sumsq += array[i] * array[i];
return sumsq / n - x * x;
}
float std_dev(float *array, int n){
return pow(variance(array, n), 0.5);
}
void display_data_set(float* array, int n){
cout<<"Data set: ";
for(int i = 0; i < n; i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
void display(float *array, int n){
cout<<"Mean: "<<mean(array, n)<<endl;
cout<<"Variance: "<<variance(array, n)<<endl;
cout<<"Standard deviation: "<<std_dev(array, n);
}
int main(){
int n = get_n();
float array[n];
fill(array, n);
display_data_set(array, n);
display(array, n);
return 0;
}
Comments
Leave a comment