Chapter 4 - Worked Examples

Exercise 1 - Static Arrays

Create a C++ program as follows:

  1. Create an integer array of size 3, called numbers, with all its values initialised to 3.

  2. Using the subscript notation ([]), print out each element of the array.

  3. Using the same subscript notation ([]), set the 3 elements to 10, 11, and 12 respectively.

  4. Again, using the subscript notation ([]), print out each element of the array.

Exercise 2 - Dynamic Arrays

Create a C++ program as follows:

  1. Declare 2 empty vectors, named row1 and row2 respectively. (You do not need to specify a size, or initial values for these vectors.)

  2. Add the values 10 and 20 to row1, using the .push_back method.

  3. Add the values 100 and 200 to row2, again using the .push_back

  4. Print the size of each vector, using the .size method.

  5. Print the contents of each vector, using the .at method for row1, and the subscript notation [] for row2.

  6. Now, declare an empty 2D vector called grid. (Remember, a 2D vector is a vector of vector<int>s.

  7. Add row1 and row2 to grid, using the .push_back method.

  8. Display all elements in grid, using the .at() method.

  9. Display all elements in grid, using the subscript notation [].

  10. Change row1.at(0) to 1000

  11. Display again all elements in grid, with your method of choice.

Exercise 3 - Tic Tac Toe

For the coming weeks, we will slowly learn how to create a tic-tac-toe program in C++.

We'll be implementing the relevant functions as we learn more concepts, and in the end we'll piece them together to create our program!

Creating a tic-tac-toe board

Create a 2D integer vector, called board, with 3 rows and 3 columns, all initialised to the value 0.

This is akin to drawing a tic-tac-toe board on a piece of paper, with each element of the 2D vector corresponding to a 'square' on board. We'll use this 2D vector to represent the state of our board.

  • A value of 0 is meant to indicate that the 'square' represented by that element is empty.

  • A value of 1 will be used to indicate that the 'square' represented by that element is an 'X'.

  • A value of 2 will be used to indicate that the 'square' represented by that element is an 'O'.

Representing the state of each cell

Great, so we have our board - but note that since it's a 2D integer vector, there's nothing stopping programmers from accidentally putting 'invalid' numbers into each element - what would an element of the value '140' mean?

To fix this, we need to limit the range of values that can be held by each element. You might remember enums being a useful solution for such scenarios!

Create an enum, called Cell, that contains the following constants:

  • EMPTY, with value 0

  • PLAYER_X, with value 1

  • PLAYER_Y, with value 2

Now, go ahead and change the type of your 2D vector from vector<vector<int>> to vector<vector<Cell>>

Also make sure that each of the elements are initialised to EMPTY.

Great! Now we've restricted the values that each element can represent - going forwards we can be certain that each element will either be EMPTY, PLYAER_X, or PLAYER_Y.

Last updated

Was this helpful?