C Program to Calculate Standard Deviation

C Program to Calculate Standard Deviation

Two classes can have the same average but different spreads. Measure how far this class’s marks lie from their mean.

Standard deviation is the square root of a variance. State whether the supplied data is the whole population or a sample, because the variance denominator differs.

Before this exercise, review arithmetic and conversions and numeric types and their limits. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why standard deviation uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Treat the three observations as a sample and use the sample-variance denominator.

Standard Deviation: follow the data

  1. Given data

    Two classes can have the same average but different spreads. Measure how far this class’s marks lie from their mean.

  2. Rule

    Standard deviation is the square root of a variance. State whether the supplied data is the whole population or a sample, because the variance denominator differs.

  3. Observed result

    Population SD: 8.165

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The population calculation averages squared distances from the mean using count. The usual sample variance estimator uses count−1 and requires at least two observations.

This direct two-pass demonstration calculates the mean first, then the squared distances. More numerically robust algorithms may be appropriate for extreme magnitudes or long streams.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
#include <math.h>
int main(void) {
  double marks[] = {40.0, 50.0, 60.0};
  int count = 3;
  double total = 0.0;
  for (int i = 0; i < count; i++) total += marks[i];
  double mean = total / count, squares = 0.0;
  for (int i = 0; i < count; i++) { double difference = marks[i] - mean; squares += difference * difference; }
  printf("Population SD: %.3f\n", sqrt(squares / count));
  return 0;
}

Expected output

Population SD: 8.165
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
#include <math.h>
int main(void) {
  double marks[] = {40.0, 50.0, 60.0};
  int count = 3;
  double total = 0.0;
  for (int i = 0; i < count; i++) total += marks[i];
  double mean = total / count, squares = 0.0;
  for (int i = 0; i < count; i++) { double difference = marks[i] - mean; squares += difference * difference; }
  printf("Population SD: %.3f\n", sqrt(squares / count));
  return 0;
}

The mean of 40,50,60 is fifty.

The squared distances are 100,0,100. Population variance is 200/3, and its square root is about 8.165.

Follow the worked calculation

  1. Start with the sample

    The mean of 40,50,60 is fifty.

  2. Apply the rule

    The squared distances are 100,0,100. Population variance is 200/3, and its square root is about 8.165.

  3. Check the result

    Population SD: 8.165

    Population SD: 8.165
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
#include <math.h>
int main(void) {
  double marks[] = {40.0, 50.0, 60.0};
  int count = 3;
  double total = 0.0;
  for (int i = 0; i < count; i++) total += marks[i];
  double mean = total / count, squares = 0.0;
  for (int i = 0; i < count; i++) { double difference = marks[i] - mean; squares += difference * difference; }
  printf("Population SD: %.3f\n", sqrt(squares / count));
  return 0;
}

Treat the three observations as a sample and use the sample-variance denominator.

Worked answer

Dividing the same squared-distance sum by two gives variance 100 and standard deviation ten. The change is an interpretation of the dataset, not merely a formatting change.

C17 · complete program

#include <stdio.h>
#include <math.h>
int main(void) {
  double marks[] = {40.0, 50.0, 60.0};
  int count = 3;
  double total = 0.0;
  for (int i = 0; i < count; i++) total += marks[i];
  double mean = total / count, squares = 0.0;
  for (int i = 0; i < count; i++) { double difference = marks[i] - mean; squares += difference * difference; }
  printf("Sample SD: %.3f\n", sqrt(squares / (count - 1)));
  return 0;
}

Expected output

Sample SD: 10.000
Run in C compiler →

Recognize the important boundaries

Population variance needs a nonempty dataset; sample variance needs at least two observations. Define how missing or invalid marks are handled before counting them.

Large or nonfinite values can make sums or squared distances unreliable. The fixed small fixture is an arithmetic explanation, not a general statistics library.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Which denominator is used for this population variance?
Choose one answer. You can try again.

Answer and explanation

The observation count.

Correct. All supplied observations are treated as the complete population in that calculation.

Use the idea independently

Standard deviation is the square root of a variance. State whether the supplied data is the whole population or a sample, because the variance denominator differs.

Continue with testing the problem’s boundary cases.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Course overview →