This week your tutor will show you how it is to be done in future weeks.
Your tutor will nominate a lab pair this week to do next week's code review.
How a code review works:
If they are valid, would they be a good name?
THX1138
2for1
mr_bean
my space
event_counter
^oo^
_MEMLIMIT
return
C is a typed language. What does this mean? Why is this useful?
cm2feet.c
which reads a height in
centimetres and prints it in feet.
Reminder: there 2.54 centimetres in an inch and 12 inches in a foot.
Use only int variables.
Your program should behave like this:
./cm2feet Enter your height in centimetres: 183 Your height in feet is 6
Would double variables have been a better choice?
count42.c
which prints the integers
1..42, one per line. For example:
dcc -o count42 count42.c ./count42 1 2 3
count_up.c
which reads an integer
n and then prints the integers 1..n, one per line. For
example:
dcc -o count_up count_up.c ./count_up Enter finish: 3 1 2 3 ./count_up Enter finish: 7 1 2 3 4 5 6 7
range.c
which reads integers
n and m and then prints the integers n..m,
one per line. For example:
dcc -o range range.c ./range Enter start: 3 Enter finish: 7 3 4 5 6 7
range7.c
which reads 2 integers
n and m, and then prints the integers between
n and m (including n and m) which are
divisible by 7.
Hint: if x is divisible by 7, then x % 7 == 0
dcc -o range7 range7.c ./range7 Enter start: 3 Enter finish: 49 7 14 21 28 35 42 49
range_divisible.c
which reads 3
integers n, m and x then prints the integers
between n and m (including n and m) which
are divisible by x.
dcc -o range_divisible range_divisible.c ./range_divisible Enter start: 20 Enter finish: 100 Enter divisor: 13 26 39 52 65 78 91 ./range_divisible Enter start: 80 Enter finish: 120 Enter divisor: 5 80 85 90 95 100 105 110 115 120
Your tutor may still choose to cover some of the questions time permitting.
rectangle_area.c
from last week so that it reads
floating-point (decimal) numbers and prints the area as a
floating-point number.
./rectangle_area Please enter rectangle length: 3.14159 Please enter rectangle width: 2.71828 Area = 8.539721Note carefully the changes.
What is output by the following C program? Why? Make sure you compile the program and run it to confirm your answer.
#include <stdio.h> #define FIRST_NUMBER 10 #define SECOND_NUMBER 20 #define TOTAL FIRST_NUMBER + SECOND_NUMBER #define AVERAGE TOTAL / 2 int main(void) { printf("The average of %d and %d is %d\n", FIRST_NUMBER, SECOND_NUMBER, AVERAGE); return 0; }
Determine the value of each expression and sub-expression:
1 / 2 * 500
1 / 2.0 * 500
(17 / 5) * 5 + (17 % 5)
(12 - 17) % 6 - 4
decompose.c
that prompts the user to
enter an integer, reads it from the input and prints out the number in
individual digits. Allow the program to work for input numbers up to 5
digits, i.e. up to 99999. You should be able to write this program
using some divisions and remainder (modulo '%') operations,
if
statements and simple comparisons.
Your program should behave as follows:
./decompose Please enter an integer: 25 You entered 25 which is decomposed: 2 5 ./decompose Please enter an integer: 2825 You entered 2825 which is decomposed: 2 8 2 5 ./decompose Please enter an integer: 2 You entered 2 which is decomposed: 2
Your program should handle all integers in range (0 to 99999).
Hint: use if
, divide (/
) and mod
(%
).
Discuss the concept of short-circuit evaluation for the C
logical operators ||
and &&
. Give
examples. Why is this feature useful?