ei guys
i really need help on how to give an output like this:

-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

im stuck in this part of my program:

# include <stdio.h>
# define r 6
# define c 6

void matrix (int a[r][c]);
void display(int a[r][c]);

int main()
{
  int table[r][c];
  
  matrix(table);
  display(table);
  return 0;
}

void matrix (int b[r][c])
{
  int i, j;
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      b[i][j]=-1;
     }
  }
}
void display(int b[r][c])
{
  int i, j;
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
     printf("%d",b[i][j]);
     }
    printf("\n");
  }
}

it only displays the -1's i can't figure out how to place the 0s and 1s in the matrix

Please use code tags.

In your matrix function, you are assigning -1 to all elements. If you want to assign the pattern of numbers you specified you would like to output, then you need to assign them as such.

Having said that, do you want to put them into your matrix completely hard coded? Probably not, but I don't know. If not, you need to modify your loops to contain logic that will assign your matrix according to the pattern. If so, then it's an easier thing to achieve because you don't need any logic.

Syntax for hard-coded array:

int table[r][c] = {{-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}};

Also, you could modify your print loops to accomplish the logical pattern. What is the requirement?

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
[B]The pattern for the first group: matrix[0][0->4] = -1[/B]

matrix[1][0] = -1
matrix[1][1] = -1
matrix[1][2] = -1
matrix[1][3] = -1
[B]The pattern for the second group: matrix[1][0->3] = -1[/B]

matrix[2][0] = -1
matrix[2][1] = -1
matrix[2][2] = -1
[B]The pattern for the third group: matrix[2][0->2] = -1[/B]

matrix[3][0] = -1
matrix[3][1] = -1
[B]The pattern for the forth group: matrix[3][0->1] = -1[/B]

matrix[4][0] = -1
[B]The pattern for the firth group: matrix[4][0] = -1[/B]

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[[B]0[/B]][i] = -1   // row of matrix =0

for(i = 0; i<=3; i++)
   matrix[[B]1[/B]][i] = -1   // row of matrix =1

for(i = 0; i<=2; i++)
   matrix[[B]2[/B]][i] = -1   // row of matrix =2

for(i = 0; i<=1; i++)
   matrix[[B]3[/B]][i] = -1   // row of matrix =3

for(i = 0; i<=0; i++)
   matrix[[B]4[/B]][i] = -1    // row of matrix =4

You can see that row 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<=[B]???[/B]; 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.

commented: Wow! Ok, I need some lottery numbers.... +1
commented: Reps for the long post, probably took 32.15 min out of your life? +3
commented: Great post :) +21
# include <stdio.h>
# define rows 6
# define cols 6

void FillArray (int a[rows][cols]);
void Manipulate_A(int a[rows][cols]);
void Manipulate_B(int a[rows][cols]);
void Display(int a[rows][cols]);

int main()
{
	int p[rows][cols];

	FillArray (p);
	Manipulate_A(p);
	Manipulate_B(p);
	Display(p);
	return 0;
}

void FillArray (int a[rows][cols])
{
	int i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<cols;j++)
		{
			a[i][j]=1;
		}
	}
}
void Manipulate_A(int a[rows][cols])
{
	int i,j;
	for(i=0;i<=4;i++)
	{
		for(j=0;j<=4-i;j++)
		{
			a[i][j]=-1;
		}
	}
}
void Manipulate_B(int a[rows][cols])
{
	int s,d;
	for(s=5,d=0;s>0,d<6;s--,d++)
	{
		a[s][d]=0;
	}
}
void Display(int a [rows][cols])
{
	int i,m;
	for(i=0;i<rows;i++)
	{
		for(m=0;m<cols;m++)
		{
			printf("%d\t", a[i][m]);
		}
		printf("\n");
	}
}

thank you for all the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.