| | |
please help
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 10
Reputation:
Solved Threads: 0
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:
it only displays the -1's i can't figure out how to place the 0s and 1s in the matrix
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:
C Syntax (Toggle Plain Text)
# 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
Last edited by John A; Sep 5th, 2009 at 1:59 am. Reason: added code tags
•
•
Join Date: Jul 2009
Posts: 908
Reputation:
Solved Threads: 145
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:
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)
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}};
Last edited by DdoubleD; Aug 29th, 2009 at 10:41 pm. Reason: added hard coded array syntax
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.
If you look at the all -1 elements, you will see a triangle-pattern.
To assign all of -1 elements in your 6x6 matrix without using any loop, I need to do this:
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
Now let turn those patterns into our loop.
Great, now we got shorter code, but again, I still see some pattern that I can shorten the code.
You can see that row of matrix keep increasing by one from one loop to next loop. So, I can shorten the code by:
Wait a minute!, but what should I put in that
Ah!, now you notice that whenever
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.
Finally we able to solve -1 pattern. but to complete your assignment, you need to be able to solve another 2 patterns.
and
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.
C Syntax (Toggle Plain Text)
-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.
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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
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.
C Syntax (Toggle Plain Text)
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[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;??? , since whenever j is changed, i is also changed. j cause i to change, so there must be some relationship between those two. C Syntax (Toggle Plain Text)
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)
j increase, i will decrease. As we have predicted above, they do have relationship between each other. Now we got another pattern: C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
// 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.
C Syntax (Toggle Plain Text)
0 0 0 0 0 0
C Syntax (Toggle Plain Text)
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.
Last edited by invisal; Aug 29th, 2009 at 11:33 pm.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
Join Date: Jul 2009
Posts: 10
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
# 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"); } }
Last edited by John A; Sep 5th, 2009 at 2:00 am. Reason: added code tags
![]() |
Other Threads in the C Forum
- Previous Thread: copying text from a text file to a string?
- Next Thread: for lexical analyzer
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi





