PPS Lab · Experiment 18
Mean, variance, standard deviation and sorting of an array
CArrays, pointers and functionsJNTUH R22 · CS108ES
Aim
To write a C program that reads n numbers into a one-dimensional array and uses separate functions to find their mean, variance and standard deviation and to sort them in ascending order.
Algorithm
- Read n. If it is not between 1 and 100, print an error and stop. Read the n numbers into the array.
- Mean: add all the numbers and divide the sum by n.
- Variance: for each number, find (number − mean)², add these squares, and divide the total by n.
- Standard deviation: take the square root of the variance.
- Sort: use bubble sort. Compare each pair of neighbours and swap them if the left one is larger. Repeat n − 1 passes.
- Print the mean, variance, standard deviation and the sorted array.
Program
#include <stdio.h>
#include <math.h>
#define MAX 100
double mean(const double a[], int n) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
return sum / n;
}
double variance(const double a[], int n) {
double m = mean(a, n);
double total = 0;
for (int i = 0; i < n; i++) {
total += (a[i] - m) * (a[i] - m);
}
return total / n;
}
double standard_deviation(const double a[], int n) {
return sqrt(variance(a, n));
}
void sort(double a[], int n) {
for (int pass = 0; pass < n - 1; pass++) {
for (int j = 0; j < n - 1 - pass; j++) {
if (a[j] > a[j + 1]) {
double temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
void print_array(const double a[], int n) {
for (int i = 0; i < n; i++) {
printf("%.2f ", a[i]);
}
printf("\n");
}
int main(void) {
double a[MAX];
int n;
printf("Enter the number of elements (1 to %d): ", MAX);
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
printf("Invalid number of elements\n");
return 1;
}
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++) {
if (scanf("%lf", &a[i]) != 1) {
printf("Invalid input\n");
return 1;
}
}
printf("Mean = %.2f\n", mean(a, n));
printf("Variance = %.2f\n", variance(a, n));
printf("Standard deviation = %.2f\n", standard_deviation(a, n));
sort(a, n);
printf("Sorted array: ");
print_array(a, n);
return 0;
}Output
Eight numbers
Enter the number of elements (1 to 100): 8
Enter 8 numbers: 9 4 7 2 5 4 5 4
Mean = 5.00
Variance = 4.00
Standard deviation = 2.00
Sorted array: 2.00 4.00 4.00 4.00 5.00 5.00 7.00 9.00 Five numbers with fractions
Enter the number of elements (1 to 100): 5
Enter 5 numbers: 3.5 1.5 6 2 4
Mean = 3.40
Variance = 2.54
Standard deviation = 1.59
Sorted array: 1.50 2.00 3.50 4.00 6.00 Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
This program finds the population variance, dividing by n. Some statistics books use the sample variance, which divides by n − 1; for 9 4 7 2 5 4 5 4 that gives 4.57 instead of 4.
The array is passed to each function as a pointer to its first element, so sort changes the array in main directly. The other functions take const double a[] because they only read it.
The mean, variance and standard deviation are printed before sorting. The order does not change these values, but it keeps the steps clear.
Common mistakes
Using
intfor the sum or for the mean. The mean of 9 4 7 2 5 4 5 4 happens to be a whole number, but for most data the fraction is lost. Usedouble.Squaring with
^, as in(a[i] - m) ^ 2. In C,^is bitwise XOR, not power. Multiply the value by itself or usepowfrommath.h.Running the inner bubble sort loop up to
j < n. Thena[j + 1]reads one place past the end of the array. The inner loop must stop atj < n - 1 - pass.Forgetting
-lmwhen compiling on Linux, which givesundefined reference to sqrt.
Viva questions
Try to answer each one aloud before you open it.
What is the difference between variance and standard deviation?
Variance is the average of the squared distances of the values from the mean. Standard deviation is the square root of the variance, so it is in the same units as the data.
Why is the variance divided by n here and not by n − 1?
Dividing by n gives the population variance of exactly these n values. Dividing by n − 1 gives the sample variance, used when the values are a sample from a larger population.
How does bubble sort work?
It compares neighbouring elements and swaps them when they are in the wrong order. After each pass the largest remaining element has moved to the end, so n − 1 passes sort the array.
When an array is passed to a function, is it copied?
No. The function receives the address of the first element, so any change it makes, like sorting, is made to the original array.
Why are some parameters written as const double a[]?
constpromises that the function will not change the array. The compiler gives an error if the function tries to, which prevents mistakes.