If an array of size 20 may be indexed from 0 to 19, I'd say no.
How about you describe what each dimension of the array is supposed to represent?
If an array of size 20 may be indexed from 0 to 19, I'd say no.
How about you describe what each dimension of the array is supposed to represent?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
> if(seats[first_class][business_class] == 0)
Simply put this line of code in to see what Dave is on about
Code:printf( "if(seats[%d][%d]==0\n", first_class, business_class); if(seats[first_class][business_class] == 0)
It produced [0] and [-858993460]. So I understand that they have no value, or garbage data. But they are just subscripts, they are supposed to cycle from 0 to -1 of the [10] or [20]Code:for(first_class=0; first_class<10; ++first_class)
The [10] is supposed to be 10 seats in first class, [20] 20 seats in business class. Each time one of them is selected, an element of the array is given a 1(for first class) or 2(for business class). when the program ends, it would display the number of times each was selected.
So you have 10 + 20 seats,
But that array has 10*20 locations.
Perhaps you need a different array - like
first_seats[10], business_seats[20];
Well, way back in the first post my original question was if in a multidimensional array like this one, can you use the first part to hold data. Or can it only act as a counter for the second part.
I thought I might have to use two separate arrays if that was the case.I am not sure about something. If you are using a multidimensional array like: scores[5][10], is it true that you can only enter data into the second size because the first size acts like a counter? Say: scores[] is an array of 5 players which is an array of 10 scores. So the 5 has to be the number of groups of 10 scores? So I could not enter data into the 5 part? Only the 10 could have data entered into it?
Yes, a multi-dimensional array can only access the last index for setting or getting an value of the type that the array is (int in your example).
int score[5][10];
You should look at it like this in 3 seperate steps:
1. score is an array of...
2. 5 arrays of...
3. 10 int's each
Arrays in an array are usually called sub-arrays
There's really nothing special about this 2D array (or any other multidimensional array) to a normal 1D array because the memory layout for both is similar (contiguous). The big difference is that, if you want to set or get a value to this 2D array, you have to index it twice (e.g, array[x][x] = value) because when you declared the 2D array, you told the compiler to generate code that would force double indexing every time it detected the use of the identifier (variable score) in the source code for setting or getting a value of type int in the array.
And it looks something like this (for space reasons, I'm going to shorten the score array):
Multidimensional Array (2 Dimensions):
int score[2][5];
Array (normal 1 Dimension):
int otherScore[10];
Each array has reserved 10 memory blocks to hold int values in your program (note: it's actually 40 memory blocks for each because int's usually take 4 memory blocks of space but the logic is the same, but, I'm going to make this easy on you). The memory layout for both would look similar to this:
score[2][5]: /* 1,2,3..... 10 memory blocks */
[1][2][3][4][5][6][7][8][9][10]
otherScore[10]: /* 1,2,3..... 10 memory blocks */
[1][2][3][4][5][6][7][8][9][10]
Since you know how 1D arrays work (I assume), I will not get into the specifics of that, but, I will explain the 2D array. The compiler knows you want to treat score as an array of... 2 arrays of... 5 int's each (it keeps track of this information internally and associates it with the identifier score), so when you do:
score[0][3] = x; The compiler does this:
it treats the 0 as the first array of the 2 arrays in the array score. This starts at memory block 1, and since each array is 5 memory blocks in length, the compiler will treat memory blocks 1 through 5 as the first array, of the 2 arrays in the array score:
score memory blocks:
[1][2][3][4][5][6][7][8][9][10]
For 3 (the second index), it'll only look in these 5 memory blocks
and jump to index 3 (0 through 3 = 4), which would be memory block 4:
[1][2][3][*4*][5][6][7][8][9][10]
For accessing an index in the second array: score[1][3]; It would look like this:
[1][2][3][4][5][6][7][8][*9*][10]
The compiler secluded memory blocks 6 - 10 for the second array, which is index 1 of score, because memory blocks 1 - 5 belong to the first array. Then it offsetted (jumped) 4 memory blocks to get to memory block 9, That's because indexing arrays always start at 0, and since 0 through 3 is 4 offsets, not 3, memory block 9 is the jackpot.
If you don't have a very strict compiler, you could verify the contiguousness of multidimensional arrays. What would happen if you assigned value number 5 into score's second array at subscript (element, index) 0 ?: score[1][0] = 5; and you tried printing the value assigned there like this: printf("%d", score[0][5]); ? You are essentially trying to access the value 5 at the second array through the first array. That, if I recall correctly, is undefined behavior, and it's called an out of bound access. You normally wouldn't want to do that, but it does prove one point, and that is, a multidimensional array is contiguous. On the other hand, an array of pointers (type *[n]), which has the same syntax as multidimensional arrays, would definitely not be ok trying to perfom this contiguous validity because, the second index is not contiguous - only the first index.
xeddiex.
p.s, I know this is probably overkill because of the specificness I got into, but, I think in the long-run knowing how things work "under the hood" is not only both benificial in that, it makes you a better programmer and, helps you in the long-run as a programmer in any language, but it also *helps* you *understand* the abstract realm better, provided it's explained clearly, as I hope I did. =)
Thank you eddie for directly answering my original question. The memory breakdown was helpful as well.Yes, a multi-dimensional array can only access the last index for setting or getting an value of the type that the array is (int in your example).