473,581 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

17 New Member
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 4327
JosAH
11,448 Recognized Expert MVP
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 New Member
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 New Member
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 Recognized Expert MVP
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 Recognized Expert MVP
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 New Member
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

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

Similar topics

0
2227
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 the same to me, but it isn't working, so i guess they aren't... $proxy = fsockopen("tcp://someaddresshere", 80, $errno, $errstr); $temp =...
0
2242
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 value="1">Main</option> <option value="2">Main > Computers</option> <option value="4">Main > Computers > Hardware </option>
2
1784
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 a website (ANY website) that requires these data - it tries to supply the relevant data to the most appropriate text field. How does it decide...
3
3376
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); da.Fill(DataSet, "Path");
0
997
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 Then Me.lblName = "Hello World"
18
5117
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 observations I've recently made. The core of the problem is that Postgres is filling up my hard drive with swap files at the rate of around 3 to 7 GB per...
2
1897
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 threading (or anything else) that I can use so that I can display some message (i.e. "Grid loading...") or any way so that I can start viewing the...
1
5121
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 can go to change the timeout setting for this. when i go to my project, then my settings tab i set the connection timeout for 120, but it doesn't...
3
1937
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() ................ Selection.End(xlUp).Select ActiveCell.Offset(0, 1).Select ActiveCell = "BSC" Cd = 4 ...
3
2006
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 VB.NET (C# will come later)? This must be an online or book adventure because I can't totally disappear to attend physical classes. I have 15 years...
0
7882
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6564
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.