pta examples

 

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, maxand minare user parameters passed. Func aarray nmaximum, minimum and average values of the elements. Maximum and minimum respectively maxand minback, 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;
}

 

Guess you like

Origin www.cnblogs.com/gsx1529/p/12408763.html