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

2-D Array Help C++

Reply to Message Icon

Original Message
Name: vegito616
Date: March 13, 2006 at 15:33:12 Pacific
Subject: 2-D Array Help C++
OS: Windows 98
CPU/Ram: Pentium III, 256 MB Ram
Model/Manufacturer: 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


Report Offensive Message For Removal


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

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: 2-D Array Help C++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes Today.
Discuss in The Lounge