Wednesday, May 16, 2012

Pattern Finder in all DIRECTIONS in a GRID of numbers -C program

/* Pattern Finder in all DIRECTIONS in a GRID of numbers -C program
you are allowed to enter a pattern of any length then the ROW and
COL of the pattern occurence is shown in the output.
pattern is searched in ALL THE EIGHT DIRECTIONS */


#include stdio.h #include string.h short int checkPattern(int,int,int); short cp[8][8]={5,4,1,2,3,4,5,6, 6,3,1,2,3,4,5,5, 5,7,5,3,5,1,3,1, 4,1,7,4,3,4,5,1, 3,2,3,4,5,1,5,2, 2,2,5,2,1,4,1,3, 1,4,7,8,6,5,4,4, 6,5,3,2,6,7,8,5}; char p[5]; void main() { int i,j,k;
char dum[5]; clrscr(); printf("\n collection we have is: \n"); for(i=0;i<8;i++) { for(j=0;j<8;j++) printf("%d ", cp[i][j]); printf("\n"); } printf("\n\n enter the pattern: "); scanf("%s",p); for(i=0;i<8;i++) { for(j=0;j<8;j++) { if( (48+cp[i][j]) == *p ) { if( checkPattern(i,j,1) ) printf("\n\nFORWARD pattern at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,2) ) printf("\n\n DOWNWARD pattern at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,3) ) printf("\n\ncross pattern(right downards) at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,4) ) printf("\n\nREVERSE pattern at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,5) ) printf("\n\ncross pattern(left downwards) at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,6) ) printf("\n\ncross pattern(right upwards) at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,7) ) printf("\n\ncross pattern(left upwards) at row-%d col-%d", (i+1),(j+1)); if( checkPattern(i,j,8) ) printf("\n\n UPWARD pattern at row-%d col-%d", (i+1),(j+1)); } } } getch(); } short int checkPattern(int r, int c, int patternType) { int s,count=0; char *d; d=p; s=strlen(d); while( (*d)!='\0' ) { if( (48+cp[r][c])== *d) { switch(patternType) { case 1: c++; count++; if(c>7) break; break; case 2: r++; count++; if(r>7) break; break; case 3: r++;c++; count++; if(r>7 || c>7) break; break; case 4: c--; count++; if(c<0) break; break; case 5: c--;r++; count++; if(r>7 || c<0) break; break; case 6: r--;c++; count++; if(r<0 || c>7) break; break; case 7: r--;c--; count++; if(r<0 || c<0) break; break; case 8: r--; count++; if(r<0) break; break; } } else break; d++; } if(count==s) return 1; else return 0; }

No comments:

Post a Comment