All your code do is assigning each element of your matrix to -1 and then display them. It is no surprise at all that you did not achieve the assignment because you haven't figured or aren't willing to figured the pattern. Everything in this world is patterned either simple or complex pattern. In your assignment, the pattern is pretty obvious.
-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
If you look at the all -1 elements, you will see a triangle-pattern.
i=0 1 2 3 4
------------------------------
j=0 -1 -1 -1 -1 -1
j=1 -1 -1 -1 -1
j=2 -1 -1 -1
j=3 -1 -1
j=4 -1
To assign all of -1 elements in your 6x6 matrix without using any loop, I need to do this:
matrix[0][0] = -1
matrix[0][1] = -1
matrix[0][2] = -1
matrix[0][3] = -1
matrix[0][4] = -1
matrix[1][0] = -1
matrix[1][1] = -1
matrix[1][2] = -1
matrix[1][3] = -1
matrix[2][0] = -1
matrix[2][1] = -1
matrix[2][2] = -1
matrix[3][0] = -1
matrix[3][1] = -1
matrix[4][0] = -1
But by assigning these -1 elements in your matrix with bare hand is pain in the ass, so I need to look for some pattern in what I have done with bare-hand
matrix[0][0] = -1
matrix[0][1] = -1
matrix[0][2] = -1
matrix[0][3] = -1
matrix[0][4] = -1
<strong>The pattern for the first group: matrix[0][0->4] = -1</strong>
matrix[1][0] = -1
matrix[1][1] = -1
matrix[1][2] = -1
matrix[1][3] = -1
<strong>The pattern for the second group: matrix[1][0->3] = -1</strong>
matrix[2][0] = -1
matrix[2][1] = -1
matrix[2][2] = -1
<strong>The pattern for the third group: matrix[2][0->2] = -1</strong>
matrix[3][0] = -1
matrix[3][1] = -1
<strong>The pattern for the forth group: matrix[3][0->1] = -1</strong>
matrix[4][0] = -1
<strong>The pattern for the firth group: matrix[4][0] = -1</strong>
Now let turn those patterns into our loop.
int i;
// matrix[0][0->4]
for(i = 0; i<=4; i++)
matrix[0][i] = -1
// matrix[1][0->3]
for(i = 1; i<=3; i++)
matrix[1][i] = -1
// matrix[2][0->2]
for(i = 2; i<=2; i++)
matrix[2][i] = -1
// matrix[3][0->1]
for(i = 3; i<=1; i++)
matrix[3][i] = -1
// matrix[4][0]
for(i = 4; i<=0; i++)
matrix[4][i] = -1
Great, now we got shorter code, but again, I still see some pattern that I can shorten the code.
for(i =0; i<=4; i++)
matrix[<strong>0</strong>][i] = -1 // row of matrix =0
for(i = 0; i<=3; i++)
matrix[<strong>1</strong>][i] = -1 // row of matrix =1
for(i = 0; i<=2; i++)
matrix[<strong>2</strong>][i] = -1 // row of matrix =2
for(i = 0; i<=1; i++)
matrix[<strong>3</strong>][i] = -1 // row of matrix =3
for(i = 0; i<=0; i++)
matrix[<strong>4</strong>][i] = -1 // row of matrix =4
You can see thatrow of matrix keep increasing by one from one loop to next loop. So, I can shorten the code by:
for (int j=0; j<=4; j++)
for (int i=0; i<=<strong>???</strong>; i++)
matrix[j][i] = -1;
Wait a minute!, but what should I put in that ??? , since whenever j is changed, i is also changed. j cause i to change, so there must be some relationship between those two.
When j = 0, i = 0 -> 4 (4 - j = 4 - 0 = 4)
When j = 1, i = 0 -> 3 (4 - j = 4 - 1 = 3)
When j = 2, i = 0 -> 2 (4 - j = 4 - 2 = 2)
When j = 3, i = 0 -> 1 (4 - j = 4 - 3 = 1)
When j = 4, i = 0 (4 - j = 4 - 4 = 0)
Ah!, now you notice that whenever j increase, i will decrease. As we have predicted above, they do have relationship between each other. Now we got another pattern:
for (int j=0; j<=4; j++)
for (int i=0; i<=4-j; i++)
matrix[j][i] = -1;
But wait. this code only apply with 6x6 matrix, what if I want my code to work with every MAX_ROW x MAX_COL matrix.
// For 6x6 matrix
for (int j=0; j<=4; j++) // j <= 4 = 6 - 2 = MAX_ROW - 2
for (int i=0; i<=4-j; i++) // i <= 4 = 6 - 2 = MAX_COL - 2
matrix[j][i] = -1;
// For 7x7 matrix
for (int j=0; j<=5; j++) // j <= 5 = 7 - 2 = MAX_ROW - 2
for (int i=0; i<=5-j; i++) // i <= 5 = 7 - 2 = MAX_COL - 2
matrix[j][i] = -1;
// For 8x8 matrix
for (int j=0; j<=6; j++) // j <= 6 = 8 - 2 = MAX_ROW - 2
for (int i=0; i<=6-j; i++) // i <= 6 = 8 - 2 = MAX_COL - 2
matrix[j][i] = -1;
....
...
...
// MAX_ROW x MAX_COL matrix
for (int j=0; j<=MAX_ROW - 2; j++)
for (int i=0; i<=MAX_COL- 2 - j; i++)
matrix[j][i] = -1;
Finally we able to solve -1 pattern. but to complete your assignment, you need to be able to solve another 2 patterns.
0
0
0
0
0
0
and
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
I will leave these two for you to solve.Note: there are many ways to find pattern of your assignment. the above is just my way of finding pattern.