Thread: Don't know why is this keep printing...

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    10

    Lightbulb Don't know why is this keep printing...

    Hi guys
    I have problem to understand why my program isn't working well..
    The goal is to find all 21 words in the 2d char array

    that's my code, I think my code can find all of them (not sure) but I can't know because he keep printing the same words over and over ...

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <stdbool.h>
    
    
    #define MATRIX_SIZE 4
    
    
    
    
    bool isWord(char* s){
    
    
            return (!strcmp(s,"CAT") |
                !strcmp(s,"CATS") |
                !strcmp(s,"TRAM") |
                !strcmp(s,"TRAMS") |
                !strcmp(s,"TAME") |
                !strcmp(s,"CAR") |
                !strcmp(s,"CARS") |
                !strcmp(s,"RAT") |
                !strcmp(s,"RATS") |
                !strcmp(s,"RAMP") |
                !strcmp(s,"ART") |
                !strcmp(s,"CART") |
                !strcmp(s,"STAMP") |
                !strcmp(s,"TAKEN") |
                !strcmp(s,"MEN") |
                !strcmp(s,"MAKE") |
                !strcmp(s,"TAKE") |
                !strcmp(s,"ATE") |
                !strcmp(s,"SELL") |
                !strcmp(s,"STEEL") |
                !strcmp(s,"RACE") |
                !strcmp(s,"RAKE") );
    
    
    }
    
    
    void append(char* s, char c){
    int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
    
    
    }
    
    int f(char* s, int x, int y, int visitedMap[MATRIX_SIZE][MATRIX_SIZE], char A[MATRIX_SIZE][MATRIX_SIZE]){
    
    
    int i,j, maxRow, minRow, maxCol, minCol;
    
        if(isWord(s))
             printf("%s\n", s);
    
    
    ///ROW
        if(x-1>0)
            maxRow=x-1;
        else
            maxRow=0;
    
    
        if(x+2<MATRIX_SIZE)
            minRow=x+2;
        else
            minRow=MATRIX_SIZE;
    
    
         ///COL
         if(y-1>0)
            maxCol=y-1;
        else
            maxCol=0;
    
    
        if(y+2<MATRIX_SIZE)
            minCol=y+2;
        else
            minCol=MATRIX_SIZE;
    
     for (i = maxRow; i < minRow; i++) {
            for (j = maxCol; j <minCol; j++) {
                if (visitedMap[i][j] == 1)
                    continue;
                else{
                    int newVisitedMap[MATRIX_SIZE][MATRIX_SIZE];
                    memset(newVisitedMap, 0, sizeof(newVisitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
                    int p,q;
                    for (p = 0; p < MATRIX_SIZE; p++) {
                        for (q = 0; q < MATRIX_SIZE; q++) {
                           newVisitedMap[p][q] = visitedMap[p][q];
                        }
                    }
                     newVisitedMap[i][j] = 1;
    
    
                     append(s, A[i][j]);
    
    
                     if(isWord(s)){
                        printf("%s\n", s);
                        return;
                     }
    
                    f(s,i,j,newVisitedMap, A);
    
    
                }///END - ELSE
    
    
                memset(s, 0, sizeof(s));
            }///END - FOR(2)
     }///END - FOR(1)
    
    }
    
    int printWords(char A[MATRIX_SIZE][MATRIX_SIZE]){
    
    char* s;
    int i,j;
    memset(s, 0, sizeof(s));
    
      int visitedMap[MATRIX_SIZE][MATRIX_SIZE];
      ///reset the array
      memset(visitedMap, 0, sizeof(visitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
    
     for(i=0;i<MATRIX_SIZE;i++){
        for(j=0;j<MATRIX_SIZE;j++){
    
            append(s, A[i][j]);
            visitedMap[i][j] = 1;
    
            f(s,i,j,visitedMap,A);
            memset(s, 0, sizeof(s));
             memset(visitedMap, 0, sizeof(visitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
        }
     }
    
    
     }
    
    
     void printMatrix(char A[MATRIX_SIZE][MATRIX_SIZE]){
    int i,j;
    printf("The matrix is: \n\n");
    for(i=0;i<MATRIX_SIZE;i++){
        for(j=0;j<MATRIX_SIZE;j++){
            printf("| %c |", A[i][j]);
        }
        printf("\n");
    }
    printf("\nThe word we found are: \n\n");
    }
    
    
    int main()
    {
    char A[MATRIX_SIZE][MATRIX_SIZE]={{'C', 'A', 'R', 'T'}, {'E', 'T', 'A', 'K'}, {'E', 'S', 'M', 'E'}, {'L', 'L', 'P', 'N'}};
    
    
    printMatrix(A);
    printWords(A);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Line 120
    Your s pointer doesn't pointer anywhere, so all useage of it is broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    Quote Originally Posted by Salem View Post
    Line 120
    Your s pointer doesn't pointer anywhere, so all useage of it is broken.
    can u explain this pls? I'm kinda new to C, Know java but don't really get what u said..

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    what are you using to check compile time warning an errors? That's a big clue why your code is not working properly.
    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "term2" "term2.c" (in directory: /home/userx/bin)
    term2.c: In function 'isWord':
    term2.c:14:18: warning: implicit declaration of function 'strcmp' [-Wimplicit-function-declaration]
             return (!strcmp(s,"CAT") |
                      ^
    term2.c: In function 'append':
    term2.c:42:11: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     int len = strlen(s);
               ^
    term2.c:42:11: warning: incompatible implicit declaration of built-in function 'strlen'
    term2.c:42:11: note: include '<string.h>' or provide a declaration of 'strlen'
    term2.c: In function 'f':
    term2.c:89:17: warning: implicit declaration of function 'memset' [-Wimplicit-function-declaration]
                     memset(newVisitedMap, 0, sizeof(newVisitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
                     ^
    term2.c:89:17: warning: incompatible implicit declaration of built-in function 'memset'
    term2.c:89:17: note: include '<string.h>' or provide a declaration of 'memset'
    term2.c:104:21: warning: 'return' with no value, in function returning non-void
                         return;
                         ^
    term2.c:113:13: warning: incompatible implicit declaration of built-in function 'memset'
                 memset(s, 0, sizeof(s));
                 ^
    term2.c:113:13: note: include '<string.h>' or provide a declaration of 'memset'
    term2.c:113:32: warning: argument to 'sizeof' in 'memset' call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
                 memset(s, 0, sizeof(s));
                                    ^
    term2.c: In function 'printWords':
    term2.c:123:1: warning: incompatible implicit declaration of built-in function 'memset'
     memset(s, 0, sizeof(s));
     ^
    term2.c:123:1: note: include '<string.h>' or provide a declaration of 'memset'
    term2.c:123:20: warning: argument to 'sizeof' in 'memset' call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
     memset(s, 0, sizeof(s));
                        ^
    term2.c:136:28: warning: argument to 'sizeof' in 'memset' call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
             memset(s, 0, sizeof(s));
                                ^
    term2.c:142:2: warning: control reaches end of non-void function [-Wreturn-type]
      }
      ^
    term2.c:123:1: warning: 's' is used uninitialized in this function [-Wuninitialized]
     memset(s, 0, sizeof(s));
     ^
    Compilation finished successfully.
    first run
    Code:
    userx@slackwhere:~/bin
    $ ./term2
    The matrix is: 
    
    | C || A || R || T |
    | E || T || A || K |
    | E || S || M || E |
    | L || L || P || N |
    
    The word we found are: 
    
    Segmentation fault
    what compiler IDE are you using to compile your code and check it while compiling for errors and warnings like these? add string.h to get rid of some of these warnings. Take a closer look at your functions, some do not have a return when they should.
    Last edited by userxbw; 11-28-2017 at 09:12 AM.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    I'm using codeBlocks to compile, and it's not returning any error.
    I add the string.h and change the function as needed but still the same problem...

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    Updated code:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    
    #define MATRIX_SIZE 4
    
    
    
    
    bool isWord(char* s){
    
    
    		return (!strcmp(s,"CAT") ||
    			!strcmp(s,"CATS") ||
    			!strcmp(s,"TRAM") ||
    			!strcmp(s,"TRAMS") ||
    			!strcmp(s,"TAME") ||
    			!strcmp(s,"CAR") ||
    			!strcmp(s,"CARS") ||
    			!strcmp(s,"RAT") ||
    			!strcmp(s,"RATS") ||
    			!strcmp(s,"RAMP") ||
    			!strcmp(s,"ART") ||
    			!strcmp(s,"CART") ||
    			!strcmp(s,"STAMP") ||
    			!strcmp(s,"TAKEN") ||
    			!strcmp(s,"MEN") ||
    			!strcmp(s,"MAKE") ||
    			!strcmp(s,"TAKE") ||
    			!strcmp(s,"ATE") ||
    			!strcmp(s,"SELL") ||
    			!strcmp(s,"STEEL") ||
                !strcmp(s,"RACE") ||
    			!strcmp(s,"RAKE") );
    
    
    }
    
    
    void append(char* s, char c){
    int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
    
    
    }
    
    
    
    
    void f(char* s, int x, int y, int visitedMap[MATRIX_SIZE][MATRIX_SIZE], char A[MATRIX_SIZE][MATRIX_SIZE]){
    
    
    int i,j, maxRow, minRow, maxCol, minCol;
    
    
    
    
        if(isWord(s))
             printf("%s\n", s);
    
    
    ///ROW
        if(x-1>0)
            maxRow=x-1;
        else
            maxRow=0;
    
    
        if(x+2<MATRIX_SIZE)
            minRow=x+2;
        else
            minRow=MATRIX_SIZE;
    
    
         ///COL
         if(y-1>0)
            maxCol=y-1;
        else
            maxCol=0;
    
    
        if(y+2<MATRIX_SIZE)
            minCol=y+2;
        else
            minCol=MATRIX_SIZE;
    
    
    
    
     for (i = maxRow; i < minRow; i++) {
            for (j = maxCol; j <minCol; j++) {
                if (visitedMap[i][j] == 1)
                    continue;
                else{
                    int newVisitedMap[MATRIX_SIZE][MATRIX_SIZE];
                    memset(newVisitedMap, 0, sizeof(newVisitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
                    int p,q;
                    for (p = 0; p < MATRIX_SIZE; p++) {
                        for (q = 0; q < MATRIX_SIZE; q++) {
                           newVisitedMap[p][q] = visitedMap[p][q];
                        }
                    }
                     newVisitedMap[i][j] = 1;
    
    
                     append(s, A[i][j]);
    
    
                     if(isWord(s)){
                        printf("%s\n", s);
                        return;
                     }
    
    
    
    
    
    
    
    
                    f(s,i,j,newVisitedMap, A);
    
    
                }///END - ELSE
    
    
                memset(s, 0, sizeof(s));
            }///END - FOR(2)
     }///END - FOR(1)
    
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    void printWords(char A[MATRIX_SIZE][MATRIX_SIZE]){
    
    
    char* s;
    int i,j;
    memset(s, 0, sizeof(s));
    
    
      int visitedMap[MATRIX_SIZE][MATRIX_SIZE];
      ///reset the array
      memset(visitedMap, 0, sizeof(visitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
    
    
    
    
     for(i=0;i<MATRIX_SIZE;i++){
        for(j=0;j<MATRIX_SIZE;j++){
    
    
            append(s, A[i][j]);
            visitedMap[i][j] = 1;
    
    
            f(s,i,j,visitedMap,A);
            memset(s, 0, sizeof(s));
             memset(visitedMap, 0, sizeof(visitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
        }
     }
    
    
     }
    
    
     void printMatrix(char A[MATRIX_SIZE][MATRIX_SIZE]){
    int i,j;
    printf("The matrix is: \n\n");
    for(i=0;i<MATRIX_SIZE;i++){
        for(j=0;j<MATRIX_SIZE;j++){
            printf("| %c |", A[i][j]);
        }
        printf("\n");
    }
    printf("\nThe word we found are: \n\n");
    }
    
    
    int main()
    {
    char A[MATRIX_SIZE][MATRIX_SIZE]={{'C', 'A', 'R', 'T'}, {'E', 'T', 'A', 'K'}, {'E', 'S', 'M', 'E'}, {'L', 'L', 'P', 'N'}};
    
    
    printMatrix(A);
    printWords(A);
    }
    When I try to run it on VS it's not working at all, but when i run it on code block its just keep printing....

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by zzxx5556 View Post
    I'm using codeBlocks to compile, and it's not returning any error.
    I add the string.h and change the function as needed but still the same problem...
    have you set it up to check because it didn't even catch the missing string.h for stnlen and missing header for memset, and functions without return values? or are you just not paying attention to the warnings?
    and you have no return value in main.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Like Smith said,
    and using printf to trace back where it is seg faulting at.
    Code:
    void printWords(char A[MATRIX_SIZE][MATRIX_SIZE]){
    
    char *s = '\0'; 
    int i,j;
         printf("here 1\n");
         // it fails here because it is just a pointer
         // so I comment it out.
    //memset(s, 0, sizeof(s)*strlen(s));
    
     
    
        int visitedMap[MATRIX_SIZE][MATRIX_SIZE];
        
        ///reset the array
        memset(visitedMap, 0, sizeof(visitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
    
        for(i=0;i<MATRIX_SIZE;i++){
            for(j=0;j<MATRIX_SIZE;j++){
            //now it fails here because
            // you're not making your char *s
            // usable to append anything to it.
            // by giving it a size,
            //or where do you even put anything into it?
            //its just a pointer without 
            //substance. 
          //
             printf("here 2\n");
                append(s, A[i][j]);
                printf("here 3\n");
                visitedMap[i][j] = 1;
                f(s,i,j,visitedMap,A);
                memset(s, 0, sizeof(s)*strlen(s));
             
                memset(visitedMap, 0, sizeof(visitedMap[0][0]) * MATRIX_SIZE * MATRIX_SIZE);
             
            }
        }
     }
    Code:
    $ ./term2
    The matrix is: 
    
    | C || A || R || T |
    | E || T || A || K |
    | E || S || M || E |
    | L || L || P || N |
    
    The word we found are: 
    
    here 1
    here 2
    Segmentation fault
    change it to this then see what you got going on with it
    Code:
    void printWords(char A[MATRIX_SIZE][MATRIX_SIZE]){
    
    //char *s = '\0'; 
    char s[10];
    Last edited by userxbw; 11-28-2017 at 10:28 AM.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    I change as u said, and I doing the same thing hahah
    I don't get any error while compile and I get:
    RAT
    RAT
    RAT
    RAT
    RAT
    ART
    RAT
    ART
    RAT
    RAT
    RAT
    ART
    RAT
    RAT
    RAT
    ART

    And its keep going...

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by zzxx5556 View Post
    I change as u said, and I doing the same thing hahah
    I don't get any error while compile and I get:
    RAT
    RAT
    RAT
    RAT
    RAT
    ART
    RAT
    ART
    RAT
    RAT
    RAT
    ART
    RAT
    RAT
    RAT
    ART

    And its keep going...
    Quote Originally Posted by zzxx5556 View Post
    can u explain this pls? I'm kinda new to C, [I] Know java but don't really get what u said..
    yep, so why do loops not stop? it is the same in Java
    but it does stop eventually
    looks like its repeating itself look at the sequence
    Code:
    RACE
    userx@slackwhere:~/bin
    Last edited by userxbw; 11-28-2017 at 11:07 AM.

  11. #11
    Registered User
    Join Date
    Nov 2017
    Posts
    10
    I wrote it in java also and it's working hahaha
    for some reason in c I dont get it
    I was trying to stop the loop with break but still nothing..
    Last edited by zzxx5556; 11-28-2017 at 11:15 AM.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by zzxx5556 View Post
    I wrote it in java also and it's working hahaha
    for some reason in c I dont get it
    I was trying to stop the loop with break but still nothing..
    so that loop is the same in Java no sense in posting it, you're going to have to examine it closer to see why it is doing that. obviously. You can do it!!!

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code::Blocks is a IDE; it is *not* an compiler!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    I only refactored is_word function :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    #define MAX_WORD_LEN 20
    
    
    bool is_word (const char * const s)
    {
        const char * const words[]={"CAT","CATS","TRAM",""}; // And so on...
        
        bool iw=false;
        
        unsigned ind=0;
        
        for(ind=0; strcmp((words[ind]),"")!=0; ind++) 
        {
            //printf("`` %*s\n",MAX_WORD_LEN-1 ,words[ind]);
            if(strcmp(s,words[ind])==0)
            {
                iw=true;
                break;
            }
        }
        
        return iw;
    }

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    The sentinal marking the end of the array of strings would more normally be assigned a NULL pointer (it makes checking for the termination of the loop a bit nicer)

    Code:
    bool is_word (const char * const s)
    {
        const char * const words[]={"CAT","CATS","TRAM",NULL};  /******** UPDATED THIS LINE */     
        bool iw=false;
        
        unsigned ind=0;
        
        for(ind=0; words[ind]!=NULL; ind++)  /**************** UPDATED THIS LINE */ 
        {
            //printf("`` %*s\n",MAX_WORD_LEN-1 ,words[ind]);
            if(strcmp(s,words[ind])==0)
            {
                iw=true;
                break;
            }
        }
        
        return iw;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out in hex value
    By kiros88 in forum C Programming
    Replies: 6
    Last Post: 08-27-2009, 12:02 PM
  2. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  3. printing ***
    By sunnypalsingh in forum C Programming
    Replies: 5
    Last Post: 10-12-2005, 12:46 PM
  4. Printing
    By fred_scotland in forum C Programming
    Replies: 3
    Last Post: 02-21-2002, 04:32 PM
  5. Printing
    By maneeshpnair in forum C Programming
    Replies: 1
    Last Post: 02-13-2002, 04:53 AM

Tags for this Thread