Thread: Help in swapping matrix rows.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    30

    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. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    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;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Sadly, not that way.
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 04-19-2020 at 02:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    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. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.

    Quote 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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 04-19-2020 at 02:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    Thank you for your help, I'll try it and comment later if it worked or not.

  10. #10
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    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. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2020
    Posts
    30
    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;
                    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping rows and columns in 2D arrray
    By 997.2 in forum C Programming
    Replies: 10
    Last Post: 05-11-2012, 12:21 PM
  2. how to get rows in a matrix when only columns are given
    By torquemada in forum C Programming
    Replies: 11
    Last Post: 09-13-2011, 07:57 PM
  3. swapping 2 rows in a matrix
    By 26friends26 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2010, 07:03 AM
  4. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  5. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:04 PM

Tags for this Thread