Calculate Sum, Average, Variance and Standard Deviation: C Program

Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main() and print the results in main().

Analyze The Above Problem Statement

1. We need to write a function to calculate the results.
2. Our function receives 5 integers.
3. We need to calculate sum, average or mean and standard deviation inside the function.
4. Problem statement is asking us to return 3 values from the function, which is not possible. But we can work around and use pointers to make it work. So the problem statement is indirectly asking us to use pointers to return more than 1 value from the function.
5. We will not be using arrays in this program since the number of inputs is fixed to 5. We can take 5 variables to store the values entered/input by the user.

Note: Perfect code for above problem statement is present at the end of this blog post / article. In the video we’ve made some modifications to get accurate results for various user inputs. None-the-less, you can find the exact c source code for above problem statement at the end of this blog post.

Related Read:
Basics of Pointers In C Programming Language
Assignment Operators in C

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: Calculate Sum, Average, Variance and Standard Deviation: C Program


[youtube https://www.youtube.com/watch?v=2CU6uSjyndA]

YouTube Link: https://www.youtube.com/watch?v=2CU6uSjyndA [Watch the Video In Full Screen.]


Source Code: Calculate Sum, Average, Variance and Standard Deviation: C Program

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

void stand_devi(float, float, float, float, float,
                float*, float*, float*, float*);

int main()
{
    float a, b, c, d, e;
    float sum = 0, avg = 0, sd = 0, vari = 0;

    printf("Enter 5 numbers\n");
    scanf("%f%f%f%f%f", &a, &b, &c, &d, &e);

    stand_devi(a, b, c, d, e, &sum, &avg, &vari, &sd);

    printf("\nSum = %0.2f\n", sum);
    printf("Mean / Average = %0.2f\n", avg);
    printf("Variance = %0.2f\n", vari);
    printf("Standard Deviation = %0.2f\n", sd);

    return 0;
}

void stand_devi(float a, float b, float c, float d, float e,
                float *sum, float *avg, float *v, float *sd)
{
    *sum = a + b + c + d + e;
    *avg = *sum / 5.0;

    *v   += pow( (a - *avg), 2 );
    *v   += pow( (b - *avg), 2 );
    *v   += pow( (c - *avg), 2 );
    *v   += pow( (d - *avg), 2 );
    *v   += pow( (e - *avg), 2 );

    *v   = *v / 5.0;
    *sd  = sqrt(*v);
}

Output:
Enter 5 numbers
50
-5
14
122
41

Sum = 222.00
Mean / Average = 44.40
Variance = 1885.84
Standard Deviation = 43.43

Logic To Calculate Sum, Average, Variance and Standard Deviation

We pass 5 floating point variables and address of 4 floating point variables to the function stand_devi().

Using below formula we calculate sum, average/mean, variance and standard deviation:

*sum = a + b + c + d + e;
*avg = *sum / 5.0;

*variance = ( (a – *avg) x (a – *avg) + (b – *avg) x (b – *avg) + (c – *avg) x (c – *avg) + (d – *avg) x (d – *avg) + (e – *avg) x (e – *avg) ) / 5.0;

*standard_deviation = sqrt(*variance);

Inside stand_devi() function we are calculating sum, average, variance and standard deviation and storing it inside the address of variables sum, avg, vari, sd. So value changes is reflected in the entire program and not just inside stand_devi() method.

C Program Source Code Which Matches the Problem Statement Exactly

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

void stand_devi(int, int, int, int, int,
                float*, float*, float*);

int main()
{
    int a, b, c, d, e;
    float sum = 0, avg = 0, sd = 0;

    printf("Enter 5 numbers\n");
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);

    stand_devi(a, b, c, d, e, &sum, &avg, &sd);

    printf("\nSum = %0.2f\n", sum);
    printf("Mean / Average = %0.2f\n", avg);
    printf("Standard Deviation = %0.2f\n", sd);

    return 0;
}

void stand_devi(int a, int b, int c, int d, int e,
                float *sum, float *avg, float *sd)
{
    float v = 0; // Variance

    *sum = a + b + c + d + e;
    *avg = *sum / 5.0;

     v   += pow( (a - *avg), 2 );
     v   += pow( (b - *avg), 2 );
     v   += pow( (c - *avg), 2 );
     v   += pow( (d - *avg), 2 );
     v   += pow( (e - *avg), 2 );

     v   /= 5.0;

    *sd  = sqrt(v);
}

Output:
Enter 5 numbers
50
69
92
30
-60

Sum = 181.00
Mean / Average = 36.20
Standard Deviation = 52.29

Here we’re following all the things mentioned in the problem statement. We are taking 5 integer values and passing it to our user defined function. Inside the function we are calculating the sum, average and standard deviation and storing it at address of variables sum, avg, and sd. This modification or change is reflected everywhere in the program as we are modifying the value present at address. Addresses are unique and any changes made to the value present at a address reflects everywhere in the program. That’s how we are able to print the result i.e., values of sum, avg and sd inside main() function, after calling stand_devi() function.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert