Week 03 Tutorial Questions
-
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 lowercase, with words separated by underscores.
-
#define
s names must be in uppercase. - Variable names must start with a letter.
- Variable names that contain vowels must be avoided.
- Variable names must 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^
-
MAX_LENGTH
-
_MEMLIMIT
-
return
-
-
What is a struct? How is it different from integers, doubles and characters?
-
We want to hold the information about a particular time in the day. We need to know the hour, minute and whether or not it is AM or PM.
- How do we define the struct?
- How do we declare and initialise it?
- How do we print the hour of the time?
- How do we print it in the format:
hh:mm [ap]m
?
Hint: For part d, you may want to try using
%02d
instead of%d
in aprintf
, and see what it does. -
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.
int i = 0; while (i < 10) { printf("hello\n"); i = i + 1; }
-
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;
-
Write a C program
count_up.c
which reads an integer n and then prints the integers 1..n, one per line.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
-
How could we build on our code from
count_up.c
to use an increment of the user's choosing?dcc -o count_inc count_inc.c ./count_inc Enter finish: 3 Enter increment: 2 1 3 ./count_inc Enter finish: 17 Enter increment: 5 1 6 11 16
-
Write a C program
Hint: if x is divisible by 7, then x % 7 == 0range7.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.dcc -o range7 range7.c ./range7 Enter start: 3 Enter finish: 49 7 14 21 28 35 42 49
-
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 *---- **--- ***-- ****- *****
-
Now modify so it prints the following pattern:
./bars Enter an integer: 9 -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*- -*-*-*-*-