C lesson #2
home
teaching
research
social
Mary PW Chin
钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:
C lesson #1
Getting started
C practice #1
Printing stars
C lesson #2
Dice
C lesson #3
Algorithm design
C lesson #4
Circles
C lesson #5
More loops
C lesson #6
Pretty nets
❮
❯
Synopsis
random numbers and the 'rand' function;
conditionals: if-else if-else;
assignment vs comparison;
booleans 'and' vs 'or';
variable names: rules and conventions;
output to screen (standard output) vs output to file;
Linux commands: ls, wc, head, tail;
probability theory;
initialisation and counters.
Exercise
For the program on slide #19 mark out the:
variable names;
arguments;
functions;
keywords;
modules;
methods;
Repeat Exercise 1 for the program on slide #23.
Repeat Exercise 1 for the program on slide #36.
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)
?
Refer the program on slide #43. What happens if we change
else if
to
if
? Draw the revised flowchart.
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.
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.
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:
YEAR
MONTH
AGE
2018
1
__ years __ months
2018
2
2018
3
…
…
__
__
90 years 0 month
Write a program simulating the draw of Magnum 4D first prize.
Write a program simulating your favourite chance-based game.
C lesson #1
Getting started
C practice #1
Printing stars
C lesson #2
Dice
C lesson #3
Algorithm design
C lesson #4
Circles
C lesson #5
More loops
C lesson #6
Pretty nets
you might like to search for wonder, goodness, delight, surprise, voila… and more