Back to main

Lab 2 for C programming

Exercises:

Exercise 5: if conditionals

Background

The logic of an if statement is fairly simple. If the statement is true, the code block (indicated by lines indented with spaces or a tab) is run. A closely relared command is the if ... else, which acts precisely as you would expect.

Here is a common problem.

x = 1 and x == 1 do completely different things:

Technical details

Conditional:

x = 3

if x == 4:
    # this line will not be printed
    print "x is equal to 4"

# one of these two lines will be printed
if (x > 10) or (1 != 0):
    print "Expression is true"
else:
    print "Expression is false"

The logical operators (not, and, or) are particularly useful with if. Occasionally xor is useful. Parentheses () are highly encouraged when using logical operators.

Generating a random number:

# extra functionality!
import random

# this gets a random number
# between 0 and 0.999999999999
number = random.random()

print "Random number is:", number
random.random() requires an extra import statement!

Your task...

Create a "guessing game" program. Your program should:


(optional: instead of guessing an int, ask players to guess a float. Then, instead of trying to guess exactly the right number, the players win if they guess a number within 0.1 of the correct value.)

... show your work to a demonstrator


Exercise 6: While loops

Background

The simplest loops in C are while loops. These loops are very similar to if statments. The difference is that there is not else statement included in a while loop, and the code block will be repeated as long as the statement is true.

To stop an infinite loop, use ctrl-c

Technical details

Infinite loops are fun:

while True:
    print "Infinite loop is infinite"

Non-infinite loops are more useful:

i = 0
while i < 10:
    print "The loop has repeated", i, "times."
    i = i + 1

Your task...

Save your guessing game from Exercise 5 with a new file name; we will be modifying it, but you don't want to lose all your hard work.

Modify your guessing game:


(optional: randomly change the rules -- instead of always going from 1 to 32, let the computer randomly decide to do 100 to 131, -97 to -66, etc. If you change the size of the range, you can't compare high scores for different games. However, you could program the computer to randomly select "all numbers divisible by 4 between 32 and 156" and the like.)

... show your work to a demonstrator


Exercise 7: For loops

Background

A loop that counts up (or down) is the most common type of loop, so most programming languages have a short-cut: the for loop.

In python, the for loop operates on a list of values. We do not use the silly conditionals seen in C for loops.

Technical details

The most common form of loop is this:

for i in range(10):
    print "The loop has repeated", i, "times."

Here's two weirder loops:

for i in range(5, 15):
    print "What is this loop doing?  i is", i

for i in range(20, 2, -2):
    print "What is this loop doing?  i is", i

Your task...

Back in the good old days, there were no fancy graphics in computer games; we used text to represent everything. Your task is to draw a room from a family of games called Roguelikes -- the player (represented by the @ symbol) must explore a dungeon.

2x2 room, player at 0,1:
+--+
|..|
|@.|
+--+

5x3 room, player at 1,2:
+-----+
|.....|
|.....|
|.@...|
+-----+

14x8 room, player at 8,5:
+--------------+
|..............|
|..............|
|..............|
|..............|
|..............|
|........@.....|
|..............|
|..............|
+--------------+

Your task is to write a function (with extra "helper" functions) to draw such a room.


(optional: combine this exercise with keyboard input -- let the player move around in the room, bump into walls, etc. Ask the user to turn on the numlock key and to press enter after every move, then you can read his moves by reading ints from the keyboard. Use a while loop for this movement.)

... show your work to a demonstrator

Move on to Lab 3


Creative Commons LicenseUnless otherwise noted, all materials on these pages are licenced under a Creative Commons Licence.