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.
Explain
Explain why standard deviation uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Treat the three observations as a sample and use the sample-variance denominator.
Standard Deviation: follow the data
Given data
Two classes can have the same average but different spreads. Measure how far this class’s marks lie from their mean.
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.
Observed result
Population SD: 8.165
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
Start with the sample
The mean of 40,50,60 is fifty.
Apply the rule
The squared distances are 100,0,100. Population variance is 200/3, and its square root is about 8.165.
Check the result
Population SD: 8.165
Population SD: 8.165
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.
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.