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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int getN();
void getValues(double* values,int N);
double mean(double *values, int N);
double variance(double *values, int N);
double standardDeviation(double *values, int N);
void printDataSet(double* values, int N);
int main(){
int i;
int N= getN();
double* values = (double*)malloc(N * sizeof(double));
getValues(values,N);
printDataSet(values,N);
free(values);
return 0;
}
int getN(){
int N=0;
while(N<1){
printf("Enter N of data set: ");
scanf("%d",&N);
}
return N;
}
void getValues(double* values,int N){
int i=0;
printf("Enter 5 numbers\n");
for(i=0;i<N;i++){
scanf("%lf",&values[i]);
}
}
double mean(double *values, int N){
double sum = 0;
for(int i = 0; i < N; i++){
sum += values[i];
}
return sum / N;
}
double variance(double *values, int N){
double sum = 0;
double m = mean(values, N);
for(int i = 0; i < N; i++){
sum += values[i] * values[i];
}
return sum / N - m * m;
}
double standardDeviation(double *values, int N){
return pow(variance(values, N), 0.5);
}
void printDataSet(double* values, int N){
int i;
printf("DATA SET\n");
for(i = N-1; i >=0; i--){
printf("Index %d: %.6f\n",i,values[i]);
}
printf("Mean: %.6f\n",mean(values, N));
printf("Variance: %.6f\n",variance(values, N));
printf("Standard deviation: %.6f\n",standardDeviation(values, N));
}
Comments
Leave a comment