Sort 3 Numbers using Functions
Last week you wrote a program to read in three numbers, sort them, and print
them out in ascending (non decreasing) order, one per line.
This week, you will write a program called sort3Functions.c
which does the
same thing, using several functions.
You are given a main
function and function prototypes to implement.
To get started, copy the program sort3Functions.c
from the course account to your
directory by typing (make sure you type the dot at the end):
$ cp /web/cs1511/17s2/week04/files/sort3Functions.c .
The first two functions you will implement work with finding the minimum and maximum of a pair of numbers. The function prototypes are:
int min2(int a, int b); int max2(int a, int b);
The min2
function must return the minimum (smallest) value out of a
and b
and the max2
function must return the maximum (largest) value out of a
and b
.
Next you will write three more functions, which work with three numbers instead of two:
int min3(int a, int b, int c); int mid3(int a, int b, int c); int max3(int a, int b, int c);
These functions, in order, return the minimum, middle, and maximum value out of a
, b
,
and c
. These three functions must call the min2
and/or max2
functions
which you wrote earlier (as appropriate).
Some Examples
Enter 3 numbers: 8 5 9 5 8 9
Enter 3 numbers: 0 3 5 0 3 5
Enter 3 numbers: -1 -2 -3 -3 -2 -1
Enter 3 numbers: 9 8 9 8 9 9
Enter 3 numbers: 3 2 1 1 2 3
To run some simple automated tests:
$ 1511 autotest sort3Functions
To run Styl-o-matic:
$ 1511 stylomatic sort3Functions.c Looks good!
You’ll get advice if you need to make changes to your code.
Submit your work with the give command, like so:
$ give cs1511 wk04_sort3Functions
Or, if you are working from home, upload the relevant file(s) to the wk04_sort3Functions activity on Give Online.