473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
12 4314
JosAH
11,448 Expert 8TB
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
ab12
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
ab12
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
JosAH
11,448 Expert 8TB
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
JosAH
11,448 Expert 8TB
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
ab12
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
JosAH
11,448 Expert 8TB
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
JosAH
11,448 Expert 8TB
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
ab12
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
JosAH
11,448 Expert 8TB
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
ab12
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

0
by: zelnaga | last post by:
i'm connecting to the internet via a proxy, and am having problems filling out forms... below is the code i have, and below that is the http request i am trying to make it look like. they look...
0
by: Brian Murphy | last post by:
<?php /* I need your help. I'd be very thankfull if write me this script.I need a script that displays a list of categories and subcategories like this: <select name="category"> <option...
2
by: hennakapoor | last post by:
How do the form filling software determine which text field gets what data i.e. a typical form filling software will ask the user to enter the username / address / password etc. Whenever it goes to...
3
by: Tee | last post by:
Hi, I need some help about filling dataset, I am not sure how to describe it, so I give the example here. da = new OleDbDataAdapter("SELECT * FROM Path WHERE PathID=1", cnn); ...
0
by: Andreas Klemt | last post by:
Hello, I have an ASPX Page with 8 Web User Controls on it. Some are Visible = False and some are Visible = True Now I wrote in each Web User Control code: Sub Page_Load() If Me.Visible...
18
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
2
by: VMI | last post by:
I'm filling up a gridview and the underlying datatable has about 30,000 records, so it takes some time before I can see the page again with the records on the gridview. Is there any type of...
1
by: mike11d11 | last post by:
I'm doing a simple TableAdapter.Fill filling my datatable from a view within a SQL database. for some reason it is timing out after 30 seconds and I cant seem to find out where in vb.net 2005 I...
3
by: nkechifesie | last post by:
I have a VBA Program that has a sub embedded in a sub, i feel there is a better way to do this but cant find it. Can you please help me this is a part of the code Sub Daily_Alerts() ...
3
by: =?Utf-8?B?SmF5IFZpbnRvbg==?= | last post by:
I see general messages about how to learn .NET but I have an immediate requirement to ramp up my old skills very quickly. Can anyone recommend the FASTEST way for me to get almost-competent in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.