Matrix sorting

Hello guys, I'm having a issue when it comes to sorting matrix. I got a code but I still don't understand how thw programmer made the compiler understand that the matrix is a simple array and sort it;
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
#include <iostream>
using namespace std;

void bubbleSort(int a[], int tam){
    int aux;
    for (int i = tam-1; i >= 1; i--) {  
	for ( int j=0; j < i ; j++) {
	    if(a[j] > a[j+1]) {
		aux = a[j];
		a[j] = a[j+1];
		a[j+1] = aux;
	    }
	}
    }
}



int main(){
    int m, n;
    cin >> m >>n;
    int a[m][n];
    for(int i=0;i<m;i++){
	for(int j=0;j<n;j++){
	cin >> a[i][j];
	}
    }
    int *b;
    b = (int *) a;
    bubbleSort(b, m*n);

    return 0;
}



A static array is contiguous in memory:
http://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays/
However,
https://stackoverflow.com/questions/2832970/does-c99-guarantee-that-arrays-are-contiguous

More importantly, the program is not legal C++. The size of static array must be a compile-time constant. It is not in this program; the size depends on user input. The program should use dynamically allocated array.
Last edited on
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <algorithm>

int main()
{
    // array of 3 array of 3 int
    int matrix[3][3] { {12,78,21}, {55,18,78}, {92,41,50} } ;
    int (*pm)[3] = matrix ; // pointer to first element of matrix (pointer to 'array of 3 int')
    int *pi = (int*)pm ; // cast pm (pointer to 'array of 3 int') to 'pointer to int'

    // pm             pm+1           pm+2            pm+3
    // |               |               |               |
    // v               v               v               v
    // --------------------------------------------------
    // ||    |    |    ||    |    |    ||    |    |    ||
    // || 12 | 78 | 21 || 55 | 18 | 78 || 92 | 41 | 18 ||
    // ||    |    |    ||    |    |    ||    |    |    ||
    // --------------------------------------------------
    // ^     ^    ^    ^     ^    ^    ^     ^    ^    ^
    // |     |    |    |     |    |    |     |    |    |
    // pi  pi+1 pi+2 pi+3  pi+4 pi+5 pi+6  pi+7 pi+8 pi+9
    
    // print the 3x3 matrix
    for( int i = 0 ; i < 3 ; ++i )
    {
        for( int j = 0 ; j < 3 ; ++j ) std::cout << pm[i][j] << ' ' ;
        std::cout << '\n' ;
    }

    // print it as an array of 9 int
    std::cout << "---------------------\n" ;
    for( int i = 0 ; i < 9 ; ++i ) std::cout << pi[i] << ' ' ;
    std::cout << '\n' ;


    // sort the matrix as if it is a single array of 9 int
    std::cout << "\nsort the integers\n\n" ;
    std::sort( pi, pi+9 ) ;

    // result of the sort
    // pm             pm+1           pm+2            pm+3
    // |               |               |               |
    // v               v               v               v
    // --------------------------------------------------
    // ||    |    |    ||    |    |    ||    |    |    ||
    // || 12 | 18 | 21 || 41 | 50 | 55 || 78 | 78 | 92 ||
    // ||    |    |    ||    |    |    ||    |    |    ||
    // --------------------------------------------------
    // ^     ^    ^    ^     ^    ^    ^     ^    ^    ^
    // |     |    |    |     |    |    |     |    |    |
    // pi  pi+1 pi+2 pi+3  pi+4 pi+5 pi+6  pi+7 pi+8 pi+9

    // print it as an array of 9 int
    for( int i = 0 ; i < 9 ; ++i ) std::cout << pi[i] << ' ' ;
    std::cout << '\n' ;

    // print the 3x3 matrix
    std::cout << "---------------------\n" ;
    for( int i = 0 ; i < 3 ; ++i )
    {
        for( int j = 0 ; j < 3 ; ++j ) std::cout << pm[i][j] << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/f1ce573be66fe26c
Topic archived. No new replies allowed.