Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 25th, 2007, 5:19 PM   #1
gamerfelipe
Newbie
 
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0 gamerfelipe is on a distinguished road
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?
gamerfelipe is offline   Reply With Quote
Old Jun 25th, 2007, 9:55 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Jun 26th, 2007, 2:28 PM   #3
gamerfelipe
Newbie
 
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0 gamerfelipe is on a distinguished road
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.
gamerfelipe is offline   Reply With Quote
Old Jun 26th, 2007, 8:05 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
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
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help filling array randomly, in a way ReggaetonKing Java 4 Oct 5th, 2006 11:29 AM
Python area calculator Volkan Python 20 Feb 4th, 2006 9:11 AM
Gettin in shape jayme Coder's Corner Lounge 15 Jan 3rd, 2006 8:28 PM
Fast Concave Polygon Filling Algorithm is wanted riekey Java 2 Dec 15th, 2005 8:50 AM
Filling a TextBox Samsonite C# 2 Apr 17th, 2005 3:39 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:56 PM.

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