This week your tutor will show you how it is to be done in future weeks.
Your tutor will nominate this week a lab pair to do next week's code review.
int i; while (i < 100) { printf("%d\n", i); i = i + 1; }
int i = 0; int j = 0; while (j = 1 || i < 100) { printf("%d\n", i); i = i + 1; }
int i = 0; int n = 10; while (i < n) { printf("%d\n", i); n = n + i; i = i + 1; }
int i = 0; while (i < 10) printf("%d\n", i); i = i + 1;
./asterisks Please enter an integer: 5 * * * * *
multiple_of_ten.c
which reads 2 integers and then prints all of the multiples of ten between those numbers.
./multiple_of_ten Enter start: 12 Enter finish: 42 20 30 40 ./multiple_of_ten Enter start: -1 Enter finish: 7 0
#include <stdio.h> int main(void) { printf("%d\n", 13 < 42); printf("%d\n", 13 < 42 || 42 > 50); printf("%d\n", 13 < 42 && 42 > 50); printf("%d\n", 13 && 42); printf("%d\n", 13 || 42); return 0; }Obviously your partner's code has to be completely rewritten, but first figure out what it will print and why?
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.
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?
Consider the following expressions:
(x == 4) && (y == 4)
(x == 4) || (y == 4)
!((x == 4) || (y == 4))
(!(x == 4)) && (!(y == 4))
What do each of the expressions evaluate to given the following values of x and y
Compare the evaluation of the last 2 expressions on the given inputs. What do you notice?
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
3 + 5 * 10 - 12
a > 5 || b < 3
a = b > c && b || d
!a || a && c
Figuring this out can be a bit tedious and prone to errors. The moral? In complex expressions make your intention explicit by using parentheses.
Note: The precedence rules of arithmetic operators in C follow the conventions used in mathematics.
int input_num, i; printf("Enter a number: "); scanf("%d", &input_num); i = input_num; if(i <= 5){ printf("%d\n", i * i); i++; }
int input_num, i; printf("Enter a number: "); scanf("%d", &input_num); i = input_num; while(i <= 5){ printf("%d\n", i * i); i++; }