Because people typically think in terms of contiguous addresses being "horizontal," though I have no idea why.
Originally Posted by core_cpp
Lower memory addresses are often considered to be "below" or "left of" higher addresses. Yet when talking about a stack, the "top" of the stack is at the lowest memory address (usually). I just think the term "row-major" brings in too many assumptions, even though that's the term people use.
"The addresses in the array vary the slowest along the last dimension" might be a better way to put it.
The moral is that you use the one that your professor uses so you both talk about the same thing ;)
" For example - "Which of the following is the statement that declares a multiple dimensional character array that contains 10 words with 40 characters in each word?" "
The correct answer on the test is: char X;
I selected char X;
In C, as explained above, he is wrong. Not that the other answer is completely wrong, but since this was an intro to programming with C++, the X is far more correct. You should inform him about this.
I've spent the past day of down time percolating (and steaming) about this question. Maybe I shouldn't feel so strongly about it but I do.
There are really two questions in this thread. One is about the correct answer to MGrancey's exam, and the other is about how to think about 2-dimensional arrays.
MGrancey, the "correct answer" of char X is absolutely wrong and you should dispute it. It doesn't even agree with your teacher's visualization of array[y][x]. Actually, it's perfectly appropriate to visualize an array that way, as cartesian coordinates on a graph, because that implies that the first dimension represents rows and the second dimension represents columns. Under that assumption, it naturally follows that the 10 words with 40 characters would be laid out as
and as Daved said, there is no doubt that an array of c-strings or an array of string literals is laid out as above.
(7 more rows)
Using the exam answer of char X with 40 as the y dimension we would have
which would be ridiculous unless you're writing in Chinese.
(30 more rows)
Regarding rows and columns in general, Daved and brewbuck, if you're talking about arrays of more than 2 dimensions I'll agree that referring to a particular dimension as rows, columns, slices, or whatever you want to call them has no intrinsic meaning. There is no particular convention in that case and its fine to interpret them in any way that makes sense for the application. The only objective "rule" is that to optimize the running of the program it's important that iterating through the last dimension corresponds to accessing data that is LOGICALLY contiguous. That depends entirely on the application.
But in 2 dimensions there is a very definite, well-established convention - the first dimension is the "rows" dimension and the second is the "columns dimension. I'd like to call this a universal convention, but apparently this discussion by itself would prove me wrong. But if we call the 2-dimensional array a matrix, then I'd be on pretty solid ground calling it a universal convention. I strongly doubt that you can find any math book that would contradict the statement that a 3 x 2 matrix has 3 rows and 2 columns. If you like to read with your head turned sideways, and turn your matrix that way too so the first row runs vertically down the right side of the page, fine, but you'd better still call it a row because otherwise you'll be making it difficult and confusing to communicate with others about it.
Every time I've read anything about 2D arrays in a programming textbook (not a language reference manual), they call the first dimension the "rows" dimension and the second the "columns". For example, it is that way in K&R, in King's C Programming - A Modern Approach, in Lippman & Lajoie's C++ Primer -- those are all I have handy. Even in Fortran tutorials, (google "Stanford fortran tutorial") 2D arrays are described this way, despite the fact that Fortran lays out the first dimension contiguously in memory. This works fine. In Fortran you simply set up your loops with the inner loop on the first dimension, and iterate through one column, then the next, and so on.
I don't think this is anything like arguing about endianness. I don't care how a compiler stores an array in memory. This question really has very little to do with how the array is stored in memory. It's mostly a language convention (I mean natural language, not programming language). If we have a convention that makes communication easier, why fly in the face of it? Conventions like this let the next person understand what we wrote or said without wasting time on unnecessary explanations, and make it much easier for someone else to understand your code. If the data is such that it is natural to think of it as being arranged in rows and columns (such as pixels in a image, for example), I think this is a very compelling reason to set up the array as Array[rows][columns] and I would try to organize the data and the implementation so that it will be accessed row by row, unless there is a compelling reason to do otherwise.
As far as I know the standard is [row][column]. That is what I also most commonly see when it comes to C arrays. So why create a confusion?
The reason I believe is that people read line from left to right. So you do the same with an array:
1 2 3 4 5 6 7
8 9 ... etc ..etc
In C do be efficient you would do that [row][column].
That is all to say. But it is best to remember [iterate_second][iterate_first] because in some programs you would like to have the matrix [column][rows] and you might get confused. So the [iterate_second][iterate_first] is more "general" not to mix C arrays with matrixes, or whatever else you need
Originally Posted by C_ntua
Isn't that what I just said?
Yeah, I am totally agreeing with you :)
The "creating a confusion" is for people that do it the opposite way