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... - #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");
-
}
-
}
12 4289
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: -
if(row>=20 || col>=20 || row<0 || col<0 || arr[row][col]!=' ')
-
kind regards,
Jos
Better turn that test around if you don't want to 'look' beyond the bounds of your array: -
if(row>=20 || col>=20 || row<0 || col<0 || arr[row][col]!=' ')
-
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.
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.
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
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
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
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
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
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.
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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);
...
|
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...
|
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...
|
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...
|
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...
|
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()
...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |