Programming Fundamentals
Information
- This page contains extra challenge exercises for week 05.
- These exercises are not compulsory, nor do they provide any marks in the course.
- These exercises are intended for students that want more challenge in the course.
- You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).
Exercise
(●●●)
:
Pointer Path
Download pointer_path.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity pointer_path
Your task is to add code to these functions in pointer_path.c:
// Prints the index-path from the given `start` index. Starts in the `pointers`
// array and bounces between pointers->indices->pointers->indices...
void print_pointer_path(
int *pointers[MAX_SIZE],
int indices[MAX_SIZE],
int start
) {
// TODO: Complete this function
}
As a quick initial remark, the syntax int *pointers[MAX_SIZE]
can be read as "pointers
is an array of length MAX_SIZE
where each element
is a pointer to an integer"
Now, the next thing to understand is that every integer we store in a variable has a memory address. This means that all integers in an array have individual memory addresses.
Using this idea, we once again consider the two arrays in the function above:
int *pointers[MAX_SIZE]
is an array ofint *
int indices[MAX_SIZE]
is an array ofint
This means that elements of pointers
can point to addresses of elements in
indices
!
Likewise, elements of indices
can store the indices of elements in
pointers
, and so we can have a two-way connection.
For this exercise, inputs will be taken for both arrays such that all elements
in pointers
will point to the addresses of elements in indices
.
Conversely, all elements in indices
will store an index into elements of
pointers
.
From here, you are to print the path from a starting index that is taken by bouncing between the two arrays until you are back at the starting index
Consider example input for the program below.
dcc pointer_path.c -o pointer_path ./pointer_path Please enter which indices the pointer array will reference: 0 5 1 6 8 9 1 1 0 3 Please enter which indices the index array will reference: 5 5 3 2 0 8 1 6 7 9 What index will the path start at? 2 Pointer path: 2->1->5->9->9->3->end
In the initial 10 indices input, each number represents which index in the 'indices' array that an element in the 'pointers' array will reference.
For example:
pointers[0]
will reference&indices[0]
pointers[1]
will reference&indices[5]
pointers[2]
will reference&indices[1]
pointers[3]
will reference&indices[6]
- And so on for all 10 indices...
Now, the next 10 indices act the same but in the reverse direction,
indices[0]
will indexpointers[5]
indices[1]
will indexpointers[5]
indices[2]
will indexpointers[3]
indices[3]
will indexpointers[2]
A full diagram has been provided for these relations below. The top array
represents pointers
and the bottom array represents indices
. Do not try
to understand it all at once as it can look quite messy. Simply pick an index
and follow the outgoing line to see single relations.
Now, in the example, 2
was chosen as the start of the path. The diagram has
been updated to show how the path is formed from this. Follow the red path from
index 2
in the pointers
array.
The path printed in the example above follows this:
- Starts at index
2
ofpointers
- Moves to index
1
ofindices
- Moves to index
5
ofpointers
- Moves to index
9
ofindices
- Moves to index
9
ofpointers
- Moves to index
3
ofindices
- Index
3
of indices contains index2
to pointers, meaning we have reached the start again, so we stop
Examples
dcc pointer_path.c -o pointer_path ./pointer_path Please enter which indices the pointer array will reference: 0 0 0 5 0 0 0 2 0 0 Please enter which indices the index array will reference: 0 0 3 0 0 7 0 0 0 0 What index will the path start at? 3 Pointer path: 3->5->7->2->end ./pointer_path Please enter which indices the pointer array will reference: 5 3 1 9 9 5 4 2 8 0 Please enter which indices the index array will reference: 3 8 4 5 1 9 6 0 9 4 What index will the path start at? 4 Pointer path: 4->9->end ./pointer_path Please enter which indices the pointer array will reference: 0 1 2 3 4 5 6 7 8 9 Please enter which indices the index array will reference: 1 2 3 4 5 6 7 8 9 0 What index will the path start at? 0 Pointer path: 0->0->1->1->2->2->3->3->4->4->5->5->6->6->7->7->8->8->9->9->end
Assumptions/Restrictions/Clarifications
- All arrays will be of size
MAX_SIZE
- You will never have to deal with paths that don't make it back to the start index
- All input indices will be valid
- All elements in
pointers
will point to addresses of elements inindices
- You can get the address of an element of
indices
by using&indices[i]
wherei
is any index
pointer_path.c
// Program that uses an array of pointers to have each index reference
// another index. Program finds the path taken from a starting index.
//
// Written by Rory Golledge (z5308772), 12-03-2022
//
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
void print_pointer_path(
int *pointers[MAX_SIZE],
int indices[MAX_SIZE],
int start
);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(void) {
int *pointers[MAX_SIZE];
int indices[MAX_SIZE];
int index;
printf("Please enter which indices the pointer array will reference:\n");
int count = 0;
while (count < MAX_SIZE && scanf("%d", &index) == 1) {
pointers[count] = &indices[index];
count++;
}
printf("Please enter which indices the index array will reference:\n");
count = 0;
while (count < MAX_SIZE && scanf("%d", &index) == 1) {
indices[count] = index;
count++;
}
int start;
printf("What index will the path start at? ");
scanf("%d", &start);
print_pointer_path(pointers, indices, start);
}
// Prints the index-path from the given `start` index. Starts in the `pointers`
// array and bounces between pointers->indices->pointers->indices...
void print_pointer_path(
int *pointers[MAX_SIZE],
int indices[MAX_SIZE],
int start
) {
// Setup all relevant loop variables
int count = 0;
int current_pointer_index = start;
int current_indices_index = -1;
int finish = 0;
// Print out start index before entering loop
printf("Pointer path: ");
while (finish == 0) {
// Even count means we are in the pointer array, odd count means we
// are in index array.
if (count % 2 == 0) {
printf("%d->", current_pointer_index);
int i = 0;
// When we branch from the pointers array to the index array, we
// need to find which `indices` index that
// `pointers[current_pointer_index]` is referring to.
while (&indices[i] != pointers[current_pointer_index]) {
i++;
}
current_indices_index = i;
} else {
printf("%d->", current_indices_index);
current_pointer_index = indices[current_indices_index];
// Once we have made it back to the start index, set the finish
// flag.
if (current_pointer_index == start) {
finish = 1;
}
}
count++;
}
printf("end\n");
}
Exercise
(●●●)
:
Math with Function Pointers
Download function_pointers.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity function_pointers
Your task is to add code to these functions in function_pointers.c:
// Given an `operation`, return a pointer to the function that can perform
// that operation.
//
// E.g. if addition is the operation, a pointer to the `addition` function
// should be returned.
double (*get_operation_function(char operation))(double, double) {
// TODO: COMPLETE THIS FUNCTION & CHANGE THIS RETURN
return NULL;
}
// Performs an operation given various amounts of information.
void perform_operation(
double first,
double second,
double (*operation_function)(double, double)
) {
// TODO: COMPLETE THIS FUNCTION
}
For this exercise you will be exploring a new topic called Function Pointers. It is important to note that this is outside the scope of COMP1511 content, but is an interesting topic to look at.
Just like how we can store pointers to integers and doubles, we can actually store pointers to functions! This can be useful as we can also execute these functions as a result. This means that a single variable can reference and execute one function at a certain point in the program, and another function at another point!
Here is a basic program containing function pointers:
#include <stdio.h>
void add(int a, int b) {
printf("%d + %d = %d\n", a, b, a + b);
}
void subtract(int a, int b) {
printf("%d - %d = %d\n", a, b, a - b);
}
int main(void) {
// Create a new variable `function_pointer` that points at the `add` function
void (*function_pointer)(int, int) = &add;
// Perform the `add` operation
(*function_pointer)(10, 5);
// Now, set the function pointer to point at the `subtract` function
function_pointer = &subtract;
// Call the function again, but this time, subtraction occurs!
(*function_pointer)(10, 5);
return 0;
}
The output of this program is:
10 + 5 = 15 10 - 5 = 5
The biggest challenge with function pointers is often the syntax of it. Here
is a diagram that explains every section of the function_pointer
variable
declaration.
It can also be seen from this that the function_pointer
variable can only
point at functions which take in two integers and return nothing.
Now, since function pointers have "types", it should be evident that they can also be used in both input and output of functions!
In this exercise, you are to implement the two functions seen above. The first function will return a different function pointer based on input. The second function will take in a function pointer as input to be used.
The actual program consists of constantly taking input from the user to perform
mathematical operations. The user first enters an operation they want to
perform ('+', '-', '*', '/'). You will be implementing a function
get_operation_function
that uses that input to return a pointer to the
corresponding function for that operation.
If you look through the provided code, there are already addition, subtraction, multiplication and division
functions implemented.
The user then enters 2 doubles as input to perform this operation on.
The next function you will need to implement is perform_operation
which takes these two inputs as well as a function pointer and calls the
function referenced by it, using the inputs as the parameters.
Examples
dcc function_pointers.c -o function_pointers ./function_pointers Please enter an operation: + Please enter 2 numbers to perform this operation on: 2 3 Applying this operation to 2.000000 and 3.000000 gives 5.000000 Please enter an operation: - Please enter 2 numbers to perform this operation on: 5 3 Applying this operation to 5.000000 and 3.000000 gives 2.000000 Please enter an operation: * Please enter 2 numbers to perform this operation on: 3 3 Applying this operation to 3.000000 and 3.000000 gives 9.000000 Please enter an operation: / Please enter 2 numbers to perform this operation on: 5 2 Applying this operation to 5.000000 and 2.000000 gives 2.500000 Please enter an operation: ./function_pointers Please enter an operation: + Please enter 2 numbers to perform this operation on: 1 1 Applying this operation to 1.000000 and 1.000000 gives 2.000000 Please enter an operation: + Please enter 2 numbers to perform this operation on: 1 5 Applying this operation to 1.000000 and 5.000000 gives 6.000000 Please enter an operation: + Please enter 2 numbers to perform this operation on: 100 100 Applying this operation to 100.000000 and 100.000000 gives 200.000000 Please enter an operation: / Please enter 2 numbers to perform this operation on: 1000 1000 Applying this operation to 1000.000000 and 1000.000000 gives 1.000000 Please enter an operation: ./function_pointers Please enter an operation: * Please enter 2 numbers to perform this operation on: 2 3 Applying this operation to 2.000000 and 3.000000 gives 6.000000 Please enter an operation: { Invalid operation '{' Please enter an operation: + Please enter 2 numbers to perform this operation on: 5 4 Applying this operation to 5.000000 and 4.000000 gives 9.000000 Please enter an operation:
If the user inputs an invalid command, return NULL;
in the
get_operation_function
function. NULL
is a standard way for programmers to
identify if a pointer actually points anywhere of value
Assumptions/Clarifications/Restrictions
- You can assume all number inputs will be valid
- If you have a function pointer named
func
that takes 3 integers as input, it can be called by saying(*func)(1, 2, 3)
. The(*func)
means dereference the pointer so we can get to the function, then the(1, 2, 3)
is the giving the parameters
function_pointers.c
// Program that utilises function pointer to perform mathematical operations.
//
// Written by Rory Golledge (z5308772), 13-03-2022
//
#include <stdio.h>
#include <stdlib.h>
double (*get_operation_function(char operation))(double, double);
void perform_operation(
double first,
double second,
double (*operation_function)(double, double)
);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(void) {
char operation;
double first;
double second;
double (*operation_function)(double, double);
printf("Please enter an operation: ");
while (scanf(" %c", &operation) == 1) {
operation_function = get_operation_function(operation);
if (operation_function == NULL) {
printf("Invalid operation '%c'\n", operation);
} else {
printf("Please enter 2 numbers to perform this operation on: ");
scanf("%lf %lf", &first, &second);
perform_operation(first, second, operation_function);
}
printf("Please enter an operation: ");
}
}
// Given two numbers `a` and `b`, return the result of adding them
double addition(double a, double b) {
return a + b;
}
// Given two numbers `a` and `b`, return the result of subtracting `b` from `a`
double subtraction(double a, double b) {
return a - b;
}
// Given two numbers `a` and `b`, return the result of multiplying them
double multiplication(double a, double b) {
return a * b;
}
// Given two numbers `a` and `b`, return the result of dividing `a` by `b`
double division(double a, double b) {
return a / b;
}
// Given an `operation`, return a pointer to the function that can perform
// that operation.
//
// E.g. if addition is the operation, a pointer to the `addition` function
// should be returned.
double (*get_operation_function(char operation))(double, double) {
if (operation == '+') {
return &addition;
} else if (operation == '-') {
return &subtraction;
} else if (operation == '*') {
return &multiplication;
} else if (operation == '/') {
return &division;
}
return NULL;
}
// Performs an operation given two numbers and the `operation_function` to
// apply on them. Prints in a human-readable way.
void perform_operation(
double first,
double second,
double (*operation_function)(double, double)
) {
double result = (*operation_function)(first, second);
printf(
"Applying this operation to %lf and %lf gives %lf\n",
first, second, result
);
}
Exercise
(●●●)
:
Conway's Game of Life
For this exercise, you are to write a program game_of_life.c
that simulates
Conway's Game of Life
This game consists of a 2D grid of cells. Each cell is said to be on 1 of 2 states:
- Dead
- Alive
In this game, the grid is said to change every "tick". The change that occurs depends on the states of each cell. Cells that are alive can die based on information about their neighbours and cells that are dead can come back to life under their own conditions.
There are many variations on what conditions are used to determine the next state of a cell. For this exercise, we will use the classic rules that were originally defined. These rules are listed below.
- If an alive cell has 2 or 3 neighbours, it will be alive in the next state.
- If a dead cell has exactly 3 neighbours, it will be alive in the next state.
- If none of the above conditions are met, the cell in question is dead in the next state.
The neighbours of a given cell are the 8 surrounding cells. E.g. if you take a cell, it has a neighbour above, below, left, right and all diagonals to it.
In this exercise, you will take input from the user in the form of row/column pairs. These correspond to the cells that will be alive when the game starts. All other cells will be dead to begin with.
You will be required to work in a 20x20 array representing a total of 400 cells. In the original Game of Life, it is assumed that the grid is infinite, however, in our game, it will obviously be 20x20.
An addition that has been made as a result of this is that cells can wrap around the grid and have no edge restrictions. This means that cells in row 0 can be affect by cells in row 19, and the same goes for columns.
Sample Program
A fully implemented version of the application is provided below.
- Click on the squares to make them alive (black means alive, white means dead)
- Click on the Tick button to see what the next state of the grid will be. This can be repeated as much as you would like.
- Click on the Play button to make the grid tick continuously.
- Click on the Tick Frequency button to select the frequency of a tick
Example inputs and outputs of your program are provided below. Basic steps that you will need to implement are:
- Scan in an arbitrary number of pairs which index the grid. Each pair makes the corresponding cell alive.
- Continuously provide commands to manipulate the grid. Commands are:
p
- Print the current gridt n
- "Tick" the gridn
times wheren
is a positive integerq
- Quit the program
Examples
dcc game_of_life.c -o game_of_life ./game_of_life Please enter the row and column of all alive cells. When finished, enter 'p'. 3 2 3 4 4 2 4 4 5 2 5 3 5 4 p Current cell state: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . # . . . . . . . . . . . . . . . . . # . # . . . . . . . . . . . . . . . . . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . t 1 Ticked 1 times! p Current cell state: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . . . . . . . . . . . . . . . . # . # . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . t 1 Ticked 1 times! p Current cell state: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . . . . . . . . . . . . . . . # # . # # . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . t 5 Ticked 5 times! p Current cell state: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . . . . . . . . . . . . . . # # . . . # # . . . . . . . . . . . . . . # # . # # . . . . . . . . . . . . . . . . # # # . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . q Exiting... ./game_of_life Please enter the row and column of all alive cells. When finished, enter 'p'. 3 10 4 10 5 6 5 10 6 5 6 7 6 10 7 6 7 8 7 10 8 9 8 10 8 11 9 8 9 10 9 12 10 11 11 9 11 12 12 8 12 11 13 7 13 10 14 7 14 9 15 8 p Current cell state: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . # . . . # . . . . . . . . . . . . . . # . # . . # . . . . . . . . . . . . . . . # . # . # . . . . . . . . . . . . . . . . . . # # # . . . . . . . . . . . . . . . . # . # . # . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . # . . # . . . . . . . . . . . . . . . # . . # . . . . . . . . . . . . . . . # . . # . . . . . . . . . . . . . . . . # . # . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . t 1 Ticked 1 times! t 3 Ticked 3 times! t 8 Ticked 8 times! p Current cell state: . . . . . . . . . # . . # . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . # # . # . . # . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . # . . . . # . . . . . . . . . . . . . # . . . . . . # # . . . . . . . . . # # . . . . # . . # # . . . . . . . . . # . . . . . # # . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . # . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # # . . . . . . . . t 30 Ticked 30 times! p Current cell state: . . . . . # # . . # # . . . . . . . . . . . . . . . # # # # . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . # # . . . # # . . . . . . . . . . . . # # . . . # # # . . . . . . . . . . . . . # # # . . # # . . . . . . . . . . . . . # . # # . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . # # . . . # . . . . . . . . . . . . . . # . . . # . . . . . . . . . . . . . . . . # . . . . . . . . . . t 100 Ticked 100 times! p Current cell state: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . # # # # # . . . . . . . . . . . . . . . # # . # # . . . . . . . . . . # . . . # . . . # . . . . . . . . . . # . . . . . . . # # . . . . . . . . . . . # . # . . . . # # . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . q Exiting...
Due to the sheer size of these outputs, no more will be provided. However, the interactive above is a great place to test different inputs and copy them to your program then tick side-by-side to test output.
Assumptions/Restrictions/Clarifications
- Your grid will always be
20x20
in size - Your grid can be of whatever type you wish (e.g. int, double, char). However, cells can only have 2 states so keep that in mind
- You will never be given invalid row/column pairs
- You may need to create two cell arrays, one for the current state and one for the next state
game_of_life.c
// Program to simulate Conway's game of life. User inputs a 20x20 array
// containing dead and alive cells. User can then choose to tick forwards
// the game by a given amount and print out the state. Although the original
// game specifies an infinite field, since we are bound by an array, cells can
// loop around.
//
// Written by Rory Golledge (z5308772), 12-03-2022
//
#include <stdio.h>
#define DEAD 0
#define ALIVE 1
#define SIZE 20
// Function Prototypes
void tick(int cells[SIZE][SIZE]);
void print_cells(int cells[SIZE][SIZE]);
// Helper prototypes
void handle_tick_command(int cells[SIZE][SIZE]);
int in_range(int value, int lower_bound, int upper_bound);
int main(void) {
int cells[SIZE][SIZE] = {DEAD};
int row;
int col;
printf(
"Please enter the row and column of all alive cells. "
"When finished, enter 'p'.\n"
);
// Scan in an arbitrary number of row, column pairs to set to alive cells
// in `cells`
while (scanf("%d %d", &row, &col) == 2) {
// Ignore invalid inputs
if (in_range(row, 0, SIZE - 1) && in_range(col, 0, SIZE - 1)) {
cells[row][col] = ALIVE;
}
}
// After this, we have our initial cells array filled up.
// Now, loop while asking for tick/print commands.
char command;
while (scanf(" %c", &command) == 1 && command != 'q') {
if (command == 't') {
handle_tick_command(cells);
} else if (command == 'p') {
print_cells(cells);
} else {
printf("Unknown command '%c'\n", command);
}
}
printf("Exiting...\n");
return 0;
}
// Returns whether the cell at (row, col) is alive or not in array `cells`.
//
// Handles rows/cols out of array range by looping them around to the other
// side of the array.
int is_alive(int cells[SIZE][SIZE], int row, int col) {
if (row < 0) {
row = SIZE - 1;
} else if (row >= SIZE) {
row = 0;
}
if (col < 0) {
col = SIZE - 1;
} else if (col >= SIZE) {
col = 0;
}
// Returns the state of the cell after row/col adjustments are made.
return cells[row][col];
}
// Return the number of alive neighbours next to the cell at cell_row, cell_col.
int alive_neighbours_count(int cells[SIZE][SIZE], int cell_row, int cell_col) {
// Neighbours are defined as the surrounding 8 cells, hence just loop
// over the 3x3 section where cell_row, cell_col is the centre.
int alive_neighbours = 0;
int row = cell_row - 1;
while (row <= cell_row + 1) {
int col = cell_col - 1;
while (col <= cell_col + 1) {
// Increment alive_neighbours if the cell is not the input one and
// the cell is alive.
if (
!(row == cell_row && col == cell_col) &&
is_alive(cells, row, col)
) {
alive_neighbours++;
}
col++;
}
row++;
}
return alive_neighbours;
}
// Given an array `cells` containing the state of all cells, apply the cellular
// rule to `cells[row][col]` such that the result is stored in
// `cells_output[row][col]`.
void apply_rule(
int cells[SIZE][SIZE],
int cells_output[SIZE][SIZE],
int row,
int col,
int alive_neighbours
) {
int alive_state = cells[row][col];
// Follow rules of https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
if (alive_state == ALIVE) {
if (alive_neighbours == 2 || alive_neighbours == 3) {
cells_output[row][col] = ALIVE;
}
} else if (alive_neighbours == 3) {
cells_output[row][col] = ALIVE;
}
}
// Given two arrays, `cells_in` and `cells_out`, copies all elements of
// `cells_in` to `cells_out`.
void copy_cells(int cells_in[SIZE][SIZE], int cells_out[SIZE][SIZE]) {
int row = 0;
while (row < SIZE) {
int col = 0;
while (col < SIZE) {
// Copy each element over
cells_out[row][col] = cells_in[row][col];
col++;
}
row++;
}
}
// Given an array `cells`, ticks the game state forward such that all rules
// of Conway's Game of Life are applied. The new state will remain in array
// `cells`
void tick(int cells[SIZE][SIZE]) {
// A secondary array is needed to store all the results in since if we
// modify `cells` in-place, incorrect states are formed.
int next_cells[SIZE][SIZE] = {DEAD};
int row = 0;
while (row < SIZE) {
int col = 0;
while (col < SIZE) {
int alive_neighbours = alive_neighbours_count(cells, row, col);
apply_rule(cells, next_cells, row, col, alive_neighbours);
col++;
}
row++;
}
copy_cells(next_cells, cells);
}
// Prints all cells in the `cells` array such that alive/dead cells are
// specified.
void print_cells(int cells[SIZE][SIZE]) {
printf("Current cell state:\n");
int row = 0;
while (row < SIZE) {
int col = 0;
while (col < SIZE) {
if (cells[row][col] == ALIVE) {
printf("# ");
} else {
printf(". ");
}
col++;
}
printf("\n");
row++;
}
}
// Handles the tick command when called as it requires a bit more work than
// the print command.
void handle_tick_command(int cells[SIZE][SIZE]) {
int amount;
scanf("%d", &amount);
// Warn for non positive tick amounts
if (amount < 1) {
printf("Cannot tick %d times!\n", amount);
} else {
int i = 0;
while (i < amount) {
tick(cells);
i++;
}
printf("Ticked %d times!\n", amount);
}
}
// Helper function to determine if a given value is within two other values
// (inclusive)
int in_range(int value, int lower_bound, int upper_bound) {
return value >= lower_bound && value <= upper_bound;
}