Thread: Whats wrong with simple recursion printing program?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Whats wrong with simple recursion printing program?

    Code:
     int find_path(char maze[][8],int x, int y);
        
     int main(int argc, const char * argv[]) {
       
          int x = 0;
          int y = 0;
          char maze[8][8];
        
        FILE *fp;
        fp = fopen ("maze3.txt","r");
        
        if(fp == NULL)
            printf("File unreadable.");
        
          for (int i=0; i<=7;i++) {
            printf("\n");
        
            for( int j = 0; j<=7;j++) {
                
            fscanf(fp," %c",&maze[i][j]);
            
            
            } // 8x8 array processed into maze
       
     } // process data file into array
            
          find_path(maze,x,y);
        
          return 0;
    
    
        } // end main
    
    
    
    
            int find_path(char maze[][8],int x, int y) {
        
       
            if(x>6)
            {
                
                exit(1);
            }//exit condition for recursion
        
        
         printf("%c",find_path(maze,x+1,y)); // never prints anything why?
    
    
    
    
             return 0;
    
    
           }
    I have checked to ensure that the data file was correctly read into maze.
    When i use recursion to print out the rows (x) in find_path it never prints out anything.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, why do you call exit(1) in find_path instead of returning a value? You're basically saying: when x is greater than 6, there is an error so dire that we must terminate the program immediately!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Hi laserlight,

    Without the exit statement, my function becomes an infinite recursion and seg faults.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    exit(1) means terminate the program immediately. What you want to do is to return from the function, without exiting the program.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tmac619619
    Without the exit statement, my function becomes an infinite recursion and seg faults.
    You should find out why, not just slap an exit call and expect it to work. Of course your printf call never prints anything: how can it print anything when you forcibly terminate the program before it has any chance of printing anything?

    This works for me:
    Code:
    #include <stdio.h>
    
    int find_path(char maze[][8], int x, int y)
    {
        if (x > 6)
        {
            return 1;
        }
    
        printf("%d ", find_path(maze, x + 1, y));
    
        return 0;
    }
    
    int main(void)
    {
        char maze[8][8];
        find_path(maze, 0, 0);
        putchar('\n');
        return 0;
    }
    I get the output:
    Code:
    1 0 0 0 0 0 0
    Notice that I indented my code properly. Your program is sorely in need of proper formatting. You absolutely need to indent your code properly. If you don't know how, ask. If you don't indent your code properly, then it will be unreadable. If your code is unreadable, people won't read it. If people won't read it, your posting it here is in vain.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    @laserlight thanks for the formatting tip.

    Code:
    int find_path(char maze[][8],int x, int y) {
        
        if(x>6)
            {
                return 1;
            }
        
        printf("%c",find_path(maze,x+1,y));   // <---- Is this the problem line? Print out a char, but find_path is of type int?
        
        
        return 0;
    } 
    
    int main(void) {
    
    
       char maze[8][8];
       find_path(maze, 0, 0);
    }
    It still won't print. The 8x8 array is an array of Char's.

    Is this the problem statement?
    printf("%c",find_path(maze,x+1,y));

    I messed around with the '%c' '%s', but it won't print out anything.
    Last edited by tmac619619; 09-02-2015 at 02:36 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tmac619619
    It still won't print. The 8x8 array is an array of Char's.

    Is this the problem statement?
    printf("%c",find_path(maze,x+1,y));

    I messed around with the '%c' '%s', but it won't print out anything.
    find_path returns an int, so you should print its return value with %d.

    Yes, you have an 8 by 8 array of char, but you are not printing the elements from that array, so it does not make sense to use %c.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statement. simple to solve but i dunno whats wrong.
    By ingeniousreader in forum C Programming
    Replies: 2
    Last Post: 05-17-2012, 09:43 AM
  2. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 12:18 PM
  3. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 01:46 PM
  4. Whats Wrong in My simple program?
    By MartinLiao in forum C Programming
    Replies: 1
    Last Post: 09-02-2004, 02:30 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM