+2 votes
in Class 11 by kratos

From a two-dimensional array A[4 x 4], write an algorithm to prepare a one dimensional array B[16] that will have all the elements of A as if they are stored in row-major form. For example for the following array:

the resultant array should be

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

1 Answer

+6 votes
by kratos
 
Best answer

include (iostream.h)

include (conio.h)

void main( )

{

int A[4][4], B[16];

// Input elements

for(int i = 0; i < 4 ; i++)

for( int j = 0; j < 4 ; j++)

{

cout<<"\n Enter elements for "<< i+1 << "," << j+1 << "location :";

cin >> A[i][j];

}

clrscr();

//Print the array

cout<<"\n The Original matrix : \n\n";

for( i = 0; i < 4 ; i++)

{

for( j = 0; j < 4 ; j++)

cout<< A[i][j]<<"\t";

cout<< "\n";

}

int k = 0;

// Convert 2 - D array into 1-D array by row-major rule

for( i = 0; i < 4 ; i++)

for( j = 0; j < 4 ; j++) B[k] = A[i][j];

//display the 1D Array

for( k=0; k<16; k++)

cout<< B[k] << " ";

getch( );

}

...