# C lesson #2

Mary PW Chin 钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:

Getting started

Printing stars

Dice

Algorithm design

Circles

More loops

#### C lesson #6

Pretty nets                                               Synopsis
1. random numbers and the 'rand' function;
2. conditionals: if-else if-else;
3. assignment vs comparison;
4. booleans 'and' vs 'or';
5. variable names: rules and conventions;
6. output to screen (standard output) vs output to file;
7. Linux commands: ls, wc, head, tail;
8. probability theory;
9. initialisation and counters.

Exercise
1. For the program on slide #19 mark out the:
• variable names;
• arguments;
• functions;
• keywords;
• modules;
• methods;
2. Repeat Exercise 1 for the program on slide #23.
3. Repeat Exercise 1 for the program on slide #36.
4. Refer to the program on slide #36. What difference would you expect if we change
for (n=0; n<so_many_times; n++) to
• for (n=-100; n<so_many_times-100; n++);
• for (n=so_many_times+1000; n>1000; n--);
• for (n=0; n<so_many_times*1000; n+=1000)?
5. Refer the program on slide #43. What happens if we change else if to if? Draw the revised flowchart.
6. Write a program to generate two random numbers between 0 and 100. Print the larger number, and the sum, difference and product of the two numbers.
7. Write a program to generate a random number between 0 and 10. Take the number as the radius of a circle. Print the diameter, circumference and area of this circle.
8. Write a program to generate a random number 1930 ≤ y ≤ 2017, take this as somebody's year of birth. Generate another random number 1 ≤ m ≤ 12, let this be his month of birth. We need to know his age in the coming years until he reaches 90. Print your output in three columns:
YEARMONTHAGE
20181__ years __ months
20182
20183
____90 years 0 month

9. Write a program simulating the draw of Magnum 4D first prize.
10. Write a program simulating your favourite chance-based game.

Getting started

Printing stars

Dice

Algorithm design

Circles

More loops

Pretty nets