Power Rangers | Mortgages | Wedding Gifts | Librerias | Personal Finance
filling a matrix in C [Archive] - PCMech Forums

PDA

View Full Version : filling a matrix in C


Force Flow
11-02-2003, 03:52 PM
I'm having a problem trying to fill a 6x6 matrix in C

Filling it like this is a piece of cake:

0 1 1 1 1 1
-1 0 1 1 1 1
-1 -1 0 1 1 1
-1 -1 -1 0 1 1
-1 -1 -1 -1 0 1
-1 -1 -1 -1 -1 0


But I'm having a real hard time trying to fill it like this without having to define almost every single cell:

1 1 1 1 1 0
1 1 1 1 0 -1
1 1 1 0 -1 -1
1 1 0 -1 -1 -1
1 0 -1 -1 -1 -1
0 -1 -1 -1 -1 -1


Is there a way to invert the matrix from the "piece of cake" one?

Here's what I have so far, such as it is.

Force Flow
11-02-2003, 06:53 PM
Updated:

I got the diagonal line. Now I just need to figure out how to fill 2 sides. I'm puzzled in this respect.

Paul Victorey
11-02-2003, 07:26 PM
Well, let's look at a for loop:


for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
}
}


and let's put the values of i and j by the array:

| 0 1 2 3 4 5 = j
- - - - - -
0| 1 1 1 1 1 0
1| 1 1 1 1 0 -1
2| 1 1 1 0 -1 -1
3| 1 1 0 -1 -1 -1
4| 1 0 -1 -1 -1 -1
5| 0 -1 -1 -1 -1 -1
=
i


Now, what condition do we have a 1? If (5-j) > i
We have a 0 when (5 - j) == i
And a -1 when (5 - j) < i

Force Flow
11-02-2003, 08:06 PM
Great! It worked! Thanks a million, Paul Victorey :) It was driving me up the wall :p