943,641 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 617
  • C RSS
Aug 29th, 2009
0

please help

Expand Post »
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:

  1. # include <stdio.h>
  2. # define r 6
  3. # define c 6
  4.  
  5. void matrix (int a[r][c]);
  6. void display(int a[r][c]);
  7.  
  8. int main()
  9. {
  10. int table[r][c];
  11.  
  12. matrix(table);
  13. display(table);
  14. return 0;
  15. }
  16.  
  17. void matrix (int b[r][c])
  18. {
  19. int i, j;
  20. for(i=0;i<r;i++)
  21. {
  22. for(j=0;j<c;j++)
  23. {
  24. b[i][j]=-1;
  25. }
  26. }
  27. }
  28. void display(int b[r][c])
  29. {
  30. int i, j;
  31. for(i=0;i<r;i++)
  32. {
  33. for(j=0;j<c;j++)
  34. {
  35. printf("%d",b[i][j]);
  36. }
  37. printf("\n");
  38. }
  39. }



it only displays the -1's i can't figure out how to place the 0s and 1s in the matrix
Last edited by John A; Sep 5th, 2009 at 1:59 am. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
D_switch is offline Offline
15 posts
since Jul 2009
Aug 29th, 2009
0

Re: please help

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:
cpp Syntax (Toggle Plain Text)
  1. int table[r][c] = {{-1,-1,-1,-1,-1, 0},
  2. {-1,-1,-1,-1,0,1},
  3. {-1,-1,-1,0,1,1},
  4. {-1,-1,0,1,1,1},
  5. {-1,0,1,1,1,1},
  6. {0,1,1,1,1,1}};
Last edited by DdoubleD; Aug 29th, 2009 at 10:41 pm. Reason: added hard coded array syntax
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 29th, 2009
0

Re: please help

Also, you could modify your print loops to accomplish the logical pattern. What is the requirement?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 29th, 2009
3

Re: please help

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 -1 0
  2. -1 -1 -1 -1 0 1
  3. -1 -1 -1 0 1 1
  4. -1 -1 0 1 1 1
  5. -1 0 1 1 1 1
  6. 0 1 1 1 1 1

If you look at the all -1 elements, you will see a triangle-pattern.
  1. i=0 1 2 3 4
  2. ------------------------------
  3. j=0 -1 -1 -1 -1 -1
  4. j=1 -1 -1 -1 -1
  5. j=2 -1 -1 -1
  6. j=3 -1 -1
  7. j=4 -1

To assign all of -1 elements in your 6x6 matrix without using any loop, I need to do this:

  1. matrix[0][0] = -1
  2. matrix[0][1] = -1
  3. matrix[0][2] = -1
  4. matrix[0][3] = -1
  5. matrix[0][4] = -1
  6.  
  7. matrix[1][0] = -1
  8. matrix[1][1] = -1
  9. matrix[1][2] = -1
  10. matrix[1][3] = -1
  11.  
  12. matrix[2][0] = -1
  13. matrix[2][1] = -1
  14. matrix[2][2] = -1
  15.  
  16. matrix[3][0] = -1
  17. matrix[3][1] = -1
  18.  
  19. 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
The pattern for the first group: matrix[0][0->4] = -1

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

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

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

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

Now let turn those patterns into our loop.
  1. int i;
  2.  
  3. // matrix[0][0->4]
  4. for(i = 0; i<=4; i++)
  5. matrix[0][i] = -1
  6.  
  7. // matrix[1][0->3]
  8. for(i = 1; i<=3; i++)
  9. matrix[1][i] = -1
  10.  
  11. // matrix[2][0->2]
  12. for(i = 2; i<=2; i++)
  13. matrix[2][i] = -1
  14.  
  15. // matrix[3][0->1]
  16. for(i = 3; i<=1; i++)
  17. matrix[3][i] = -1
  18.  
  19. // matrix[4][0]
  20. for(i = 4; i<=0; i++)
  21. 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[0][i] = -1   // row of matrix =0

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

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

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

