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 result in main( ).

Q. 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 result in main( ).

In the question ,the marked words shows that we have to make a single user defined function instead of making three separate function  that can satisfy all the demands made by the given question. 

NOTE:
  1. First we will ask user to input 5 integer values and will store all the values in an integer array. 
  2. Since we are using a single function fan( )  to return multiple values to main , so  we will use pointer to do so.
  3. In the function prototype , all parameters are pointers as :
    1. first parameter  is for accessing values of array.
    2. second , third and fourth parameters are for storing the addresses of the variables present in main( ) , which will be further initialized with the value of sum ,average and standard deviation respectively in the fan( ) .
    3. Since the question is asking to input integer values , so the sum will be always have integer value. But average and standard deviation may have decimals values.So we are using float pointer  for storing average and standard deviation and int datatype for sum.
main( ):-


fan( ) :-


copy here :-

#include<stdio.h>
#include<math.h>
void fan(int *,int *,float *,float *);
int main()
{
int a[5];
int p;
float q,d;
int x =0;
printf("\nEnter the Values: \n");
while(x<5)
{
scanf("%d",&a[x]);
x++;
};
fan(a,&p,&q,&d);
printf("\nsum: %d",p);
printf("\nperc: %g %",q);
printf("\nstand_deviation: %g",d);
return 0;
}
void fan(int *a,int *p,float *q,float *r)
{
int x=0;
int y=0;
float b[5];
float z,sd=0;
while(x<5)
{
y = y + *a;
a++;
x++; 
}
--a;--a;--a;--a;--a;
*p=y;/*<---this is for sum*/
z = (float)y/5;
*q=z;/*<---this is for percentage*/
for(x=0;x<5;x++)
{
b[x]= pow((*a - z),2);
a++;
};
for(x=0;x<5;x++)
{
sd = sd + b[x];
}
*r=sqrt(sd/5);/*<---this is for standard deviation */
}

Comments

Popular posts from this blog

Write a function that receives marks received by a student in 3 subject and return the average and percentage of these marks. Call this function from main( ) and print the results in main( ).

A positive integer is entered through the keyboard , write a program to obtain the prime factors of the number . Modify the function suitably to obtain the prime factors recursively .