![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0
![]() |
Filling in a shape
I have been having trouble with this program. It is supposed to get an input of stars as a "boundary" of a picture. It also receives a point inside of the shape that will be used as a starting point to fill in the shape. It then fills it in and displays it.
This is what I have so far: /* File name: perimeter.c
* This program gets an inputed perimeter and fills it in using recursion */
#include <stdio.h>
#include "simpio.h"
void fill(char perimeter[][], int row, int column);
void getpic(char perimeter[][], int lines, int character);
void display(char perimeter[][]);
int main()
{
int lines, character;
printf("Lines: ");
lines = GetInteger();
printf("Characters per line: ");
character = GetInteger();
char perimeter[lines+1][character+1];
getpic(perimeter, lines, character);
display(perimeter);
getchar();
}
void getpic(char perimeter[][], int lines, int character)
{
char input;
int countrow, countcolumn, row, column;
printf("\n\n");
for(countrow = 0; countrow <= lines; countrow++)
{
for(countcolumn = 0; countcolumn <= character; countcolumn++)
{
perimeter[countrow][countcolumn] = getchar();
}
}
printf("\nSelect a point to fill from:\n");
printf("Row # ");
row = GetInteger();
printf("Column # ");
column = GetInteger();
fill(perimeter, row, column);
}I'm not sure how to start a recursive function to fill in the shape. Any ideas? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
Your fill function could look at the current position, if it is empty, fill it and then call itself for the positions 1 to the left, 1 to the right, 1 up, and 1 down. If the position you are looking at is not empty (or out of bounds), do nothing.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0
![]() |
My new program (not finished though):
/* File name: perimeter.c
* This program gets an inputed perimeter and fills it in using recursion */
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
void fill(char perimeter[][5], int row, int column);
void getpic(char perimeter[][5]);
void display(char perimeter[][5]);
int main()
{
char perimeter[5][5];
printf("Please input a picture:\n");
getpic(perimeter);
display(perimeter);
getchar();
}
void getpic(char perimeter[][5])
{
int countrow, countcolumn, row, column;
printf("\n\n");
for(countrow = 0; countrow <= 4; countrow++)
{
for(countcolumn = 0; countcolumn <= 4; countcolumn++)
{
perimeter[countrow][countcolumn] = getchar();
}
}
printf("\nSelect a point to fill from:\n");
printf("Row # ");
row = GetInteger();
printf("Column # ");
column = GetInteger();
fill(perimeter, row, column);
}
void fill(char perimeter[][5], int row, int column)
{
if(perimeter[row][column] == ' ')
{
perimeter[row][column] = '*';
}
else {
return;
}
fill(perimeter, row-1, column);
fill(perimeter, row+1, column);
fill(perimeter, row, column-1);
fill(perimeter, row, column+1);
}
void display(char perimeter[][5])
{
int countrow, countcolumn;
printf("\n\n");
for(countrow = 0; countrow <= 4; countrow++)
{
for(countcolumn = 0; countcolumn <= 4; countcolumn++)
{
putchar(perimeter[countrow][countcolumn]);
}
}
printf("\n");
}Unfortunately, its just not filling in the entire figure. Each time I choose a different point, different stars are filled in. What am I doing wrong? P.S. Here, I manually specify the size of the figure so the figure must be that size. I want to ask the user for the size of their figure instead, but I'm not sure how to do that since it needs to be specified in the figure prototype. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
Seems to work for me - what inputs are you using? Are you sure your program (or you) isn't getting confused by the input needing to be on one line, except for the column number? You should probably take each line of the picture with a carriage return in between, that way the input could actually look like a picture, rather than a stream of *'s.
I used: ****** ** **** * ******1 1 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help filling array randomly, in a way | ReggaetonKing | Java | 4 | Oct 5th, 2006 12:29 PM |
| Python area calculator | Volkan | Python | 20 | Feb 4th, 2006 10:11 AM |
| Gettin in shape | jayme | Coder's Corner Lounge | 15 | Jan 3rd, 2006 9:28 PM |
| Fast Concave Polygon Filling Algorithm is wanted | riekey | Java | 2 | Dec 15th, 2005 9:50 AM |
| Filling a TextBox | Samsonite | C# | 2 | Apr 17th, 2005 4:39 AM |