Professional Documents
Culture Documents
C Programming Exam: Game of Life 25-04-2017, 8:45-10.30
C Programming Exam: Game of Life 25-04-2017, 8:45-10.30
In this interim exam you will develop a simulator for the Game of Life, invented by John Conway in 1970.
The game is played on a two-dimensional grid of cells. Normally it is infinite, but in this exam the grid will
only be 7 by 7 cells. Each cell is either alive or dead. Cells outside the 7x7 grid are always dead. Each cell
has eight neighbours. In the image below, the neighbours of the red cell are all those in the green box, except
the red cell itself.
(To be precise, the neighbours of cell (x,y) are (x-1,y-1), (x,y-1), (x+1,y-1), (x-1,y), (x+1,y), (x-1,y+1),
(x,y+1), (x+1,y+1).) Cell (0,0) is in the bottom left, and cell (6,6) is in the top right of the image.
Important
• Your grade is based on the number of cases passed. So try to complete the cases one by one.
• You can press “evaluate” as often as you like during the exam to evaluate your solution. We advise
you to do this regularly during the exam.
• The grade is based on your last submission. So make sure you submit a working version that completes
as many cases as possible.
1 / 22
Task 1.
• If you’re not using the USB stick, go to exam.oncourse.tue.nl on the TUE-WPA2 network.
• First, you will have to do the “C Programming (enable)” quiz. It contains a single ungraded question,
where you have to fill in the number of your USB stick (which you should have, even if you don’t use
it).
• Then go to the “C Programming: Game of Life” activity. You will see a screen similar to:
2 / 22
Select the “Edit” tab and the following screen will appear:
You can write your own C program in the text editor that is now shown in your browser. Once you press
“Save” you can “Run” and “Evaluate” your program. Using the “Run” command you will see a terminal
where you can provide input to your program. You can use this to debug your program. Using the “Evaluate”
command all test cases are evaluated and at the end the results are displayed (grade and errors if present).
3 / 22
Task 2. Create a variable called grid for the two-dimensional grid of 7 by 7 characters. It is most natural
to use a two-dimensional array of size 7 by 7 containing characters, but you are allowed to use other data
structures too (such as a one-dimensional array).
Task 3. Make a function resetGrid that sets the value of all cells in the grid to dead.
Task 4. Make a function printGrid that prints the grid. Print a ’.’ for a dead cell, and ’*’ for a live cell.
In the output below, the top-left character is cell (0,6); the top-right character is cell (6,6); the bottom-left
character is cell (0,0), and so on.
Task 5. Make a main function and call resetGrid and printGrid on the grid variable. Since all cells in the
grid are dead, you should see this output when executing:
.......
.......
.......
.......
.......
.......
.......
Task 6. Your program already supports multiple operations (i.e., reset grid and print grid). Adapt your main
function such that a person playing the Game of Life can select the desired operation. For now, your program
should support the following operations:
command operation
r reset the grid (set all cells to dead)
p print the grid
q quit program
Command: r
Command: p
.......
.......
.......
.......
.......
.......
.......
Command: q
In the remaining tasks you will also implement the following commands:
4 / 22
command operation
i input a cell
l count the number of live cells
n count the number of live neighbours of a cell
c print the grid, showing the number of neighbours of each cell
d print the longest distance between any two live cells on the grid
s compute the next generation, and update the grid
5 / 22
Task 7. Write a function insertCell that asks for cell coordinates (i.e. integers x and y) and inverts the
status of the cell (x,y) in the grid. If the coordinates are outside the 7x7 grid, the error message “Invalid
input!” must be printed, and the status of the grid must be unchanged.
Of course you will have to extend the set of operations supported by your program. You must use the
command ‘i’ for the insert operation. Given the inputs below, the output of your program should look like:
Command: r
Command: i 2 3
Command: i 3 3
Command: i 2 2
Command: i 3 2
Command: p
.......
.......
.......
..**...
..**...
.......
.......
Command: i 3 3
Command: p
.......
.......
.......
..*....
..**...
.......
.......
Command: q
(Recall that (0,0) is the lower-left corner of the grid.)
6 / 22
Task 8. Add a function liveCells that counts the number of live cells on the grid, and displays it with the
command ‘l’ (the letter l, not the digit 1). Given the inputs below, the output of your program should look like:
Command: r
Command: i 2 3
Command: i 3 3
Command: i 2 2
Command: p
.......
.......
.......
..**...
..*....
.......
.......
Command: l
The number of live cells is 3
Command: i 2 2
Command: p
.......
.......
.......
..**...
.......
.......
.......
Command: l
The number of live cells is 2
Command: q
7 / 22
Task 9. Add a function neighbours that computes the number of live neighbours of a cell, and displays
it with the command ‘n’. Recall that all cells outside the 7x7 grid are dead, but that they may have live
neighbours within the grid. Also add the command ’c’ that displays the number of neighbours for each cell
on the grid (there should be a number 0-8 instead of ’*’ and ’.’). The output of your program should then
look as follows:
Command: i 1 4
Command: i 2 3
Command: i 3 3
Command: i 2 2
Command: p
.......
.......
.*.....
..**...
..*....
.......
.......
Command: l
The number of live cells is 4
Command: n 2 1
Cell 2 1 has 1 live neighbours
Command: n 2 3
Cell 2 3 has 3 live neighbours
Command: n 3 3
Cell 3 3 has 2 live neighbours
Command: n 6 6
Cell 6 6 has 0 live neighbours
Command: c
0000000
1110000
1132100
1332100
0223100
0111000
0000000
Command: q
8 / 22
Task 10. Add a function longestDistance that computes the longest distance between any two live cells
on the grid, and returnspa double. If there are no live cells the longest distance is zero. The distance is the
Cartesian distance, i.e. √(x2 − x1 )2 + (y2 − y1 )2 . The distance of a cell to itself is therefore zero, and to a
diagonal neighbour it is 2 = 1.414214. Given the shown inputs, the output of your program should then
look as follows:
Command: i 1 1
Command: i 2 2
Command: p
.......
.......
.......
.......
..*....
.*.....
.......
Command: d
The longest distance between any two live cells is 1.414214
Command: i 5 5
Command: p
.......
.....*.
.......
.......
..*....
.*.....
.......
Command: d
The longest distance between any two live cells is 5.656854
Command: q
Hint. You should now be able to pass cases 0-7. The following tasks do not depend on the distance function.
So, if you do not get it to work, you can continue to try to get points for the following tasks, if you wish.
9 / 22
Task 11. Finally, add a function step that computes the next generation of the grid with the ’s’ command,
and updates the grid (i.e. the grid contains the next generation cells after calling the function). Given the
shown inputs, the output of your program should then look as follows:
Command: i 2 4
Command: i 2 5
Command: i 2 6
Command: i 1 4
Command: i 0 5
Command: p
..*....
*.*....
.**....
.......
.......
.......
.......
Command: s
Command: p
.*.....
..**...
.**....
.......
.......
.......
.......
Command: s
Command: p
..*....
...*...
.***...
.......
.......
.......
.......
Command: s
Command: s
Command: p
.......
...*...
.*.*...
..**...
.......
.......
.......
Command: q
Case 8 shows an oscillating population, called the toad. Case 9 shows a glider, which after 4 steps has moved
one position to the right and down. This is shown in the output above.
Submission: Your final solution must be submitted through OnCourse which will automatically grade this
submission. Note that only the last submission will be graded. The input and output of all test cases used
by OnCourse can be found on the next page(s).
10 / 22
Input / output test cases
Case 0
Input:
p
q
Output:
Command: .......
.......
.......
.......
.......
.......
.......
Command:
11 / 22
Case 1
Input:
r
i 2 3
i 3 3
i 2 2
i 3 2
p
i 3 3
p
q
Output:
Command: Command: Command: Command: Command: Command: .......
.......
.......
..**...
..**...
.......
.......
Command: Command: .......
.......
.......
..*....
..**...
.......
.......
Command:
12 / 22
Case 2
Input:
r
i 20 0
i 2 3
i 3 3
i 2 2
p
r
i 2 3
i 2 3
i 2 2
p
q
Output:
Command: Command: Invalid input!
Command: Command: Command: Command: .......
.......
.......
..**...
..*....
.......
.......
Command: Command: Command: Command: Command: .......
.......
.......
.......
..*....
.......
.......
Command:
13 / 22
Case 3
Input:
r
i 2 3
i 3 3
i 2 2
p
l
i 2 2
p
l
q
Output:
Command: Command: Command: Command: Command: .......
.......
.......
..**...
..*....
.......
.......
Command: The number of live cells is 3
Command: Command: .......
.......
.......
..**...
.......
.......
.......
Command: The number of live cells is 2
Command:
14 / 22
Case 4
Input:
r
i 20 0
i 1 4
i 2 3
i 3 3
i 2 2
i 0 6
p
l
n -1 19
n 0 6
n 1 6
n 6 6
n 2 1
n 2 3
n 3 3
q
Output:
Command: Command: Invalid input!
Command: Command: Command: Command: Command: Command: *......
.......
.*.....
..**...
..*....
.......
.......
Command: The number of live cells is 5
Command: Cell -1 19 has 0 live neighbours
Command: Cell 0 6 has 0 live neighbours
Command: Cell 1 6 has 1 live neighbours
Command: Cell 6 6 has 0 live neighbours
Command: Cell 2 1 has 1 live neighbours
Command: Cell 2 3 has 3 live neighbours
Command: Cell 3 3 has 2 live neighbours
Command:
15 / 22
Case 5
Input:
r
i 1 4
i 2 3
i 3 3
i 2 2
i 6 0
p
l
c
n 7 0
q
Output:
Command: Command: Command: Command: Command: Command: Command: .......
.......
.*.....
..**...
..*....
.......
......*
Command: The number of live cells is 5
Command: 0000000
1110000
1132100
1332100
0223100
0111011
0000010
Command: Cell 7 0 has 1 live neighbours
Command:
16 / 22
Case 6
Input:
d
i 2 3
d
i 2 4
d
i 2 5
d
i 3 4
d
i 4 4
d
i 6 0
d
p
q
Output:
Command: The longest distance between any two live cells is 0.000000
Command: Command: The longest distance between any two live cells is 0.000000
Command: Command: The longest distance between any two live cells is 1.000000
Command: Command: The longest distance between any two live cells is 2.000000
Command: Command: The longest distance between any two live cells is 2.000000
Command: Command: The longest distance between any two live cells is 2.236068
Command: Command: The longest distance between any two live cells is 6.403124
Command: .......
..*....
..***..
..*....
.......
.......
......*
Command:
17 / 22
Case 7
Input:
d
i 0 0
d
i 1 1
d
i 2 2
d
i 3 3
d
i 0 3
d
i 3 0
d
p
r
i 6 0
i 6 6
d
p
q
Output:
Command: The longest distance between any two live cells is 0.000000
Command: Command: The longest distance between any two live cells is 0.000000
Command: Command: The longest distance between any two live cells is 1.414214
Command: Command: The longest distance between any two live cells is 2.828427
Command: Command: The longest distance between any two live cells is 4.242641
Command: Command: The longest distance between any two live cells is 4.242641
Command: Command: The longest distance between any two live cells is 4.242641
Command: .......
.......
.......
*..*...
..*....
.*.....
*..*...
Command: Command: Command: Command: The longest distance between any two live cells is 6.000000
Command: ......*
.......
.......
.......
.......
.......
......*
Command:
18 / 22
Case 8
Input:
r
i 2 3
i 3 3
i 4 3
i 3 4
i 4 4
i 5 4
p
s
p
s
p
c
l
q
Output:
Command: Command: Command: Command: Command: Command: Command: Command: .......
.......
...***.
..***..
.......
.......
.......
Command: Command: .......
....*..
..*..*.
..*..*.
...*...
.......
.......
Command: Command: .......
.......
...***.
..***..
.......
.......
.......
Command: 0000000
0012321
0134421
0124431
0123210
0000000
0000000
Command: The number of live cells is 6
Command:
19 / 22
Case 9
Input:
i 2 4
i 2 5
i 2 6
i 1 4
i 0 5
p
s
p
s
p
s
p
s
p
s
s
s
s
p
s
s
s
s
p
c
l
q
20 / 22
Output:
Command: Command: Command: Command: Command: Command: ..*....
*.*....
.**....
.......
.......
.......
.......
Command: Command: .*.....
..**...
.**....
.......
.......
.......
.......
Command: Command: ..*....
...*...
.***...
.......
.......
.......
.......
Command: Command: .......
.*.*...
..**...
..*....
.......
.......
.......
Command: Command: .......
...*...
.*.*...
..**...
.......
.......
.......
Command: Command: Command: Command: Command: .......
.......
....*..
..*.*..
...**..
.......
.......
Command: Command: Command: Command: Command: .......
.......
.......
.....*.
...*.*.
....**.
.......
Command: 0000000
0000000
0000111
0011312
0011533
0012322
0001221
Command: The number of live cells is 5
Command:
21 / 22
22 / 22