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;
#include <iostream>
usingnamespace 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;
}
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.