/********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* maxmin.c (Chapter 11, page 250) */ /* Finds the largest and smallest elements in an array */ #include #define N 5 #define LENGTH (sizeof(b) / sizeof( b[0]) ) /* Declare the function. Tell compiler the array should not be changed. */ void max_min(const int a[], int n, int *max, int *min); int main(void) { int b[N], i, big, small; printf("Enter %d numbers: ", N); for (i = 0; i < N; i++) scanf("%d", &b[i]); max_min(b, N, &big, &small); /* Alternative verion is also fine: max_min(b, LENGTH, &big, &small); */ printf("Largest: %d\n", big); printf("Smallest: %d\n", small); return 0; } void max_min(const int a[], int n, int *max, int *min) { int i; /* To begin, we initialize max and min value to the 0-th element in array */ *max = *min = a[0]; for (i = 1; i < n; i++) { if (a[i] > *max) *max = a[i]; else if (a[i] < *min) *min = a[i]; } }