DAY10
Monday, 04 June 2012
Today we will:
-
look again at the scanf() function for getting input from the user;
-
look again at how a pair of for loops can be nested, and why
you want to do that;
-
say a little more about the rules of for loops, another way to think
about them, and also how they can have "local" memory;
-
introduce the while and do loops, which repeat some
statements until a condition is met;
-
look at the break and continue statements for
changing the way a loop is carried out (the book warns you not to
use these. I disagree!);
Programs and functions we might talk about:
-
after_loop.c, a program which asks, what is the value
of the loop index after the loop is done?
-
bran.c, a function which returns a random boolean value,
either true or false;
-
dot_product.c, a program which shows how a for loop
can be used to compute the dot product of two vectors (although we
don't know what vectors are yet!). This program needs to be run
with dran01.
-
dran01.c, a function which returns a random double between 0 and 1.
-
f10.c, a function to use with find_min.
-
factors.c, a program which uses nested for loops to compute
and display the factors of each number from 2 to n.
-
find_min.c, a program which uses a loop to sample a function,
looking for its minimum value.
-
head_count.c, a program which simulates
flipping a (fair) coin n times, and counts the heads.
-
matrix_multiply.c, a program which
uses double for loops to multiply a matrix times a vector.
-
scanf_demo.c, a program which demonstrates how the scanf(format,address)
function can be used to read data from the user.
-
scientific.c, a program which uses while loops
to rewrite a large or small real number in scientific notation.
-
vegas.c, a program which lets you specify how much
money you start with, and then lets you bet repeatedly on a flip of a coin.
The program illustrates the while and do/while statements as it checks that
you still have money to play with, and that you didn't bet more than you have.
This program needs to be run with bran.
Last revised on 04 June 2012.