Answer to Question #185517 in C for Woyengimiesindo Malati

Question #185517

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.

SAMPLE RUN

Enter size of data set: 5

Enter 5 numbers

2.4

-3

6

11.2

3

DATA SET

Index 4: 3.000000

Index 3: 11.200000

Index 2: 6.000000

Index 1: -3.000000

Index 0: 2.400000

Mean: 3.920000

Variance: 21.673600

Standard Deviation: 4.655491


1
Expert's answer
2021-05-04T12:43:02-0400
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>


int getSize();
void getRealNumbers(float* realNumbers,int size);
float calculateMean(float *realNumbers, int size);
float calculateVariance(float *realNumbers, int size);
float calculateStandardDeviation(float *realNumbers, int size);
void displayDataSet(float* realNumbers, int size);
void displayResult(float *realNumbers, int size);




int main(){
	int i;
	int size= getSize();
	//Creates an array for real numbers using the value from (a) as size.
	float* realNumbers = (float*)malloc(size * sizeof(float));
	getRealNumbers(realNumbers,size);
	displayDataSet(realNumbers,size);
	displayResult(realNumbers,size);
    // deallocating the memory
    free(realNumbers);
	getchar();
	getchar();
	return 0;
}


//Gets from the user the size of a data set, N.
int getSize(){
	int size=0;
	while(size<1){
		printf("Enter size of data set: ");
		scanf("%d",&size);
	}
	return size;
}
//Uses the function from (1) to populate the array with values from the user.
void getRealNumbers(float* realNumbers,int size){
	int i=0;
	printf("Enter 5 numbers\n");
	for(i=0;i<size;i++){
		scanf("%f",&realNumbers[i]);
	}
}
//Uses the function from (3) to get the mean, variance, and standard deviation. 
float calculateMean(float *realNumbers, int size){
    float sum = 0;
    for(int i = 0; i < size; i++){
		sum += realNumbers[i];
	}
    return sum / size;
}
//Calculates variance
float calculateVariance(float *realNumbers, int size){
    float sumProduct = 0;
	float mean = calculateMean(realNumbers, size);
    for(int i = 0; i < size; i++){
		sumProduct += realNumbers[i] * realNumbers[i];
	}
    return sumProduct / size - mean * mean;
}
//Calculates standard deviation
float calculateStandardDeviation(float *realNumbers, int size){
	return pow((double)calculateVariance(realNumbers, size), (double)0.5);
}
//Uses the function from (2) to display the numbers in the array (data set).
void displayDataSet(float* realNumbers, int size){
    int i;
	printf("DATA SET\n");
    for(i =  size-1; i >=0; i--){
		printf("Index %d: %.6f\n",i,realNumbers[i]);
    }
}
//Displays the mean, variance, and standard deviation of the data set.
void displayResult(float *realNumbers, int size){
    printf("Mean: %.6f\n",calculateMean(realNumbers, size));
    printf("Variance: %.6f\n",calculateVariance(realNumbers, size));
    printf("Standard deviation: %.6f\n",calculateStandardDeviation(realNumbers, size));
}


Output


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS