input:
- First line:number of row and number of column.
- Line 2 to n: 2 dimensional array.
output:
2 Dimensional Array that is already arranged.
Example:Input
2 2
7 9
2 1
#include<iostream>
usingnamespace std;
int main(){
int m[99][99],n[255],a,b,temp;//a is num of row and b is num of col
int dem=0;
cin>>a;cin>>b;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
cin>>m[i][j];
}
}
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
n[dem]=m[i][j];
dem++;
}
}
for(int i=0;i<dem;i++){
for(int j=0;j<i;j++){
if(n[j]>n[j+1]){
temp=n[j];
n[j]=n[j+1];
n[j+1]=temp;
}
}
}
dem=0;
for(int i=0;i<a;i++){
if ((i%2==0) or (i==0)){
for(int j=0;j<b;j++){
m[i][j]=n[dem];
dem++;
}
}
else{
for (int j=b-1;j>=0;j--){
m[i][j]=n[dem];
dem++;
}
}
}
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
cout<<m[i][j];
}
}
return 0;
}
I try to solve the problem using 3 step:
1/Turn 2 dimension array into 1 dimension array.
2/Arrange the 1 dimension array into ascending order.
3/Turn 1 dimension array back to 2 dimension array.
All help are really appreciated.
Arranged here should be in ascending order.Sorry for the example not being clear since my habit of debugging is from low to high (like 2-3-4) and if the 1st test is failed, i will try to fix instead of using another test input:
Input:
3 3
487
652
193
After turning into 1 dimension array:487652193
After ordering:123456789
Output:
123
654
789