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

How to return multiple values from a single function

Program: 127

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

How to return multiple values form a single function.

#include<stdio.h>
#include<math.h> //for sqrt function
void func(int a, int b, int c, int d, int e, float *sum, float *avg, float *std_dev);

//main function
void main()
{
    int a,b,c,d,e;
    float sum, avg, std_dev;

    printf("Enter 1st number: ");
    scanf("%d", &a);
    printf("Enter 2nd number: ");
    scanf("%d", &b);
    printf("Enter 3rd number: ");
    scanf("%d", &c);
    printf("Enter 4th number: ");
    scanf("%d", &d);
    printf("Enter 5th number: ");
    scanf("%d", &e);

    func(a,b,c,d,e,&sum,&avg,&std_dev);

    printf("The Sum: %f\n", sum);
    printf("The Average: %f\n", avg);
    printf("The Standard Deviation: %f", std_dev);

}

//func function
void func(int a, int b, int c, int d, int e, float *sum, float *avg, float *std_dev)
{
    *sum = a+b+c+d+e;
    *avg = *sum/5.0;

    //standard deviation
    *std_dev = sqrt (((a-*avg)*(a-*avg))+((b-*avg)*(b-*avg))+((c-*avg)*(c-*avg))+((d-*avg)*(d-*avg))+((e-*avg)*(e-*avg))/5.0);


}

Output:

Enter 1st number: 2
Enter 2nd number: 3
Enter 3rd number: 5
Enter 4th number: 5
Enter 5th number: 3
The Sum: 18.000000
The Average: 3.600000
The Standard Deviation: 2.629068

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.

View Comments (1)

  • why not to use call by refrence
    only just creating three variables in main sum,avg and stdinand remainaning variables in func variable may it reduces compilation time
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void func( int * ,int ,int *);
    void main()
    {
    int sum,avg,std;
    func(&sum,&avg,&std);
    printf("sum=%d\n Average=%d\n std=%d",);
    return 0;
    }
    void func (int *sum, int *avg, int *stdin)
    {
    int a,b,c,d,e;
    printf("enter the numbers");
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
    *sum=a+b+c+d+e/5;
    *avg=
    sum/5;
    stdin=sqrt((pow(a-avg),2)+(pow(b-avg),2)+(pow(c-avg),2)+(pow(d-avg),2)+(pow(e-avg),2));
    }

Related Post
Leave a Comment

This website uses cookies.