指针:找出数组中最大最小数

Posted on Posted in 程序方程式

编译环境为Debian

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
int max_min(int a[],int n,int *max,int *min);
int main(void)
{
    int N;
    printf("How number you want to know large or small?,please in put number: ");
    scanf("%d",&N);
    int b[N],i,big,small;
    printf("\nPlease input %d Num :  ",N);
    for(i=0;i<N;i++)
        scanf("%d",&b[i]);
    max_min(b,N,&big,&small);
    printf("\nLarge Number is : %d",big);
    printf("\nSmall Number is : %d \n ",small);
 
    return 0;
}
int max_min(int a[],int n,int *max,int *min)
{
    int i;
    *max = *min = a[0];//将a[0]的值赋给max min 
    for(i=0;i<n;i++)
    {
        if (a[i]>*max) 
            *max = a[i];//数组的值赋给max ,让MAX成为当前最大的数值
        else if (a[i]<*min)
            *min = a[i];
 
    }
}

发表评论