# include <iostream.h>
# include<conio.h>
void main( )
{
int matrix[3][3], sort[9], i, j, temp, k=0;
cout<<"\n Input 3 × 3 matrix elements : \n";
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>matrix[i][j];
for (i=0; i<3; i++) // copy into an array for sorting
for (j=0; j<3; j++)
sort[k++] = matrix[i][j];
for (i=0; i<9; i++) //sort the copied array
for (j=i+1; j<9; j++)
{
if (sort[i] > sort[j])
{
temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
cout<<"\n Sorted matrix is : \n ";
for ( k=0, i=0, j=0; k<9; k++) //copy back to the matrix
{
if (j == 3)
{
j = 0;
i ++;
cout<<"\n";
}
matrix[i][j] = sort[k];
cout << matrix[i][j]; //print sorted matrix.
j++;
}
getch( );
}
0 comments:
Post a Comment