Write a C program that calculate the mean









- Slides: 9
Write a C program that calculate the mean, variance and standard deviation of n observations
Real life example 9
#include <stdio. h> #include <conio. h> #include <math. h> main() { /* Start of main() function */ float x[10]; /* declares the array variable x[i] as float */ int i, n; float mean, var, SD, sum=0, sum 1=0; /* declares the variables mean , var, SD and sum as float */ clrscr(); printf("Enter the value of Nn"); scanf("%d", &n); /* accepts values from user */ printf("Enter %d real numbersn“, n); for(i = 0; i < n; i++) { scanf("%f", &x[i]); }
/* Compute the sum of all elements */ for(i = 0; i < n; i ++) { sum = sum + x[i]; /* calculating the mean using the equation */ } mean = sum /(float) n; /* Compute variance and standard deviation */ for(i = 0; i < n; i++) { sum 1 = sum 1 + pow((x[i] - mean), 2); /* calculate the variance using general equation */ } var = sum 1 / (float) n; SD = sqrt(var); /* calculating standard deviation */ printf("Average of all elements = %. 2 fn", mean); /* prints the output of mean */ printf("Varience of all elements = %. 2 fn", var); /* prints the output 0 f variance */ printf("Standard deviation = %. 2 fn", SD); /* prints the output of standard deviation */ } /* End of main() function */
#include <stdio. h> #include <conio. h> #include <math. h> #define MAXSIZE 10 main() { /* Start of main() function */ float x[MAXSIZE]; int i, n; float mean, var, SD, sum=0, sum 1=0; /* declares the variables mean , var, SD and sum as float */ clrscr(); printf("Enter the value of Nn"); scanf("%d", &n); /* accepts values from user */ printf("Enter %d real numbersn", n); for(i = 0; i < n; i++) { scanf("%f", &x[i]); }
/* Compute the sum of all elements */ for(i = 0; i < n; i ++) { sum = sum + x[i]; /* calculating the mean using the equation */ } mean = sum /(float) n; /* Compute variance and standard deviation */ for(i = 0; i < n; i++) { sum 1 = sum 1 + pow((x[i] - mean), 2); /* calculate the variance using general equation */ } var = sum 1 / (float) n; SD = sqrt(var); /* calculating standard deviation */ printf("Average of all elements = %. 2 fn", mean); /* prints the output of mean */ printf("Varience of all elements = %. 2 fn", var); /* prints the output 0 f variance */ printf("Standard deviation = %. 2 fn", SD); /* prints the output of standard deviation */ } /* End of main() function */
How can we fit a two variables regression line?
H. W. Write a C program that calculate the estimated regression coefficients ( ) and finally shows the fitted regression line.