Thread: swapping 2 rows in a matrix

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    10

    Unhappy swapping 2 rows in a matrix

    hey guys!..
    i am writing a code that solves 6 unknowns using
    gaussjordan method..
    but i am stuck in making a code that swaps 2 rows
    if the pivot is zero (0)..
    please give me an idea how to swap a row
    with zero pivot to a row that is non-zero pivot..
    thanks!.

    here is my code:

    //manipulating the equations using Gauss-Jordan M.
    Code:
        for (m = 1; m <= 6; m++) // number of rows
        {
            float T = matrix[m][m];
            
            for (e = 1; e <= 7; e++) { // number of columns
                matrix[m][e] = matrix[m][e]/T; } // Stores Multipliers.
            
            for (r = 1; r <= 6; r++) // to create zeros below & above the diagonal.
            {
                T = matrix[r][m];
                if (r != m) {
                      for (e = 1; e <= 7; e++){ // number of columns 
                          matrix[r][e] = matrix[r][e] - (T * matrix[m][e]); } }
            }
        }
    i just need to put something when my matrix[m][m] = 0;

    thanks in advance!.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Store row A in a temporary array, set row A to contain the same as row B, set row B to contain what is stored in temporary array.

    I would make a function out of it for easy reading.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    how??

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    consejitos

    if you are studying problems of this type and have got as far as your code posted you should EASILY be able to follow the advice given already. also have you initialised your arrays? if not u are performing operations on garbage, also you understand counting indicies begin at 0 for arrays? unless you never reference element 0 you will have problems here
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    Quote Originally Posted by Shakti View Post
    Store row A in a temporary array, set row A to contain the same as row B, set row B to contain what is stored in temporary array.

    I would make a function out of it for easy reading.
    hey thanks!..
    i got it..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM