Selecting the maximum value, minimum and average values of a set of numbers (the pointer)
Function interface definition:
float fun ( you're a [], you're n, you * max, you * min);
Where a
, n
, max
and min
are user parameters passed. Func a
array n
maximum, minimum and average values of the elements. Maximum and minimum respectively max
and min
back, the function returns the average.
Referee test program Example:
#include <stdio.h> float fun(int a[],int n,int *max,int *min); int main() { int x[10],i,m,n; float p; for(i=0;i<10;i++) scanf("%d",&x[i]); p=fun(x,10,&m,&n); printf("max=%d,min=%d,average=%.2f\n",m,n,p); return 0; } /*Please fill in the answer here * /
Sample input:
2 5 4 8 6 9 1 3 7 0
Sample output:
max=9,min=0,average=4.50
Code
float fun(int a[],int n,int *max,int *min) { int i; float sum = 0; *min=*max=a[0]; for(i=0;i<10;i++) { if(a[i]>*max){ *max = a[i]; } if(a[i]<*min){ *min = a[i]; } sum+=a[i]; } return sum/n; }