Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Filling in a shape (http://www.programmingforums.org/showthread.php?t=13424)

gamerfelipe Jun 25th, 2007 6:19 PM

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?

The Dark Jun 25th, 2007 10:55 PM

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.

gamerfelipe Jun 26th, 2007 3:28 PM

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.

The Dark Jun 26th, 2007 9:05 PM

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

as my input


All times are GMT -5. The time now is 2:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC