Help undeerstandin Multidimensional Arrays

Hi, i am watching a tutorial on multidimentional arrays and i have a question because im a bit confused on one part. Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int map[10][10] = {{0,1,2,3,4,5,6,7,8,9}, {10,11,12,13,14,15,16,17,18,19}};

    //0  1  2  3  4  5  6  7  8  9
    //10 11 12 13 14 15 16 17 18 19

    cout << map[1][6];
}


now when i output "cout << map[1][6];" i get 16, how is that? im confused on how its getting 16, is it going in the first row and then down? or what? im so lost as how to even explain it.
Arrays are zero indexed. That means they start indexing at 0, so map[0][0] == 0, map[1][0] == 10.

Your array is setup at row in the first index and column in the second.
Ok i see now, so if i wanted to make a map out of this how would i make it 10 rows down???
10x10 is 100, so you'll have to enter 100 values for array somehow. Either read them in from a file or type them out manually.
could i do it in a for loop? i seen a video on that. but if i wanted to set up a grid and have a character appear wherever i specify on that part of the grid, how would i do that?
Yes, you could do it in a for loop.

As for the second question, that is for you to work out.
think of a 2d array as a table (like excel)

rows and columns

so myarray[0][0] is the first element

and myarray[10][10] is the last element
Ok im a little confused on how to actually make the character go up anfd down.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <windows.h>
#define VK_UP 0x57
#define VK_DOWN 0x53
#define VK_LEFT 0x41
#define VK_RIGHT 0x44

using namespace std;

int W;
int A;
int S;
int D;

int PlayerPositionX = 0;
int PlayerPositionY = 0;

string character = "O";

int main()
{
    int map[10][10] = {{0,1,2,3,4,5,6,7,8,9}, {10,11,12,13,14,15,16,17,18,19}};

    W = GetAsyncKeyState(0x57);
        if(W){
        character[0][0]++;
        }

}


I tried this but it didnt work. Im not sure how to put the character on the grid. can you give me any hints??
Last edited on
ok..

first number goes here myarray[x] second goes here myarray[][x]
for instance, character[0][0] = 00 in my diagram below
character[0][2] is 02 in my diagram, etc..
10 rows, 10 columns = 100 characters

[00] [01] [02] [03] [...etc] [] [] [] [] []
[10] [11] [12] [] [] [] [] [] [] []
[20] [] [] [] [] [] [] [] [] []
[30] [] [] [] [] [] [] [] [] []
[40] [] [] [] [] [] [] [] [] []
[...etc] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] [1010]

this is a 10x 10 array

I get that part, but how do i make the "O" character move on this grid, thats what im asking.
bump
if you want a character (char) in your map your map shouldn't be of the type int (-> char). You can place character in this map but you won't see the 'O' instead you'll see number of the character which is 79.

You cannot place a string inside of an int map.

if you change that type of the map to char you can move the 'O' like so:
1
2
3
4
5
6
7
8
int x = 5;
int y = 5;

// move up
map[x][y] = orginial_value;
y--;
orginial_value = map[x][y];
map[x][y] = 'O';
Ok i have this but how do i make it move on screen?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <windows.h>
#define VK_UP 0x57
#define VK_DOWN 0x53
#define VK_LEFT 0x41
#define VK_RIGHT 0x44

using namespace std;

int W;
int A;
int S;
int D;

int X = 0;
int Y = 0;

char character = 'O';

int main()
{
    char map[10][10] = {{0,1,2,3,4,5,6,7,8,9}, {10,11,12,13,14,15,16,17,18,19}};

    W = GetAsyncKeyState(0x57);
        if(W){
        map[X][Y] = character;
        Y--;
        character = map[X][Y];
        map[X][Y] = 'O';
        }
}
Last edited on
uh, I didn't look at your code so I don't know if you set it up correctly. But if it is, nested for loops with cout << should work.
1
2
3
4
5
6
for (i =0; i < 10; i++){
            for(j = 0; j<10; j++){
                   cout<< "["map[i][j] << "] ";
            }
            cout << endl; // ends the line
}
1. You can use a switch/case statement.
2. You could use a map to map inputs to function calls.
Topic archived. No new replies allowed.