By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

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

P: 17
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...

Expand|Select|Wrap|Line Numbers
  1. #include "simpio.h"
  2. #include <stdio.h>
  3. #include "genlib.h"
  4.  
  5. #define size 20;
  6.  
  7. void GetArray(char arr[][20]);
  8. void fill(char arr[][20], int row, int col);
  9. void disp(char arr[][20]);
  10.  
  11. main()
  12. {
  13.     char arr[20][20];
  14.     int row, col;
  15.  
  16.     GetArray(arr);
  17.     printf("\nEnter row of interior point: ");
  18.     row = GetInteger();
  19.     printf("Enter column of interior point: ");
  20.     col = GetInteger();
  21.     fill(arr, row, col);
  22.     disp(arr);
  23.     getchar();
  24.     system("pause");
  25. }
  26.  
  27. void GetArray(char arr[][20])
  28. {
  29.     char input;
  30.     int i,j;
  31.     for(i=0;i<20;i++)
  32.     {
  33.      for(j=0;j<20;j++)
  34.       {
  35.        arr[i][j] = ' ';
  36.       }
  37.     }
  38. printf("Enter the closed shape using asterisks and spaces. Keep the number of rows and columns under 20\n");
  39. printf("To signal the end of the input, type '!'. Use the 'enter' key to move down rows\n\n");
  40. i = 0;
  41. j = 0;
  42. while(TRUE)
  43.  {
  44.  input = getchar();
  45.  if(input == 'r')
  46.       i++;
  47.  else if(input == '!')
  48.       break;
  49.  else
  50.   {
  51.      arr[i][j] = input;
  52.      j++;
  53.   }
  54.  }
  55.     i=0;
  56.     j=0;
  57.     printf("\n\nThe input shape is:\n");
  58.     for(i=0;i<20;i++)
  59.     {
  60.      for(j=0;j<20;j++)
  61.      {
  62.       printf("%c",arr[i][j]);
  63.      }
  64.     }
  65. }
  66.  
  67. void fill(char arr[][20], int row, int col)
  68. {
  69.     if(arr[row][col]!=' '|| row>=20 || col>=20 || row<0 || col<0)
  70.       {}
  71.     else
  72.     {
  73.     arr[row][col] ='*';
  74.     fill(arr,row+1,col);
  75.     fill(arr,row-1,col);
  76.     fill(arr,row,col+1);
  77.     fill(arr,row,col-1);
  78.     }
  79. }
  80.  
  81. void disp(char arr[][20])
  82. {
  83.     int i,j;
  84.  
  85.     printf("\nThe filled shape is:\n");
  86.     for(i=0;i<20;i++)
  87.     {
  88.      for(j=0;j<20;j++)
  89.      {
  90.       printf("%c",arr[i][j]);
  91.      }
  92.      printf("\n");
  93.     }
  94. }
Aug 31 '08 #1
Share this Question
Share on Google+
12 Replies


Expert 10K+
P: 11,448
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:

Expand|Select|Wrap|Line Numbers
  1. if(row>=20 || col>=20 || row<0 || col<0 || arr[row][col]!=' ')
  2.  
kind regards,

Jos
Aug 31 '08 #2

P: 17
Better turn that test around if you don't want to 'look' beyond the bounds of your array:

Expand|Select|Wrap|Line Numbers
  1. if(row>=20 || col>=20 || row<0 || col<0 || arr[row][col]!=' ')
  2.  
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.
Aug 31 '08 #3

P: 17
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.
Aug 31 '08 #4

Expert 10K+
P: 11,448
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
Aug 31 '08 #5

Expert 10K+
P: 11,448
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
Aug 31 '08 #6

P: 17
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
Aug 31 '08 #7

Expert 10K+
P: 11,448
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
Aug 31 '08 #8

Expert 10K+
P: 11,448
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
Aug 31 '08 #9

P: 17
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.
Aug 31 '08 #10

Expert 10K+
P: 11,448
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
Aug 31 '08 #11

P: 17
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
Sep 1 '08 #12

Expert 10K+
P: 11,448
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
Sep 1 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.