Week 03 Tutorial Questions
-
What is a While Loop?
What are the three most important things that should happen in (almost) every while loop?
Write a while loop that prints out "hello" ten times.
-
Discuss the errors in these while loops
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;
-
The following statements are either: Rules of the C language, Style Rules, or Not rules. Decide as a class which are which.
- Variable names must start with either a letter or an underscore.
- Variable names must be shorter than 8 characters.
- Variable names can contain numbers, as long as they aren't the first character.
- Variable names must be lower case, with words seperated by underscores.
-
#define
s names must be in upper-case - Variable names must start with a letter.
- Variable names that contain vowels must be avoided.
- Variable names be relevant and descriptive.
-
Which of the following are valid variable names in C?
If they are valid, would they be a good name?
-
THX1138
-
2for1
-
mr_bean
-
my space
-
event_counter
-
^oo^
-
_MEMLIMIT
-
return
-
-
Write a C program
count_up.c
which reads an integer n and then prints the integers 1..n, one per line.Your tutor may do this exercise as a competition or game -- your tutor will try to make as many mistakes as possible while coding this exercise, and you should try and correct them. If this is a competition, your class might get split up into groups, and compete to find the most errors.
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
-
Write a program that reads in an integer and prints out that many
asterisks, each on a new line.
./asterisks Please enter an integer: 5 * * * * *
How would you convert it to printing out a square rather than a line?
-
The square program written above might behave like this:
./square Enter size: 5 ***** ***** ***** ***** *****
Modify the program so that it prints out a triangle like this:./triangle Enter number: 5 ----* ---** --*** -**** *****
./diagonal Enter an integer: 10 *--------- -*-------- --*------- ---*------ ----*----- -----*---- ------*--- -------*-- --------*- ---------*
Now modify so it prints the following pattern:./bars Enter an integer: 9 -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*-
Revision questions
The remaining tutorial questions are primarily intended for revision - either this week or later in session.
Your tutor may still choose to cover some of the questions time permitting.
-
Write a C program
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
-
Write a C program
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
-
Write a C program
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
-
Write a program that reads in an integer and prints out that many
asterisks, each on a new line.
./asterisks Please enter an integer: 5 * * * * *
-
Consider the following program square.c
The output if the user types in the number 5 is:
#include <stdio.h> int main(void) { int number; int row, column; // Obtain input printf("Enter size: "); scanf("%d", &number); row = 1; while (row <= number) { column = 1; while (column <= number) { printf("*"); column = column + 1; } printf("\n"); row = row + 1; } return 0; }
./square Enter size: 5 ***** ***** ***** ***** *****
Modify the program so that it prints out a triangle like this:./triangle Enter number: 5 ----* ---** --*** -**** *****
./diagonal Enter an integer: 10 *--------- -*-------- --*------- ---*------ ----*----- -----*---- ------*--- -------*-- --------*- ---------*
Now modify so it prints the following pattern:./bars Enter an integer: 9 -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*-
-
Write a program that reads two integers (height x length) and prints a rectangular, asterisk outline
with the specified dimensions, for example:
./rectangle Enter rectangle height and length: 3 5 ***** * * *****
- For the program above consider and discuss the ways in which you would test their correct behaviour. Then extend the program to include error checking for, and handling of, invalid input.
-
Design and write a program that reads an integer n and prints a diamond asterisk outline
with side length n, for example:
./diamond Enter side length: 3 * * * * * * * * ./diamond Enter side length: 6 * * * * * * * * * * * * * * * * * * * *
- Write a program (a small game), which generates a random number for the program-user to guess. For example:
./guess_number Random number is between 1 and 100. Enter your guess: 50 Random number is between 1 and 50. Enter your guess: 40 Random number is between 1 and 40. Enter your guess: 37 Yay, you guessed the number 37 correctly! ./guess_number Random number is between 1 and 100. Enter your guess: 80 Random number is between 80 and 100. Enter your guess: 90 Random number is between 80 and 90. Enter your guess: 85 Random number is between 80 and 85. Enter your guess: 83 Random number is between 80 and 83. Enter your guess: 82 Yay, you guessed the number 82 correctly!
-
What does precedence of an operator mean?
Make the precedence explicit in the following expressions by adding parentheses:
3 + 5 * 10 - 12
a > 5 || b < 3
a = b > c && b || d
!a || a && c
While Loops
Variable Naming
A common phrase in Computer Science asserts that: "there are only two hard problems in Computer Science: Cache Invalidation, and Naming Things". In this section, we will discuss the problem of naming variables, and how to name variables to make your program better.
Programming Exercise
Two-Dimensional Loops
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++; }
What would be the output of the following code?
int x = -9; int y = 0; while (x != 0){ y = y - 1; x = x + 1; } printf("%d\n", x); printf("%d\n",y);
What would be the output of the following code?
int i = -7; int j = 0; while (i != 0){ j = j - i; i = i + 1; } printf("%d\n", i); printf("%d\n",j);
count42.c
which prints the integers 1 through to 42, one per line. For example:
dcc -o count42 count42.c ./count42 1 2 3 . . . 41 42