Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
For a project of mine in C++, i need to create a 2d that displays something along these lines,while i need a little help getting started since i am fairly new to 2d arrays, thx for ur time. I am using MSVC++
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 5 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1

for starters you can instantiate the array like this:
int my_array[7][7];
and you can access each element or the array like this to set the array:
my_array[0][0] = 1; //row 0 col 0
my_array[0][1] = 1; //row 0 col 1
my_array[0][2] = 1; //row 0 col 2
my_array[0][3] = 1; //row 0 col 3
my_array[0][4] = 1; //row 0 col 4
...
...
...
my_array[1][1] = 2; //row 1 col 1
my_array[2][2] = 3; //row 2 col 2
my_array[3][3] = 5; //row 3 col 3it is fairly arbitrary which index you want to use for row and which to use for column as long as you are consistent. So you could assume that
my_array[row][column]
or
my_array[column][row]
I will use the first convention.
Then to print you just write a nested for loop like this:
//each time through this loop you print one row
for(int row = 0; row < 7; row++)
{
//this loop prints each column of a row
for(int col = 0;col < 7; col++)
{
cout << my_array[row][col];
}
cout << endl;
}

![]() |
Soud Card Beep?
|
Two for loops at the same...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |