---
title: "Standard deviation and mean of array"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:standard-deviation-pseudocode"
source: https://www.bctengineer.com/q/semester_1/computer_programming/standard-deviation-pseudocode
site: BCT Engineering
---

# Standard deviation and mean of array

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2067 Asar, 8 marks
- Video: [Mean Variance and Standard Deviation in C (Hindi) #cprogramming #cprogrammingtutorial](https://www.youtube.com/watch?v=oVkUk38hDUE) — AshMit Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/standard-deviation-pseudocode
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a pseudocode to find the standard deviation of an array of values in C programming. The array elements are read from the terminal. Use user defined functions to calculate standard deviation and mean. Standard deviation of n value is given by
SD = √( Σ (xm - xi)² / n ), where the sum runs over i = 1 to n and xm is the mean of the values.

## Short answer

Compute the mean of the array, then for each element accumulate (mean - xi)². Divide the sum by n and take the square root to get standard deviation. Use separate functions for mean and standard deviation.

## Answer

SD = √( Σ (xm − xi)² / n ), where xm is the mean. Two user defined functions are used: `mean()` and `stdDev()`.

#### Pseudocode

```text
FUNCTION mean(x[], n)
    sum <- 0
    FOR i <- 0 TO n-1
        sum <- sum + x[i]
    END FOR
    RETURN sum / n
END FUNCTION

FUNCTION stdDev(x[], n)
    xm <- mean(x, n)
    s <- 0
    FOR i <- 0 TO n-1
        s <- s + (xm - x[i]) * (xm - x[i])
    END FOR
    RETURN sqrt(s / n)
END FUNCTION

MAIN
    READ n
    FOR i <- 0 TO n-1
        READ x[i]
    END FOR
    m  <- mean(x, n)
    sd <- stdDev(x, n)
    PRINT "Mean =", m
    PRINT "Standard deviation =", sd
END
```

#### C version of the same logic (checked by running)

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

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

float stdDev(float x[], int n)
{
    float xm = mean(x, n), s = 0;
    for (int i = 0; i < n; i++)
        s += (xm - x[i]) * (xm - x[i]);
    return sqrt(s / n);
}

int main(void)
{
    float x[100];
    int n;
    printf("Enter n: ");
    scanf("%d", &n);
    printf("Enter %d values: ", n);
    for (int i = 0; i < n; i++)
        scanf("%f", &x[i]);
    printf("Mean = %.3f\nStandard deviation = %.3f\n", mean(x, n), stdDev(x, n));
    return 0;
}
```

**Sample run** (values 2 4 4 4 5: mean = 3.8, Σ(xm − xi)² = 4.8, SD = √(4.8/5) = 0.980)

Input: `5 2 4 4 4 5`

```text
Enter n: Enter 5 values: Mean = 3.800
Standard deviation = 0.980
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does mean(x, n) return?
   A. The sum of all elements
   B. The largest element
   C. The sum of squares divided by n
   D. The sum of all elements divided by n
   Answer: D. mean adds all elements and divides by n. (Key point: Mean function)

2. Which statement inside the loop of stdDev() builds the sum needed for SD?
   A. s <- s + x[i]
   B. s <- s + (xm - x[i]) * (xm - x[i])
   C. s <- s + xm * x[i]
   D. s <- s + (xm - x[i])
   Answer: B. SD needs the sum of squared deviations from the mean. (Key point: Formula in code)

3. What does stdDev() return at the end?
   A. s / n
   B. sqrt(s) * n
   C. sqrt(s / n)
   D. s * n
   Answer: C. SD is the square root of the average squared deviation. (Key point: Final step)

4. How does stdDev() get the mean xm?
   A. It asks the user again
   B. It uses the first element
   C. It calls the user-defined function mean(x, n)
   D. It computes sqrt of n
   Answer: C. stdDev calls mean() so the mean logic is reused. (Key point: Function reuse)

5. For values 2 4 4 4 5, what are the mean and SD?
   A. Mean 3.8, SD about 4.8
   B. Mean 4, SD about 0.980
   C. Mean 3.8, SD about 0.96
   D. Mean 3.8, SD about 0.980
   Answer: D. Sum of squared deviations is 4.8, so SD = sqrt(4.8/5) = 0.980. (Key point: Sample calculation)

## Related in Computer Programming

- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Insert value in array at position](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-insert-algorithm) — Computer Programming, chapter 7
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Complex number sum via functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-sum-function) — Computer Programming, chapter 8
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6

## Related in other subjects

- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
- [Standard and ladder programming block diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Afilter-standard-ladder-block) — Digital Control System, chapter 4
- [Queuing notation, default values, elements](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Aqueuing-notation-defaults-elements) — Simulation and Modeling, chapter 4
