For example, for the following array
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The resultant array should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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";
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The resultant array should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#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( );
}
No comments:
Post a Comment