当前位置:网站首页>Pointers: maximum, minimum, and average

Pointers: maximum, minimum, and average

2022-07-06 14:35:00 |Light|

requirement

Programming , Enter a one-dimensional integer array , Output the maximum value 、 Minimum and average .( Use a pointer to achieve )

Code

#include<stdio.h>
/* *  This function is used to input a one-dimensional integer array , The input data is stored in the formal parameter a Array  *  Input data in 0 As an end sign ,0 It is not stored in the array nor included in the total number of input data  *  The return value is the number of input data  */
int input(int a[])
{
    
    int n=0;
    int b = 0;
    do
    {
    
        scanf("%d",&b);
        if(b == 0)
        break;
        else
        {
    
            a[n] = b;
            n++;
        }
    }
    while(b != 0);
    return n;

}

/* *  This function is used to calculate the formal parameter array a Maximum of 、 minimum value 、 Average  *  Maximum 、 minimum value 、 The average value passes through the formal parameter pointer variable pmax、pmin、pavg To pass on  * n Is a formal parameter array a Number of data in  */
void fun(int a[],int *pmax,int *pmin,int *pavg,int n)
{
    
    int i,j,k=0;
    *pmax = a[0];
    *pmin = a[0];
    for(i=1;i<n;i++)
    {
    
        if(*pmin > a[i])
        {
    
            *pmin = a[i];
        }
        if(*pmax < a[i])
        {
    
            *pmax = a[i]; 
        }

        k = k + a[i];
        *pavg = (k + a[0])/n;
    }

}

main function

int main()
 {
    
    int a[200],n,max,min,avg;
    n=input(a);
    fun(a,&max,&min,&avg,n);
    printf(" The maximum value is %d, The minimum value is %d, The average value is %d\n",max,min,avg);
    return 0;
 }   

test

Test input
1 3 5 7 0
Output
The maximum value is 7, The minimum value is 1, The average value is 4

原网站

版权声明
本文为[|Light|]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918542504.html