loop building question

This is a discussion on loop building question within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to build a loop that makes a matrix diagnolly dominant (I'm not sure if thats the ...

loop building question

Hello, I am trying to build a loop that makes a matrix diagnolly dominant (I'm not sure if thats the right term... I mean where the values on the diagnol are from greatest to least... like this

like if I was given this:

Code:

2 3 7 8
9 2 3 4
5 9 0 6
3 8 4 3

I would get this:

Code:

9 2 3 4
3 8 4 3
2 3 7 8
5 9 0 6

with 9,8,7,6 making this diagnolly dominant.

I'm not looking for code but am wondering if anyone has any ideas on the simplest way to go about this...

I'm having a hard time thinking on how to approach this, I'm thinking

1) compare the entire first column
2) take the line with the largest first column value and switch
it with first line
3) lock the first line then compare colum 2 in the following 3 lines
put largest value for column 2 line as second line
4) lock the 2nd line check the third column last two lines
if the 3rd line has the larger value switch those as well....

That handles putting the highest values in the diagonal. Your second part of ordering that diagonal the way you do is slightly more difficult because it assumes that the succeeding row has at least one value less than the highest value in the last row.

This will order it properly and when implemented in a simple program I wrote, I see that it does work correctly.

Output:

Code:

8 6 6 1
5 4 3 6
5 1 2 3
4 2 4 1
8 4 6 1
5 6 2 6
5 1 4 1
4 2 3 3

...but these were random numbers between 1 and 9, where there is a good chance of not running into the situation of succeeding row not having at least one value less than the highest value in the last row. If I were to say do random numbers between 1-50, I would run into the error more often.

No, that's not what my code does. It orders the row putting the greatest value (that is less than the previous diagonal value) into the diagonal. Look at my output again.

Code:

8 6 6 1
5 4 3 6
5 1 2 3
4 2 4 1
8 4 6 1
5 6 2 6
5 1 4 1
4 2 3 3

The two things you'll have to add for checks is: One, you have to make sure there isn't a higher value in the diagonal initally before the swap. Two, you have to make sure each column has at least one value lower than the preceding column.

I've been looking on google for some kind of sorting algorithm that would help with this, but I can't find any... I think this might be called a diagnolly dominant matrix... but I can't find any algrithms on it.... I'm suppose to do it by only using if and do loops....

I'm doing this:
1) compare the entire first column
2) take the line with the largest first column value and switch
it with first line
3) lock the first line then compare colum 2 in the following 3 lines
put largest value for column 2 line as second line
4) lock the 2nd line check the third column last two lines
if the 3rd line has the larger value switch those as well....

but when I get to the last coumn the previous three rows I have locked where I'm not checking agains them anymore... and I get stuck at this point in my thinking