Computing.Net > Forums > Programming > 2-D Array Help C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

2-D Array Help C++

Reply to Message Icon

Name: vegito616
Date: March 13, 2006 at 15:33:12 Pacific
OS: Windows 98
CPU/Ram: Pentium III, 256 MB Ram
Product: Dell
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: geohoffman49431
Date: March 13, 2006 at 23:21:18 Pacific
Reply:

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 3

it 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;
}


0
Reply to Message Icon

Related Posts

See More


Soud Card Beep? Two for loops at the same...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: 2-D Array Help C++

Return 2-D Array in C www.computing.net/answers/programming/return-2d-array-in-c/13956.html

C 2-D array www.computing.net/answers/programming/c-2d-array/8058.html

2-d array from a file in c++ www.computing.net/answers/programming/2d-array-from-a-file-in-c/3907.html