I have a two-d array from which I need to read values from any and all sides of each index.

for example

int x=0, y=0;
myArray [x - 1][y - 1];

where x-1 and y-1 would be the values of the other end of the array.

I've been up over 24 hours trying to write this program, and my mind's far from lucid. Can someone PLEASE tell me how to do this being as elaborate as possible (or even better providing the complete code).

THANK YOU IMMENSELY!

Recommended Answers

All 3 Replies

You could do it if you know the value of x & y.

int x=0, y=0;
int[][] myArray;  // just declare the variable but not initiate it yet
// some code before you create the 2D array
// you could change values of x & y along the way
...
...
...
// once you are ready to create it, you could declare as follows:
myArray = new int[x-1][y-1];
// now myArray is filled with 0s
...
...

PS: Are you sure that you want x-1 & y-1 instead of just x & y values?

[x-1] and [y-1] was just an example. This is worded a little more eloquently than my question:

...the board will have no edges, i.e. it `wraps around' so every square will have eight neighbors.
For example, the row above the first row on the board is the last row, and the column to the right of the last column is the first column on the board.

I need to be able to read all eight adjacent index values from any other index value, such as [0][0].

You mean...

/*
For example, myArray size is 6x8 as below.
|00  01  02  03  04  05  06  07|
|10  11  12  13  14  15  16  17|
|20  21  22  23  24  25  26  27|
|30  31  32  33  34  35  36  37|
|40  41  42  43  44  45  46  47|
|50  51  52  53  54  55  56  57|

The number inside represent the location in an array. For example, if the number is 24, it means myArray[2][4].
*/

From the example of array built above, you mean you want to know all index value of, let's say at [0][0]? If it is out of bound, what you do? For example at [1][2], all 8 neighbors are [0][1], [0][2], [0][3], [1][1], [1][3], [2][1], [2][2], and [2][3]. So [0][0] would have only 3 indices - [0][1], [1][0], and [1][1]. Correct?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.