Need help filling a user-defined shape with asterisks using recursion in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ab12
    New Member
    • Aug 2008
    • 17

    Need help filling a user-defined shape with asterisks using recursion in C

    I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape from which to start filling. I need to use recursion here. for example, to be clear:

    input: (ignore the line, think of that as blank space)
    *****
    *___*
    *****
    coordinates are (2,1) /*its an array so numbering will start from 0*/

    output
    *****
    *****
    *****

    my program compiles but for some reason everything in the shape and past its boundaries get filled with asterisks. it gets filled to the maximum size i've set the array to, even if the shape isn't that big. its like it completely ignores when it gets to a boundary of asterisks when it's filling.

    here is the program i've written, its rather long...

    Code:
    #include "simpio.h"
    #include <stdio.h>
    #include "genlib.h"
    
    #define size 20;
    
    void GetArray(char arr[][20]);
    void fill(char arr[][20], int row, int col);
    void disp(char arr[][20]);
    
    main()
    {
        char arr[20][20];
        int row, col;
    
        GetArray(arr);
        printf("\nEnter row of interior point: ");
        row = GetInteger();
        printf("Enter column of interior point: ");
        col = GetInteger();
        fill(arr, row, col);
        disp(arr);
        getchar();
        system("pause");
    }
    
    void GetArray(char arr[][20])
    {
        char input;
        int i,j;
        for(i=0;i<20;i++)
        {
         for(j=0;j<20;j++)
          {
           arr[i][j] = ' ';
          }
        }
    printf("Enter the closed shape using asterisks and spaces. Keep the number of rows and columns under 20\n");
    printf("To signal the end of the input, type '!'. Use the 'enter' key to move down rows\n\n");
    i = 0;
    j = 0;
    while(TRUE)
     {
     input = getchar();
     if(input == 'r')
          i++;
     else if(input == '!')
          break;
     else
      {
         arr[i][j] = input;
         j++;
      }
     }
        i=0;
    	j=0;
    	printf("\n\nThe input shape is:\n");
        for(i=0;i<20;i++)
        {
         for(j=0;j<20;j++)
         {
          printf("%c",arr[i][j]);
         }
        }
    }
    
    void fill(char arr[][20], int row, int col)
    {
        if(arr[row][col]!=' '|| row>=20 || col>=20 || row<0 || col<0)
          {}
        else
        {
        arr[row][col] ='*';
        fill(arr,row+1,col);
        fill(arr,row-1,col);
    	fill(arr,row,col+1);
        fill(arr,row,col-1);
        }
    }
    
    void disp(char arr[][20])
    {
        int i,j;
    
        printf("\nThe filled shape is:\n");
        for(i=0;i<20;i++)
        {
    	 for(j=0;j<20;j++)
         {
          printf("%c",arr[i][j]);
         }
    	 printf("\n");
        }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by ab12
    if(arr[row][col]!=' '|| row>=20 || col>=20 || row<0 || col<0)
    Better turn that test around if you don't want to 'look' beyond the bounds of your array:

    [code=c]
    if(row>=20 || col>=20 || row<0 || col<0 || arr[row][col]!=' ')
    [/code]

    kind regards,

    Jos

    Comment

    • ab12
      New Member
      • Aug 2008
      • 17

      #3
      Originally posted by JosAH
      Better turn that test around if you don't want to 'look' beyond the bounds of your array:

      [code=c]
      if(row>=20 || col>=20 || row<0 || col<0 || arr[row][col]!=' ')
      [/code]

      kind regards,

      Jos
      its still not working. do i have too many recusive calls? should i have something in my if statement of the fill function? i'm pretty sure the problem lies in the fill function itself. i really need help on this guys, i've really tried hard to do this myself.

      Comment

      • ab12
        New Member
        • Aug 2008
        • 17

        #4
        Originally posted by ab12
        its still not working. do i have too many recusive calls? should i have something in my if statement of the fill function? i'm pretty sure the problem lies in the fill function itself. i really need help on this guys, i've really tried hard to do this myself.
        basically now i get as the output a few lines of different lengths of asterisks and then after that a filled 20X20 box of asterisks.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by ab12
          basically now i get as the output a few lines of different lengths of asterisks and then after that a filled 20X20 box of asterisks.
          Also look at your line #45: 'r' does not represent the enter key; try '\r' instead.
          (it might be followed by another character '\n' and you don't anticipate for that)

          kind regards,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            ps. after you've fixed that little bug it still won't work because at line #46 you just
            increment the row counter i without resetting the column counter j. Your code
            prints out what it has read you should've seen something wrong there ...

            kind regards,

            Jos

            Comment

            • ab12
              New Member
              • Aug 2008
              • 17

              #7
              Originally posted by JosAH
              ps. after you've fixed that little bug it still won't work because at line #46 you just
              increment the row counter i without resetting the column counter j. Your code
              prints out what it has read you should've seen something wrong there ...

              kind regards,

              Jos
              that didn't really make a difference as anyway when the program display's the input shape, it displays it fine. i'm pretty sure the problem is in the fill function

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by ab12
                that didn't really make a difference as anyway when the program display's the input shape, it displays it fine. i'm pretty sure the problem is in the fill function
                Sure, fine with me; display what is in those arrays using decimal values (%d)
                and you'll see what had happened. What you see is not what you expected;
                your floodfill function is fine by itself; it's your input function that goofs.

                kind regard,

                Jos

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  ps. here's a question for you: how come your shape prints out 'fine' while all you're
                  supposed to be printing are spaces and stars? There must be other characters
                  stored in your array.

                  kind regards,

                  Jos

                  Comment

                  • ab12
                    New Member
                    • Aug 2008
                    • 17

                    #10
                    Originally posted by JosAH
                    ps. here's a question for you: how come your shape prints out 'fine' while all you're
                    supposed to be printing are spaces and stars? There must be other characters
                    stored in your array.

                    kind regards,

                    Jos
                    uh..by fine i didn't mean the word fine. I meant it displayed the input shape correctly. i do only have spaces and stars in my array. so you're saying the problem lies in how i take in the shape of stars and not the recursive fill function? the thing is that i am able to display the input correctly, so i think that the array is functioning properly when it goes into the fill function. the fill function seems to be what is screwing things up. it is filling up everything past the boundaries, though it depends on the shape i create.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by ab12
                      uh..by fine i didn't mean the word fine. I meant it displayed the input shape correctly. i do only have spaces and stars in my array. so you're saying the problem lies in how i take in the shape of stars and not the recursive fill function? the thing is that i am able to display the input correctly, so i think that the array is functioning properly when it goes into the fill function. the fill function seems to be what is screwing things up. it is filling up everything past the boundaries, though it depends on the shape i create.
                      Just look at your own code: all you do is displaying (%c) the chars in the arrays.
                      Your code doesn't explicit dislay new lines (printf("\n")); how come your shape
                      ends up being displayed 'fine' then? There must be new line characters somewhere
                      in your arrays. Your shape is not stored correctly. Reread my other replies again.

                      kind regards,

                      Jos

                      Comment

                      • ab12
                        New Member
                        • Aug 2008
                        • 17

                        #12
                        Originally posted by JosAH
                        Just look at your own code: all you do is displaying (%c) the chars in the arrays.
                        Your code doesn't explicit dislay new lines (printf("\n")); how come your shape
                        ends up being displayed 'fine' then? There must be new line characters somewhere
                        in your arrays. Your shape is not stored correctly. Reread my other replies again.

                        kind regards,

                        Jos
                        ok i fixed it thanks. i changed the 'r' to '\n' and did some other small things

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by ab12
                          ok i fixed it thanks. i changed the 'r' to '\n' and did some other small things
                          Good; I bet that flood-fill function ran unaltered ;-)

                          kind regards,

                          Jos

                          Comment

                          Working...