0% found this document useful (0 votes)
10 views9 pages

Assignment 2

The document outlines Assignment 2 for the Introduction to Algorithms & Programming course at the University of the Witwatersrand, due on May 27, 2025. It involves implementing a cellular automaton based on rules governing an ant's movement on a grid, with tasks ranging from grid representation to simulating multiple ants. The assignment emphasizes originality and warns against plagiarism, with automated online marking for correctness.

Uploaded by

liyyah.m25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Assignment 2

The document outlines Assignment 2 for the Introduction to Algorithms & Programming course at the University of the Witwatersrand, due on May 27, 2025. It involves implementing a cellular automaton based on rules governing an ant's movement on a grid, with tasks ranging from grid representation to simulating multiple ants. The assignment emphasizes originality and warns against plagiarism, with automated online marking for correctness.

Uploaded by

liyyah.m25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

School of Computer Science & Applied Mathematics

Introduction to Algorithms & Programming (COMS1018A/1022A)


Assignment 2

Due Date: 27 May 2025 23h55

1 Introduction

Like the labs, assignments are marked automatically online. This means that your program
must produce exactly the expected output for it to be considered correct. For each task, write
C++ code to solve the problem and submit the appropriate .cpp file t o t he M oodle marker.
Successfully completing all six tasks will earn you 100%.

Again, please be aware of the policy surrounding plagiarism (see the course outline for a full
description). Code will be scrutinised, and anyone found to have cheated (including the person
from whom the code was copied) will immediately receive 0, and may be subject to disciplinary
sanctions.

2 Background

Stephen Wolfram is a pretty interesting fellow. If you haven’t heard of him, he’s a mathematician
and computer scientist who founded Wolfram Research, the company behind Mathematica and
WolframAlpha.

His book A New Kind of Science focuses on the a mathematical concept called cellular au-
tomata. Cellular automata are discrete models that evolve over time. They consist of a regular
grid of cells, with each cell in some state (e.g. on or off ). At each time step, the grid is modified
according to a set of rules. Simple rules can give rise to vastly complex emergent behaviour, as
seen in the most famous of all cellular automata, Conway’s Game of Life. The Wikipedia page
(https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) has some nice visualisations of
the system’s evolution.

3 The Plan

We are going to implement our own cellular automaton. Imagine that there is an ant placed on

1
a 2D grid. The ant can face in any of the four cardinal directions, but begins facing north. The
cells of the grid have two state: black and white. Initially, all the cells are white. The ant moves
according to the following rules:

1. At a white square, turn 90◦ right, flip the color of the square, move forward one square.
2. At a black square, turn 90◦ left, flip the color of the square, move forward one square.

Figure 1 illustrates provides an illustration of this.

Figure 1: An illustration of the automaton. The ant is currently on a black square and facing
south. On the next time step, it would turn to face east, change the colour of the cell to white,
and move forward one cell.

The following series of tasks will lead us to creating an automaton based on these rules.

2
4 The First Task (20 marks)

The first task involves storing a representation of the grid and displaying it to screen. Write a
program that reads in a grid, stores it in a 2D data structure and then outputs it to screen. Black
cells should be represented by 1, and white cells by 0.

4.1 Input

The first line of input consists of two integers r and c, separated by a single space. These are the
number of rows and columns of the grid. r rows of input follow, each containing c characters
separated by a single space, which specify the colour of the cell at that position.

4.2 Output

Output the representation of the board, using 1 for black and 0 for white cells. There should be
no spaces between the cells.

Sample Input

5 5
0 0 0 0 0
1 0 0 1 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0

Sample Output

00000
10010
11100
00000
00000

3
5 The Second Task (20 marks)

We will now encode the rules. Write a program that accepts the colour of the cell the ant is
currently at, as well as its position and direction it is facing, and computes the cell’s new colour,
and the ant’s new position and direction after taking a single step.

5.1 Input

The first line of input is a single integer, representing the colour of the cell. The next line consists
of two integers m and n and a character d, separated by a single space, specifying the row and
column location of the ant, and the direction it is facing (either N, S, E, or W for the four cardinal
directions).

5.2 Output

The first line of output is the colour of the cell after the ant takes a single step. The next line is
the new position and direction the ant is facing after executing a single step.

Sample Input

1
3 2 E

Sample Output

0
2 2 N

4
6 The Third Task (20 marks)

We will now simulate a single step of the ant. Write a program that reads in the grid, the
position and direction of the ant, and displays the grid after the ant moves according to the
aforementioned rules.

6.1 Input

