taeyounkim LOG

pointer practice 2 (program to find the largest and smallest elements in an array) 본문

Career&Study/Coding Practice

pointer practice 2 (program to find the largest and smallest elements in an array)

taeyounkim 2021. 7. 7. 22:23
728x90

#include <stdio.h>
#define NUM 10

void max_min(int a[], int n, int *, int *); /*declare prototype of function*/

int main()
{
    int array[NUM];
    int big, small;
    
    printf("Enter 10 numbers: ");
    for(int i=0; i<NUM; i++){
        scanf("%d", &array[i]); /*read the value by scanf, and save it into array[i]*/
    }
    
    max_min(array, NUM, &big, &small); /*max points to big, min points to small*/
    
    printf("Maximum number is: %d\n", big);
    printf("Minimum number is: %d", small);
    
    return 0;
}

void max_min(int a[], int n, int *max, int *min) /*compare the numbers and sort out the max/min number*/
{
    *max = *min = a[0];
    
    for(int i=1; i<n; i++){
        if (*max < a[i])
            *max = a[i];
        
        else if(*min > a[i])
            *min = a[i];
    }
}

728x90