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

C Program To Swap Two Numbers using Pointers

Lets write a C program to swap 2 numbers using pointers and function.

When we call the function, we pass the reference or address of the variable, so this method is called “Call by Reference“.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Swap Two Numbers using Pointers


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

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


Source Code: C Program To Swap Two Numbers using Pointers and Function

#include<stdio.h>

void swap(int*, int*);

int main()
{
    int a, b;

    printf("Enter values for a and b\n");
    scanf("%d%d", &a, &b);

    printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

    swap(&a, &b);

    printf("\nAfter swapping: a = %d and b = %d\n", a, b);

    return 0;
}

void swap(int *x, int *y)
{
    int temp;

    temp = *x;
    *x   = *y;
    *y   = temp;
}

Output 1:
Enter values for a and b
100
200

Before swapping: a = 100 and b = 200

After swapping: a = 200 and b = 100

Output 2:
Enter values for a and b
30
20

Before swapping: a = 30 and b = 20

After swapping: a = 20 and b = 30

Logic To Swap Two Numbers using Pointers and Function

We ask the user to enter values for variable a and b. We pass the address of variable a and b to function swap().

Inside function swap() we take a local variable temp. Since address of variable a and b are passed to swap() method, we take 2 pointer variables *x and *y. Pointer variable x holds the address of a and pointer variable y holds the address of b. Using below logic we swap the values present at address a( or x ) and b( or y ).


temp = *x;
*x = *y;
*y = temp;

Since we are directly changing the value present at particular address, the value gets reflected throughout the program and not just inside particular function. That’s why we are able to print the result inside main function and the values of variable a and b are swapped, without getting any returned value from function swap().

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