473,503 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a problem of 2D array...

1 New Member
hello i am a newbie in c++
and now got confused in the 2D array

i want to represent xy coordinate in a 2D array
however i'm confused with the array representation
actually is that ,let say, a[0][1] is actually (1,0) on the xy plane?
if i want to create a plane with width=5, height=4, is that i should create an array plane[4][5]?
how to understand how a 2D array represent a xy plane more easily?

I m really confused with this>.<
i m working this for finding the shortest path....
i dunno wht to do....
thanks
May 11 '07 #1
2 1815
Ganon11
3,652 Recognized Expert Specialist
Remember that, since you are the programmer, you can determine if a[0][1] will correspond to the point (0, 1) or the point (1, 0). It's completely up to you. I do know, however, that when printing and processing in the common row-column form, you will be processing in a y, x fashion. If you want your rows to be x's and your columns to be y's (a.k.a. array[X_VARIABLE][Y_VARIABLE]), you can process/display your array in column-row order. But the ultimate decision is up to you.
May 11 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Avoid thinking a 2D array corresponds to an x-y coordinate. C++ has only one-dimensional arrays:

Expand|Select|Wrap|Line Numbers
  1. int arr[3];
  2.  
is an array of 3 elements where each element is an int.

Expand|Select|Wrap|Line Numbers
  1. int arr[3][3];
  2.  
is an array of 3 elements where each element is an array of 3 int.

So, the first index is the number of elements in the array and the other indexes just describe the elements.

Expand|Select|Wrap|Line Numbers
  1. int array[9] = {1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of:

1 2 3 4 5 6 7 8 9

and this array:
[code]
int tictactoe[3][3] = {1,2,3,4,5,6,7,8,9};

has a memory layout of

1 2 3 4 5 6 7 8 9

So, in memory the arrays are identical.

The indexes descirbe how the array is to be viewed. In the case of array[9], the element with the 8 is array[7]. That is, the 8th element (counting from 0). In teh case of tictactoe[3][3], the 8 is tictactoe[2][1]. That is, it is in the 3rd array (which is tictactoe[2]) and it is element 1 in that array giving tictactoe[2][1].

Locating an element in array, like array[7], consists of adding the size of 7 ints to the address of the array: array[7] = address of array + 7 * sizeof(int).

Locating an element in tictactoe, like tictactoe[2][1] consists of adding 2 times the size of an array of 3 int plus the size of 1 int: tictactoe[2][1] = address of tictactoe + 2* sizeof(3* sizeof(int))+ 1*sizeof(int).

In both cases, the starting address of each array has been increased by the size of 7 ints.

So, the "dimensions" are really how you want to view the array in memory. Regardless of your choice of dimensions, the pointer arithmetic must yield the same address for an element or the compiler won't be able to locate elements.
May 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2757
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5155
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3463
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55527
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10198
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
10046
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
16852
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
3167
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.