Please help with this code

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

expected output:
1 2
9 7

my output
2 7 9 1
My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace 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.
Last edited on
Please, use code tags. Like when you did back in April 2014
http://www.cplusplus.com/forum/beginner/130025/
Last edited on
Sorry i didnt go in for too long. I kind of forgot the rule.Thank for your kind reminder.
Please explain the:
Array that is already arranged.

What does the "arranged" mean? That is not clear from the example.

It does look like result should have only unique values, but what about the other properties?
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
Lets ignore the 1-D array. How do you describe the ordering within the 2-D array?

The "natural" order of the underlying storage is presented here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <algorithm>

int main(){
    constexpr size_t Cols {3};
    int arr[][Cols] {4,8,7,6,5,2,1,9,3};
    size_t rows {0};
    for ( auto row : arr ) {
        for ( size_t x=0; x < Cols; ++x ) {
            std::cout << ' ' << row[x];
        }
        std::cout << '\n';
        ++rows;
    }
    std::cout << '\n';

    auto begin = &(arr[0][0]);
    std::sort( begin, begin + rows*Cols );

    for ( auto row : arr ) {
        for ( size_t x=0; x < Cols; ++x ) {
            std::cout << ' ' << row[x];
        }
        std::cout << '\n';
        ++rows;
    }
    std::cout << '\n';

    return 0;
}
Last edited on
Topic archived. No new replies allowed.