+ 4

Can anyone explain how the objects or data in an multiple array are indexed?

i would like to know how a [3][3] array would store data and how it would index it?

8th Jan 2017, 9:31 PM
Siddharth Naithani
Siddharth Naithani - avatar
5 Antworten
+ 6
The logic is: most left index is outer, most right inner... If you define an array of one dimension, its elements are stored sequencially. Now, figure that for each elements we store another one dimension array: array[0]=array[] array[1]=array[] array[2]=array[] ... array[n]=array[] We have defined a two dimentionnal array: array[A][B] means get value of array[A], wich is too an array, and get in it the value stored at index B... You can also write equivalent: (array[r])[c] So, it's common to represent 2-dim' arrays as left index as rows, and right as cols... which correspond to the occidental reading direction but can be confusing, because in others context, we have the habitude to think horizontal dimension first, and vertical second ( geometry ) :P
9th Jan 2017, 4:08 AM
visph
visph - avatar
+ 4
Consider: int i = 3; int j = 3; int mat[i][j] = {1, 2, 3}, {4 5 6}, {7 8 9}; Let's make the assumption that the data is in row-major form, so 1 is [0][0], 2 is [0][1], 3 is [0][2]. 4 is [1][0], and so on. Then suppose that we use pointers to push the values of mat onto the stack. The stack is like a building with several floors, and each floor represents a memory location. However, in this building we consider the top of the building to be the ground floor, and the bottom of the building is the top floor (maximum stack memory). Normally, a pointer will place our data into the bottom floor and work its way toward the top until the stack is full. Each time we place data into a floor, the stack pointer will point to the next available floor. Hence, our 3x3 matrix would look something like... x00F6 <-- stack pointer 9 - x00F7 8 - x00F8 7 - x00F9 6 - x00FA 5 - x00FB 4 - x00FC 3 - x00FD 2 - x00FE 1 - x00FF However, the integer number in the stack will actually contain the memory address (from the heap) of that integer. The compiler will randomly assign each value of the matrix to whatever block of memory is available, but it's usually placed one after the other when pushed onto the stack using pointers. For example... int *matrix = mat[0][0]; is going to point to the memory address that contains the integer value of 1. We will take the elevator (stack pointer) to the floor that contains the address in the heap of mat[0][0] when we wish to retrieve the address of *matrix. The heap is like an apartment complex our data is renting for some time, and a pointer can store its address in the stack. Our data could rent a space directly in our building (stack) by using *matrix = 1; The address of a particular element of an NxM matrix in row-major form is found by using the formula: mat(i,j) = (i x M) + j + address of mat(0,0), but you will rarely need to use such a formula.
9th Jan 2017, 4:29 AM
Jonathan Rasch
Jonathan Rasch - avatar
+ 3
you should use for loop within for loop the first one for rows and the other for columns ,the index of the first loop should still 0 till the column loop is finished it's loop for the first row then the second loop index will turn into the second row till the number of rows are finished. for example: int arr[a][b]; for(int i=0,i<a,i++) //a=rows { for (int k=0,k<b,k++) //k=columns cin>>arr[i][k]>>endl; }
10th Jan 2017, 8:53 AM
Mohamed Eldsoky
Mohamed Eldsoky - avatar
+ 2
It can be indexed as follows: [Row] [Column]
9th Jan 2017, 4:48 PM
Porkodi Sachi
Porkodi Sachi - avatar
+ 1
You can define it as a[column][row] or a[row][column], it works like table or Ms. Excel
8th Jan 2017, 9:47 PM
Joseph Kristiano
Joseph Kristiano - avatar