The first line of input consists of two integers r and c, separated by a single space. These are the
number of rows and columns of the grid. The next line consists of two integers m and n and a
character d, separated by a single space, specifying the row and column location of the ant, and
the direction it is facing (either N, S, E, or W for the four cardinal directions). r rows of input
follow, each containing c characters separated by a single space, which specify the colour of the
cell at that position.

6.2 Output

The first line of output is the position and direction the ant is facing after executing a single step.
The next lines of output are the representation of the new state of the board, as in the previous
task.

Sample Input

5 5
1 0 E
0 0 0 0 0
1 0 0 1 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0

Sample Output

0 0 N
00000
00010
11100
00000
00000

5
7 The Fourth Task (20 marks)

We will now simulate multiple steps. Write a program that takes in the grid size and the initial
position of the ant, and displays the grid after each time step.

7.1 Input

The first line of input is an integer T , the number of steps to simulate. The next line consists of
two integers r and c, separated by a single space. These are the number of rows and columns of
the grid. Every cell is initially white. The next line consists of two integers m and n, separated
by a single space, specifying the row and column location of the ant (recall that the ant starts
facing north).

7.2 Output

Output the initial board representation, and then the board after every step taken. The represen-
tations should be the same as they are in The First Task. Each board output should be separated
by a single blank line.

Sample Input

2
5 5
2 2

Sample Output

00000
00000
00000
00000
00000

00000
00000
00100
00000
00000

00000
00000
00110
00000
00000

6
8 The Fifth Task (10 marks)

Improve upon the previous task by implementing “wrapping”: if the ant has reached the edge of
the board and is about to step off, it should appear on the other side of the board. For example,
if it is about to step of the bottom of the board, it should appear in the first row on the next step.

8.1 Input

The first line of input is an integer T , the number of steps to simulate. The next line consists of
two integers r and c, separated by a single space. These are the number of rows and columns of
the grid. Every cell is initially white. The next line consists of two integers m and n, separated
by a single space, specifying the row and column location of the ant (recall that the ant starts
facing north).

8.2 Output

Output the initial board representation, and then the board after every step taken. The represen-
tations should be the same as they are in The First Task. Each board output should be separated
by a single blank line.

Sample Input

2
5 5
2 4

Sample Output

00000
00000
00000
00000
00000

00000
00000
00001
00000
00000

00000
00000
10001
00000
00000

7
9 The Sixth Task (10 marks)

Further extend your code by implementing multiple ants! Note that ants move simultaneously.

9.1 Input

The first line of input consists of two integers T and A, separated by a single space. These are
the number of steps to simulate, and the number of ants. The next line consists of two integers
r and c, separated by a single space. These are the number of rows and columns of the grid.
Every cell is initially white. The next A lines each consist of two integers m and n, separated by
a single space, specifying the row and column location of a single ant (recall that the ant starts
facing north).

9.2 Output

Output the initial board representation, and then the board after every step taken. The represen-
tations should be the same as they are in The First Task. Each board output should be separated
by a single blank line.

Sample Input

2 2
5 5
2 2
2 4

Sample Output

00000
00000
00000
00000
00000

00000
00000
00101
00000
00000

00000
00000
10111
00000
00000

8
10 The Seventh Task (Bonus for 0 Marks)

If you have completed all of the previous tasks, download the file ant.cpp from Moodle and
complete the code to create a visualisation of the automaton in action!

Do not modify the code except where instructed. You will need to implement input in the
main function and do the initial setup, as well as implement two functions:

1. isBlack(vector<vector<int, int, int), which returns true if there is a black cell at


the given index, and false otherwise
2. step(vector<vector<int&, vector<pair<int, int&, vector<char>&), which executes
one step of the simulation. The 2D vector is the grid, the second parameter lists the posi-
tions of the ants, and the final parameter lists the directions the ants are facing.

You will need to have the SDL2 libraries on your system and link your application to them.
This will differ from system to system, so please Google how to do this.

Note that I hacked together the visualiser pretty quickly. If you try click or move the
screen around, the program will freeze. Sorry.

10.1 Input

The first line of input consists of two integers T and A, separated by a single space. These are
the number of steps to simulate, and the number of ants. The next line consists of two integers
r and c, separated by a single space. These are the number of rows and columns of the grid.
Every cell is initially white. The next A lines each consist of two integers m and n, separated by
a single space, specifying the row and column location of a single ant (recall that the ants starts
facing north).

10.2 Output

A visualisation of the ants in progress. Look how pretty!

You might also like