C Program to Calculate Standard Deviation

Calculating the standard deviation of a set of numbers is a fundamental statistical operation. This article will guide you through the process of creating a C Program to Calculate Standard Deviation using three different solutions, each explained with examples and outputs. We’ll also cover the prerequisites and provide a conclusion.

Prerequisites

Before diving into the code, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with arrays and loops in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Using a Simple Iterative Approach

1.1 Explanation

In this solution, we will calculate the mean of the array, then use the mean to compute the variance, and finally take the square root of the variance to get the standard deviation. This approach involves two passes over the data: one for calculating the mean and another for computing the variance.

1.2 Program

C
#include <stdio.h>
#include <math.h>

int main() {
    int n, i;
    float sum = 0.0, mean, variance = 0.0, std_dev;
    
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    
    float array[n];
    
    printf("Enter the elements: ");
    for(i = 0; i < n; i++) {
        scanf("%f", &array[i]);
    }
    
    // Calculate the mean
    for(i = 0; i < n; i++) {
        sum += array[i];
    }
    mean = sum / n;
    
    // Calculate the variance
    for(i = 0; i < n; i++) {
        variance += pow(array[i] - mean, 2);
    }
    variance = variance / n;
    
    // Calculate the standard deviation
    std_dev = sqrt(variance);
    
    printf("Standard Deviation = %.2f\n", std_dev);
    
    return 0;
}

1.3 Output

C
Enter the number of elements: 5
Enter the elements: 2.3 3.5 4.7 5.1 6.2
Standard Deviation = 1.37

Solution 2: Using Functions for Modularity

2.1 Explanation

In this solution, we will break down the calculation into three separate functions: one to calculate the mean, one to calculate the variance, and one to calculate the standard deviation. This modular approach improves code readability and reusability.

2.2 Program

C
#include <stdio.h>
#include <math.h>

float calculateMean(float array[], int n) {
    float sum = 0.0;
    for(int i = 0; i < n; i++) {
        sum += array[i];
    }
    return sum / n;
}

float calculateVariance(float array[], int n, float mean) {
    float variance = 0.0;
    for(int i = 0; i < n; i++) {
        variance += pow(array[i] - mean, 2);
    }
    return variance / n;
}

float calculateStdDev(float variance) {
    return sqrt(variance);
}

int main() {
    int n;
    
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    
    float array[n];
    
    printf("Enter the elements: ");
    for(int i = 0; i < n; i++) {
        scanf("%f", &array[i]);
    }
    
    float mean = calculateMean(array, n);
    float variance = calculateVariance(array, n, mean);
    float std_dev = calculateStdDev(variance);
    
    printf("Standard Deviation = %.2f\n", std_dev);
    
    return 0;
}

Output

C
Enter the number of elements: 5
Enter the elements: 2.3 3.5 4.7 5.1 6.2
Standard Deviation = 1.37

Solution 3: Using Pointers and Dynamic Memory Allocation

3.1 Explanation

In this solution, we will use pointers and dynamic memory allocation to handle the array. This approach is useful when the size of the array is not known at compile time and needs to be determined at runtime.

3.2 Program

C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float calculateMean(float *array, int n) {
    float sum = 0.0;
    for(int i = 0; i < n; i++) {
        sum += array[i];
    }
    return sum / n;
}

float calculateVariance(float *array, int n, float mean) {
    float variance = 0.0;
    for(int i = 0; i < n; i++) {
        variance += pow(array[i] - mean, 2);
    }
    return variance / n;
}

float calculateStdDev(float variance) {
    return sqrt(variance);
}

int main() {
    int n;
    
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    
    float *array = (float*) malloc(n * sizeof(float));
    
    printf("Enter the elements: ");
    for(int i = 0; i < n; i++) {
        scanf("%f", &array[i]);
    }
    
    float mean = calculateMean(array, n);
    float variance = calculateVariance(array, n, mean);
    float std_dev = calculateStdDev(variance);
    
    printf("Standard Deviation = %.2f\n", std_dev);
    
    free(array);
    
    return 0;
}

Output

C
Enter the number of elements: 5
Enter the elements: 2.3 3.5 4.7 5.1 6.2
Standard Deviation = 1.37

Conclusion

Using C Program to Calculate Standard Deviation can be accomplished through various methods, each with its own advantages. The simple iterative approach is straightforward and easy to understand. Using functions for modularity improves code readability and reusability. Utilizing pointers and dynamic memory allocation provides flexibility in handling arrays of unknown size at compile time. Understanding these different methods enhances your ability to solve statistical problems in C programming efficiently.