Assignment 5 — Pointers, and functions that change your variables
Four small programs where a function is handed the address of a variable instead of its value — the swap from Assignment 3 that could not work, working, and functions that hand back more than one answer.
Due Oct 7, 2026
A3’s swap.c compiled clean and swapped nothing, and it ended: the tool that fixes it is week 9. It is week 6, and here it is. A function that is given the address of a variable can change the variable itself, not a copy of it — which is also how one function can hand back two answers.
The reading for the week is [DIS] 2.2 — C’s Pointer Variables and [DIS] 2.3 — Pointers and Functions. Quiz 5 on Tuesday is on it.
You will hand in five files: order.c, minmax.c, divide.c, readnum.c and notes.txt — and calc.c, if you do the optional step 6.
order.c first
Every other program here makes the same move it does — pass an address, then change what is at it — so it is the one that makes the rest quick.
Every program compiles with gcc -Wall and no warnings. Every function you write gets the design recipe’s one-line comment above it. Every program also gets at least one assert of your own, beside the ones given.
order.c— a swap that decides. Writevoid order(int * a, int * b). If the value atais larger than the value atb, swap them, so the smaller one always ends up in the first variable. Then, in the same file, writevoid sort3(int * a, int * b, int * c), which puts three variables in increasing order by callingorderthree times. Yourmain:int main() { int x = 8; int y = 3; order(&x, &y); assert(x == 3); assert(y == 8); order(&x, &y); assert(x == 3); assert(y == 8); int p = 9; int q = 2; int r = 5; sort3(&p, &q, &r); assert(p == 2); assert(q == 5); assert(r == 9); printf("all examples passed\n"); }Done when it prints
all examples passed.minmax.c— two answers from one function. Writevoid min_max(int arr[], int n, int * min, int * max), which stores the smallest of thenvalues in*minand the largest in*max. Yourmain:#define SIZE 4 int main() { int values[SIZE] = {4, -2, 9, 0}; int low; int high; min_max(values, SIZE, &low, &high); assert(low == -2); assert(high == 9); int cold[SIZE] = {-5, -1, -9, -3}; min_max(cold, SIZE, &low, &high); assert(low == -9); assert(high == -1); printf("all examples passed\n"); }Done when it prints
all examples passed. The second array is all negative on purpose: it is the case a function that starts from0gets wrong.divide.c— an answer, and whether there is one. Writeint divide(int a, int b, int * quotient, int * remainder). Whenbis0it returns0and leaves both variables alone. Otherwise it storesa / bin*quotientanda % bin*remainder, and returns1. Yourmain:int main() { int q = 0; int r = 0; assert(divide(17, 5, &q, &r) == 1); assert(q == 3); assert(r == 2); assert(divide(7, 0, &q, &r) == 0); assert(q == 3); assert(r == 2); printf("all examples passed\n"); }Done when it prints
all examples passed. The last two asserts check that dividing by zero left the earlier answers where they were.readnum.c—scanf, from the inside. Writeint read_number(int * out), which reads one number withscanfand puts it in the variableoutpoints to. It returns1if it read a number and0if it could not. You need one fact for this:scanfreturns how many values it read, so1means it got the number. Yourmainreads numbers until there are none left:int main() { int n; int count = 0; int total = 0; while (read_number(&n)) { count = count + 1; total = total + n; } printf("%d numbers, total %d\n", count, total); }Make a file
numbers.txtholding4 8 15 16 23 42, then run./readnum < numbers.txtThe
<hands the file to your program as if you had typed it. Done when it prints6 numbers, total 108.notes.txt— three short answers.- A3’s
swapcompiled clean and changed nothing. In two or three sentences: what did it actually swap, and what does the swap inside yourorderhave that it did not? - Inside
sort3the calls areorder(a, b), with no&. Why not? Then put an&in front of each, compile, and paste the first linegccprints. - Every
scanfyou had written before this week had an&. The one inread_numberhas none. Why?
- A3’s
calc.c— optional: finish the calculator. This step is optional and not needed for full marks. Finish the calculator from Class 12, every stage on that page:*and/before+and-, the two error messages, and reading expressions until there are none left. Make a fileexprs.txtholding these seven lines:12+34 12+34-5+100 2+3*4 10-2*3 100/7*7 7/0 12+then run
./calc < exprs.txtDone when it prints these seven lines:
46 141 14 4 98 cannot divide by zero missing number