/* Finds the largest and smallest elements in an array */ #include #include /* We declara the function that will find the largest and smallest values */ void max_min(const int a[], int n, int *max, int *min); int main(void) { int i, big, small; int* array; /* declare a pointer only */ int size; /* ask user for size of array */ printf ("How large do you want your array? "); scanf ("%d", &size); /* allocate the array in the heap */ array = (int *) calloc (size, sizeof(int)); printf("Enter %d integer numbers: ", size); for (i = 0; i < size; i++) scanf("%d", &array[i]); max_min(array, size, &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]; } }