The first problem is that you're printing the matrix on one line. Add
cout << '\n';
after lines 48 & 57.
It looks like you're doing selection sort, whereby you find the smallest element and put it in the first position. Then find the next smallest and put in the second position, etc.
In sort _matrix. you want i & j to traverse the entire matrix, so lines 11 and 13 should be:
1 2
|
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
|
Lines 19-26 should compare matrix[i][j] with matrix[k][l] and swap them if necessary. Get rid of pmin and do it directly:
1 2 3 4 5 6
|
if (matrix[k][l] < matrix[i][j]) {
// swap them
t = matrix[i][j];
matrix[i][j] = matrix[k][l];
matrix[k][l] = t;
}
|
Also, consider using std::swap() instead of swapping by hand.
Lastly comes the hardest part, how do you make k and l cover the items that come "after" i and j? k needs to start at i to handle other items on the same row, but j is tricky: is j+1 for the first row and 0 for the others. I solved this with a new variable called startl:
1 2 3 4 5 6 7 8 9 10 11 12
|
int startl = j+1;
for (int k = i; k < 4; k++) {
for (int l = startl; l < 4; l++) {
if (matrix[k][l] < matrix[i][j]) {
// swap them
t = matrix[i][j];
matrix[i][j] = matrix[k][l];
matrix[k][l] = t;
}
}
startl = 0;
}
|
Finally, I'll say that another way to do this is to treat the entire matrix as a 1D array of 16 items. I think the standard guarantees that they're stored this way.