Rows and columns? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Rows and columns?

Are the x,y predefined or depend on how you access the array.?

10th Jul 2017, 1:46 AM
mfa
4 Answers
+ 8
It depends on how you access the array. Taking array[x][y], it can be accessed for each x in y, although the norm is to go for each y in x. E.g. for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { // either print array[x][y] or array[y][x] is feasible as long as array index stays inbound and is logically correct } }
10th Jul 2017, 2:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
@Rrestoring Faith I think I meant to say that the concepts of 'rows' and 'columns' represented in 2D arrays can be interchanged according to how the array is accessed. While the first index of an array definitely corresponds to the 'largest box' or the 'outermost frame', the data structure entirely depends on the programmer's mental model. E.g. Matrices being listed vertically or horizontally.
10th Jul 2017, 3:39 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
@Hatsy Maybe I'm wrong but isn't it always [y][x]? If we have an array: {4,3,2},{1,2,6} The first index [c][] will always be which array you are pointing to. (The y array at index c.) So, [1][2] is 6. [2][1] is error. [1][0] is 1. [0][1] is 3. Am I wrong about this, or miss-reading the question? 😜.
10th Jul 2017, 3:27 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Ahh alrighty, very true. My bad.
10th Jul 2017, 3:40 AM
Rrestoring faith
Rrestoring faith - avatar