Consider a 2-dimensional array x with 10 rows and 4 columns, with each element…
2020
Consider a 2-dimensional array x with 10 rows and 4 columns, with each element storing a value equivalent to the product of the row number and column number. The array is stored in row-major format. If the first element x[0][0] occupies the memory location with address 1000 and each element occupies only one memory location, which locations (in decimal) hold a value of 10?
- A.
1018, 1019
- B.
1022, 1041
- C.
1017, 1036
- D.
1000, 1399
Attempted by 311 students.
Show answer & explanation
Correct answer: C
In row-major storage, the address of element x[i][j] (using the array's 0-based indices i, j) is Address = Base + (i × numColumns + j). Separately, when a question speaks of an element's 'row number' and 'column number', it means the element's 1-based position -- row_number = i + 1 and column_number = j + 1 -- not the raw array index.
Here numColumns = 4, Base = 1000, and each element's value = row_number × column_number.
We need row_number × column_number = 10, with row_number from 1 to 10 (10 rows) and column_number from 1 to 4 (4 columns).
The factor pairs of 10 with column_number <= 4 are: column_number = 1 with row_number = 10, and column_number = 2 with row_number = 5. (column_number = 3 or 4 give non-integer row_numbers.)
Converting to 0-based array indices: row_number 10 -> i = 9; row_number 5 -> i = 4. column_number 1 -> j = 0; column_number 2 -> j = 1.
Address for (i=9, j=0): 1000 + (9×4 + 0) = 1036.
Address for (i=4, j=1): 1000 + (4×4 + 1) = 1017.
Both indices are within the array's bounds (rows 0-9, columns 0-3) -- unlike a literal reading of 'row number'/'column number' as the raw array index, which would force one location to need row index 10, impossible in a 10-row array.
Thus, addresses 1017 and 1036 hold the value 10.
A video solution is available for this question — log in and enroll to watch it.