Compute the Variance in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the variance of a set of numbers in C. The lab covers three main steps: calculating the mean, computing the sum of squared deviations from the mean, and dividing the sum by the count of numbers to obtain the variance. The lab provides detailed code examples and explanations to guide you through the process of implementing these statistical calculations in C.

The lab starts by demonstrating how to calculate the mean of a set of numbers, which is a crucial step in the variance computation. It then shows how to compute the sum of squared deviations from the mean, and finally, how to divide the sum by the count of numbers to arrive at the variance. By following the step-by-step instructions, you will gain a solid understanding of how to perform these statistical operations using the C programming language.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435167{{"`Compute the Variance in C`"}} c/for_loop -.-> lab-435167{{"`Compute the Variance in C`"}} c/arrays -.-> lab-435167{{"`Compute the Variance in C`"}} c/math_functions -.-> lab-435167{{"`Compute the Variance in C`"}} end

Compute Mean

In this step, you will learn how to compute the mean of a set of numbers in C. The mean is calculated by summing all values and dividing by the total count of numbers.

First, create a new C file to implement the mean calculation:

cd ~/project
nano mean.c

Now, write the following C code to compute the mean:

#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int count = sizeof(numbers) / sizeof(numbers[0]);
    int sum = 0;

    // Calculate sum of all numbers
    for (int i = 0; i < count; i++) {
        sum += numbers[i];
    }

    // Calculate mean
    float mean = (float)sum / count;

    printf("Numbers: ");
    for (int i = 0; i < count; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\nCount: %d\n", count);
    printf("Sum: %d\n", sum);
    printf("Mean: %.2f\n", mean);

    return 0;
}

Compile and run the program:

gcc mean.c -o mean
./mean

Example output:

Numbers: 10 20 30 40 50
Count: 5
Sum: 150
Mean: 30.00

Let's break down the code:

  • We define an array of integers numbers
  • Calculate the count of numbers using sizeof()
  • Use a for loop to calculate the sum of all numbers
  • Compute the mean by dividing the sum by the count
  • Print out the numbers, count, sum, and mean

Sum (x-mean)Âē and Divide by Count

In this step, you will learn how to calculate the variance by computing the sum of squared deviations from the mean and dividing by the count of numbers.

First, modify the previous mean.c file to include variance calculation:

cd ~/project
nano variance.c

Write the following C code to compute variance:

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

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int count = sizeof(numbers) / sizeof(numbers[0]);
    int sum = 0;
    float mean, variance = 0.0;

    // Calculate sum and mean
    for (int i = 0; i < count; i++) {
        sum += numbers[i];
    }
    mean = (float)sum / count;

    // Calculate sum of squared deviations
    for (int i = 0; i < count; i++) {
        variance += pow(numbers[i] - mean, 2);
    }

    // Divide by count to get variance
    variance /= count;

    printf("Numbers: ");
    for (int i = 0; i < count; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\nMean: %.2f\n", mean);
    printf("Variance: %.2f\n", variance);

    return 0;
}

Compile and run the program with math library:

gcc variance.c -o variance -lm
./variance

Example output:

Numbers: 10 20 30 40 50
Mean: 30.00
Variance: 200.00

Key steps in variance calculation:

  • Calculate the mean (from previous step)
  • Subtract mean from each number
  • Square the differences
  • Sum the squared differences
  • Divide by the count of numbers

Print the Variance

In this step, you will learn how to format and print the variance calculation with different precision levels and create a function to make the code more modular.

Modify the previous variance.c file to improve variance printing:

cd ~/project
nano variance_print.c

Write the following C code to enhance variance output:

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

// Function to calculate variance
float calculate_variance(int numbers[], int count) {
    int sum = 0;
    float mean, variance = 0.0;

    // Calculate sum and mean
    for (int i = 0; i < count; i++) {
        sum += numbers[i];
    }
    mean = (float)sum / count;

    // Calculate sum of squared deviations
    for (int i = 0; i < count; i++) {
        variance += pow(numbers[i] - mean, 2);
    }

    // Divide by count to get variance
    variance /= count;

    return variance;
}

// Function to print variance with different formats
void print_variance(float variance) {
    printf("Variance Representations:\n");
    printf("1. Standard Format:   %.2f\n", variance);
    printf("2. Scientific Notation: %e\n", variance);
    printf("3. Precise Format:    %.4f\n", variance);
}

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int count = sizeof(numbers) / sizeof(numbers[0]);

    // Calculate and print variance
    float variance = calculate_variance(numbers, count);

    printf("Original Numbers: ");
    for (int i = 0; i < count; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n\n");

    print_variance(variance);

    return 0;
}

Compile and run the program:

gcc variance_print.c -o variance_print -lm
./variance_print

Example output:

Original Numbers: 10 20 30 40 50

Variance Representations:
1. Standard Format:   200.00
2. Scientific Notation: 2.000000e+02
3. Precise Format:    200.0000

Key improvements:

  • Created separate functions for variance calculation
  • Added multiple variance printing formats
  • Demonstrated different ways to represent variance

Summary

In this lab, you learned how to compute the mean of a set of numbers in C by summing all the values and dividing by the total count. You then learned how to calculate the variance by computing the sum of squared deviations from the mean and dividing by the count of numbers. This allows you to measure the spread or dispersion of the data around the mean, which is a useful metric for statistical analysis.

Other C Tutorials you may like