for(i = 0; i<=0; i++)
   matrix[4][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<=???; 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.

  1. When j = 0, i = 0 -> 4 (4 - j = 4 - 0 = 4)
  2. When j = 1, i = 0 -> 3 (4 - j = 4 - 1 = 3)
  3. When j = 2, i = 0 -> 2 (4 - j = 4 - 2 = 2)
  4. When j = 3, i = 0 -> 1 (4 - j = 4 - 3 = 1)
  5. 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:

  1. for (int j=0; j<=4; j++)
  2. for (int i=0; i<=4-j; i++)
  3. 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.

  1. // For 6x6 matrix
  2. for (int j=0; j<=4; j++) // j <= 4 = 6 - 2 = MAX_ROW - 2
  3. for (int i=0; i<=4-j; i++) // i <= 4 = 6 - 2 = MAX_COL - 2
  4. matrix[j][i] = -1;
  5.  
  6. // For 7x7 matrix
  7. for (int j=0; j<=5; j++) // j <= 5 = 7 - 2 = MAX_ROW - 2
  8. for (int i=0; i<=5-j; i++) // i <= 5 = 7 - 2 = MAX_COL - 2
  9. matrix[j][i] = -1;
  10.  
  11. // For 8x8 matrix
  12. for (int j=0; j<=6; j++) // j <= 6 = 8 - 2 = MAX_ROW - 2
  13. for (int i=0; i<=6-j; i++) // i <= 6 = 8 - 2 = MAX_COL - 2
  14. matrix[j][i] = -1;
  15.  
  16. ....
  17. ...
  18. ...
  19. // MAX_ROW x MAX_COL matrix
  20. for (int j=0; j<=MAX_ROW - 2; j++)
  21. for (int i=0; i<=MAX_COL- 2 - j; i++)
  22. 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.

  1. 0
  2. 0
  3. 0
  4. 0
  5. 0
  6. 0
and
  1. 1
  2. 1 1
  3. 1 1 1
  4. 1 1 1 1
  5. 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.
Last edited by invisal; Aug 29th, 2009 at 11:33 pm.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Sep 4th, 2009
0

Re: please help

  1. # include <stdio.h>
  2. # define rows 6
  3. # define cols 6
  4.  
  5. void FillArray (int a[rows][cols]);
  6. void Manipulate_A(int a[rows][cols]);
  7. void Manipulate_B(int a[rows][cols]);
  8. void Display(int a[rows][cols]);
  9.  
  10. int main()
  11. {
  12. int p[rows][cols];
  13.  
  14. FillArray (p);
  15. Manipulate_A(p);
  16. Manipulate_B(p);
  17. Display(p);
  18. return 0;
  19. }
  20.  
  21. void FillArray (int a[rows][cols])
  22. {
  23. int i,j;
  24. for(i=0;i<rows;i++)
  25. {
  26. for(j=0;j<cols;j++)
  27. {
  28. a[i][j]=1;
  29. }
  30. }
  31. }
  32. void Manipulate_A(int a[rows][cols])
  33. {
  34. int i,j;
  35. for(i=0;i<=4;i++)
  36. {
  37. for(j=0;j<=4-i;j++)
  38. {
  39. a[i][j]=-1;
  40. }
  41. }
  42. }
  43. void Manipulate_B(int a[rows][cols])
  44. {
  45. int s,d;
  46. for(s=5,d=0;s>0,d<6;s--,d++)
  47. {
  48. a[s][d]=0;
  49. }
  50. }
  51. void Display(int a [rows][cols])
  52. {
  53. int i,m;
  54. for(i=0;i<rows;i++)
  55. {
  56. for(m=0;m<cols;m++)
  57. {
  58. printf("%d\t", a[i][m]);
  59. }
  60. printf("\n");
  61. }
  62. }
thank you for all the help
Last edited by John A; Sep 5th, 2009 at 2:00 am. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
D_switch is offline Offline
15 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: sizeof operator
Next Thread in C Forum Timeline: for lexical analyzer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC