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?
fahrenheit2celsius.c
which reads a US temperature in Fahrenheit
and prints it in Celsius . Reminder:
Celsius = 5/9 (Fahrenheit - 32)
Your tutor may still choose to cover some of the questions time permitting.
rectangle_area.c
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?