# Thread: Help in swapping matrix rows.

1. ## Help in swapping matrix rows.

Hello,
I need a small help in coding this issue in c, for example I have a matrix(4×3), I want to find the sum of it and then arrange it in descending order, and the sum of row should be connected with its row.
For example:
Matrix:
5 2 3
6 5 8
9 2 7
8 9 8
The sum:
10
19
18
25
The final result:
8 9 8 25
6 5 8 19
9 2 7 18
5 2 3 10

For real, I know how to arrange the sum, but I can't connect the matrix with it!

Here's my code:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(){
int i, j, n, a, b;
int y[4]={10, 19, 18, 25};
int x[4][3]={{5, 2, 3},
{6, 5, 8},
{9, 2, 7},
{8, 9, 8}};

for (i = 0; i < 3; ++i){
for (j = i + 1; j < 4; ++j){
if (y[i] < y[j]){
a = y[i];
y[i] = y[j];
y[j] = a;
for (n=0; n<3; n++){
b=x[j-1][n];
x[j-1][n]=x[j][n];
x[j][n]=b;
}
}
}
}

for (i=0; i<4; i++){
for (j=0; j<3; j++){
printf("%d ", x[i][j]);
}
if(j == 3){
j=0;
printf("%d\n", y[i]);
}
}
}```

2. I'm not entirely sure what you mean by "connect the matrix with it".
You could add an extra column and put the sum there.
Code:
```#include <stdio.h>
#include <stdlib.h>

#define ROWS 4
#define COLS 4   // one extra to hold the sum

// Compare function for qsort, sorting rows by last column.
int cmp(const void *a, const void *b) {
return ((int*)a)[COLS-1] - ((int*)b)[COLS-1];
}

int main() {
int m[ROWS][COLS] = {
{ 5, 2, 3, 0 },  // init sum column to 0
{ 6, 5, 8, 0 },
{ 9, 2, 7, 0 },
{ 8, 9, 8, 0 }
};

// Calculate sums.
for (int r = 0; r < ROWS; ++r)
for (int c = 0; c < COLS - 1; ++c)
m[r][COLS-1] += m[r][c];

// Sort.
qsort(m, ROWS, sizeof m[0], cmp);

// Print.
for (int r = 0; r < ROWS; ++r) {
for (int c = 0; c < COLS; ++c)
printf("%2d ", m[r][c]);
putchar('\n');
}

return 0;
}```

I have two arrays, the first is matrix, and the second is the sum. I don't know the sum of rows, I just enter random numbers into the matrix from keyboard, and I want to sort the sum in descending order with its corresponding row. So mainly, the numbers aren't entered by me, its from keyboard.

4. That makes no difference: you can still have an additional column for the sum.

If you're adamant about having a matrix such that the sum is not part of it, then assuming your matrix is in row-major order, you can take an index approach: declare a struct consisting of a pointer and a number. The pointer will point to the first entry in a row of the matrix; the number will be the sum of the entries of that row. You then create an array of these struct objects and sort them according to the sums. The result is that you will have the row pointers in sorted order according to the sum, and so this array will serve as an index to the matrix to access it in sorted order according to the sum of each row.

5. I know that there are many ways to solve this problem but in my case it's restricted, I should sort it without creating new arrays. And the array rows and columns are limited from the question so I can't add additional column.
If you don't mind, what's the problem with this part from my code?
Code:
```for (n=0; n<3; n++){
b=x[j-1][n];
x[j-1][n]=x[j][n];
x[j][n]=b;
}```

6. If you cannot add a new column and you cannot create another array, then you must implement the sort yourself because you then cannot use qsort to do what you want.

Originally Posted by amohammed63
If you don't mind, what's the problem with this part from my code?
What is that code supposed to do and how does it not work?

7. The code is supposed the row according to its sum, but here it swap the first row with the second, and the third with the foruth.
But it doesn't work, as well as, I'm supposed to create one function to sort the sum with its corresponding row. And I tried many ways, none of them worked.

8. Ah. To swap the rows of the matrix, use memcpy with a temporary array. It's easier than writing your own loop. If you must write your own loop, then the swapping will mirror the swapping of the sums: presently you're inexplicably involving j-1 in your swap, but that expression doesn't appear when you're swapping the sums.

To implement the sort, first implement the sort to sort the sums only. Compile and test to see it if works as expected. When you're sure that works, only then do you add the code to swap the corresponding rows of the matrix.

9. Thank you for your help, I'll try it and comment later if it worked or not.

10. The matrix before swapping:
5 2 3
6 5 8
9 2 7
8 9 8
When I run the code I provided above the result is:
6 5 8 25
8 9 8 19
5 2 3 18
9 2 7 10

So, that's what I got. And, I should write the loop myself with using functions like memcpy.

11. I repeat my advice from my previous post: to implement the sort, first implement the sort to sort the sums only. Compile and test to see it if works as expected. When you're sure that works, only then do you add the code to swap the corresponding rows of the matrix. The code to swap the corresponding rows of the matrix should mirror the code to swap the sums when sorting them, e.g., if you're swapping y[i] and y[j], then likewise you will swap x[i] and x[j], not x[j-1] and x[j].

By the way, x and y aren't very descriptive names. I would rename x to matrix (or if you have an even better name concerning what the matrix is for, use that) and y to sums or row_sums.

12. Thank you very much for your time, sir.
It works now after this change:
Code:
```for(n=0; n<3; n++){                    b=x[j][n];
x[j][n]=x[i][n];
x[i][n]=b;
}```