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:
- First we will ask user to input 5 integer values and will store all the values in an integer array.
- Since we are using a single function fan( ) to return multiple values to main , so we will use pointer to do so.
- In the function prototype , all parameters are pointers as :
- first parameter is for accessing values of array.
- 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( ) .
- 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.
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
Post a Comment