Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following code to initialize an array in C++: 1) Compare the execut

ID: 3565821 • Letter: C

Question

Consider the following code to initialize an array in C++:

1) Compare the execution speed between the code on the left and the code on the right and justify your answer.

2) Suppose the address for A[1][1] is 10000 (decimal number), double takes 8 bytes, and byte addressing is used, what are the addresses for A[40, 50] and the last element in the array?

Consider the following code to initialize an array in C++: 1) Compare the execution speed between the code on the left and the code on the right and justify your answer. 2) Suppose the address for A[1][1] is 10000 (decimal number), double takes 8 bytes, and byte addressing is used, what are the addresses for A[40, 50] and the last element in the array?

Explanation / Answer

first is Row major and second is Column major.

The difference between row-major and column-major order is simply that the order of the dimensions is reversed. Equivalently, in row-major order the rightmost indices vary faster as one steps through consecutive memory locations, while in column-major order the leftmost indices vary faster.

Row major is faster than Coloumn major because of contiguous memory allocation.

Row-major order is used in C/C++, Mathematica, PL/I, Pascal, Python, Speakeasy, SAS and others.

Column-major order is used in Fortran, OpenGL and OpenGL ES, MATLAB,[2] GNU Octave, S-Plus,[3] R,[4] Julia, Rasdaman, and Scilab.

2)

Address of A[ I ][ J ]th element = BA + [ N * ( I - LBR) + ( J - LBC) ] * W


Where BA = Base address
W = Number of bytes occupied by each element
N = Number of columns

A[40][50]= 10000 +[100 * (40-1)+(50-1)]*8

10000+[8800]*8

80400

A[99][99]= 10000 +[100 * (99-1)+(99-1)]*8

10000+[19600]*8

166800

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote