2-D Arrays

Hi guys, I'm trying to write a text-based adventure game. The basic gist is that I create a layout with different rooms, each numbered starting with 0.

In a part of this assignment, I am expected to "Declare a 2-dimensional integer array to hold each room’s exits. This array should have 2 sizes: the number of rooms (7) and total possible exits (4). Use an initializer list to add all of your room exits. Each room should contain a 4 element array containing the indices to the rooms that connect to it in the order: North of this room, East of this room, West of this room, South of this room.

Can someone please show me how to declare a 2-D array with these directions? I'm confused about the instructions--am I supposed to create an array of 7 rows and 2 columns? How do I initialize a 2-D array?
I think what you want is this

arr[7][4];

This is a row with 7 rows and 4 columns. Or in this case, 7 rooms and 4 exits within each room.
I'm confused about the instructions--am I supposed to create an array of 7 rows and 2 columns?

No; you actually quote the portion of the assignment that tells you the dimensions:
Declare a 2-dimensional integer array to hold each room’s exits. This array should have 2 sizes: the number of rooms (7) and total possible exits (4).


On initializing arrays, this shows examples for arrays with one dimension:
http://www.cplusplus.com/doc/tutorial/arrays/#initialization
As I recall, you can nest the curly brace notation for multiple dimensions.

I hope this helps clear up some confusion without doing it for you.
Thanks, but can you also show me how I should do the 4 element array containing the indices to the rooms that connect to it?
If you know how to use normal arrays, you'll understand 2d arrays.

So, the 7 is the rooms. and the 4 is exits. So if you want to access the third room, and the last exit in that room, it would be

arr[2][3];

If you want to input to it etc, you would use a nested for loops, 2 for loops within eachother. The outside for-loop runs 7 times becuase there are 7 rooms, and the inside one runs 4 times.

I understand what the array is (7 rooms, 4 possible exits). I also know that for each room, these are the possible exits from the layout I have (each number is a room; I can't get the formatting right, but room 6 is supposed to be directly under room 4)--

____________________________
| 0 | 1 | 2 |
| 3 | 4 | 5 |
| 6 |



room 0: {-1,1,-1,3}
room 1: {-1,2,0,4}
room 2: {-1,-1,1,5}
room 3: {0,4,-1,-1}
room 4: {1,5,3,6}
room 5: {2,-1,4,-1}
room 6: {4,-1,-1,-1}



I just don't understand how to initialize it? I'm not sure if initialize is the word, but I hope you can understand what I'm asking after this diagram.
I'm just trying to understand how to "tie it all together". If I put arr[7][4]; like you said, wouldn't the default value be {0,0,0,0} for all of the rooms?
If you want to initialize it, you would do it like this.


1
2
3
4
5
6
7
int arr[7][4] =
    {
        {1,8,12,20},
        {5,9,13,24},
        {8,1,16,99}.
        ....  etc
    };


Edit: The default values would not be 0 for all rooms, it would be nothing, just a garbage value.
Last edited on
Thank you!
Topic archived. No new replies allowed.