Paul's Tutorials - logo2-2Arrays



Reading

Go here:
Arrays
and read through the whole page.

Extra Reading

Companion Video

C Strings

You are likely aware of this, but C++ is actually derived from another language called C. In C, there is no string data type. Instead, C programmers use an array of chars.

Since many functions are actually C functions ported to C++ (like printf()), they still rely on C strings. If you need to use a string in a C function, you can do one of two things:

Practice

Once you feel comfortable with the above content, complete Pancake Glutton. As an added challenge, you may also complete Dungeon Crawl or 2048 if you have time. However, the last two activities are rather time consuming, and many have found it useful to move on and return to these later.

Pancake Glutton

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10). Once the data has been entered the program so that it outputs a list in order of number of pancakes eaten of all 10 people.

Example

Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

Extra Practice

Sort the people in the other direction.

Dungeon Crawl

Make a program that outputs a simple 10 x 10 grid based gameboard to the screen using characters.

Example

. . . . . E . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X
. . . . . . . T . .
. . . E . . . . . .
. T . . . . . . . .

Allow the user (marked by G) to move either up, down, left, or right each turn. If the player steps on a trap (marked by T) then they lose. If they make it to the treasure X then they win.

Add enemies (denoted by E) that move randomly in any direction once per turn (enemies, just like traps, cause the player to lose if touched).

If you don't want to have the user hit enter (for example, you want them to use the WASD keys to navigate), look at unbuffered input.

HINT: Don't let the player move off the gameboard! You program will crash if they move off the top or bottom of the board! (the same holds true for enemies)

Extra Practice

Do any of the following:

2048

Create a clone of the 2048 game in the terminal using an array (you do not need to include animations).

The game is played on a 4x4 grid. The grid starts with all tiles empty except two, which contain either a 2 or a 4. The player can move the tiles up, down, left, or right. When the player moves the tiles, they all move as far as possible in that direction. When two tiles of the same number meet, they merge and the resulting tile contains twice the value of the first (however, tiles may only merge once per move). Every time the player makes a move, a 2 or a 4 is randomly added at a random location.

The goal, of course, is to reach the 2048 tile.

Example

.    .    2    .

.    .    .    .

.    4    .    .

.    .    .    .

Use a multidimensional array of ints to do this. (Hint: If the number were, say, 0, you don't have to print that number)

Be sure that your grid has enough space to contain the 2048 tile! Numbers cannot run over other tiles and the grid must look continuous. In order to do this, you could use the setw() function to ensure each tile gets a set amount of space, and then the left function to align it all to the left before printing the tile's contents.

If you don't want to have the user hit enter (for example, you want them to use the WASD keys to navigate), look at unbuffered input.

Extra Practice

Allow the player to keep playing after they have reached the 2048 tile.


← 2-1 Functions 2-2 Arrays 2-3 Structures →