English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to create a 2D array of 3x3 using a single dimensional array starting from 1-9. 3 rows and 3 cols but the array is single dimensional. [1,2,3,4,5,6,7,8,9] but i am trying to represent it as :
[1,1 (1)] [1,2 (2)] [1,3 (3)]
[2,1 (4)] [2,2 (5)] [2,3 (6)]
[3,1 (7)] [3,2 (8)] [3,3 (9)]

The user only enters the rows and cols and the grid is fixed 3x3.
How can i calculate for example:

r2, c2 = 5?

if i do this r*c then i get the correct answer for 1*1, 1*2, 1*3, 2*3, 3*3 but not for others.

Thanks everyone

2007-01-10 05:03:37 · 3 answers · asked by Manish 5 in Computers & Internet Programming & Design

3 answers

You have used row major method.
So if you want to calculate the value of
A[r][c] then it will be (r-1)*no. of total columns + c
so for r=2,c=2 -> (2-1)*3 + 2 = 5
for r=3, c=3 -> (3-1)*3 + 3 = 9
got it!

2007-01-10 05:14:37 · answer #1 · answered by rohit 1 · 1 0

element = (row-1)+column

2007-01-10 13:15:50 · answer #2 · answered by Wonka 5 · 0 0

http://www.google.com/search?hl=en&lr=&q=how+to+solve+arrays

2007-01-10 13:09:00 · answer #3 · answered by god knows and sees else Yahoo 6 · 0 0

fedest.com, questions and answers