|
CS256 - Fundamentals of Structured Programming
|
Displaying ./code/functions/min-max.c
#include <stdio.h>
int min(int a, int b)
{
if (a < b) return a;
return b;
}
int max(int a, int b)
{
if (a > b) return a;
return b;
}
// nums and n are passed by reference (they are pointers):
void read_integers(int nums[], int *n)
{
printf("Enter some integers:\n");
// The * operator next to n is the dereference operator, it gives us the
// "value" of the variable:
while(scanf("%d", &nums[*n]) == 1) {
*n++;
}
}
int main(void)
{
int nums[100];
int n;
read_integers(nums, &n);
int num_min, num_max;
num_min = num_max = nums[0];
for(int i = 1; i < n; i++) {
num_min = min(num_min, nums[i]);
num_max = max(num_max, nums[i]);
}
printf("Smallest number = %d\n", num_min);
printf(" Largest number = %d\n", num_max);
return 0;
}
|