# Thread: swapping 2 rows in a matrix

1. ## 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. 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. how??

4. ## 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

5. Originally Posted by Shakti
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