recursive fill function

Pages: 123
Hi I'm trying to write a program that gets as an input:
* A perimeter (boundary) of a "closed" shape, such as an ellipse, circle, etc,.
* Coordinates of an interior point.
The program should fill in a recursive way the area (enclosed by) this shape from the given interior point without exceeding the boundary of this shape. What I need is someone one how to start. Like how would get the input for these? and what kind of prototype should i use?
I was also given this information:

Instructions
The input will include:
• m lines, each including n characters. If the character read is not a space, the character belongs
to the boundary of the shape.
• Two numbers that specify the coordinates, namely the row and column indices of the point
from which the program is supposed to start filling the shape.
You can assume that this input point is inside the shape and that the shape is really closed. The
interior point is such that to any direction you “go” from this point, you will eventually reach the
boundary.
Note that the perimeter is represented as a sequence of characters (asterisks).
The program should display both the shapes before filling the shape and after filling the shape.

Hints
1.The recursive procedure, fill, should be very short..
The procedure, fill, takes three arguments: Row and column of the current point checked and the
multidimensional array that represents the shape.
2. Think of a simple end condition, and of the neighbouring cells.
Thanks helios but I need to change the characters inside the shape not change the color. Is there a link that gives an example of that?
It's the same.
Oh okay.
Can someone help me I'm getting a lot of errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include "simpio.h"

void getshape(int row, int column, char shape);
void fill(char shape, int row, int column);
int main()
{
    int row, column;
    int r, c;
    char shape[row][column];
    getshape(shape, row, column);
    fill(shape, row, column);
    getchar();
    return 0;
}
void getshape(int row, int column, char shape)
{
     printf("Enter the number of rows\n");
     row=GetInteger();
     printf("Enter the number of columns\n");
     column=GetInteger();
     
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape=getchar();
     }
}
void fill(char shape, int row, int column)
{
     int r, c;
     printf("Enter the interior point(where the program will start filling)\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
     if (r < 0 || r > row || c > column || c < 0) return;
     if (shape[r][c] == '*' || shape[r][c] == '#') return;
     fill[r][c] = '*';
	
	 fill(shape,r+1,c);//down
	 fill(shape,r-1,c);//up
	 fill(shape,r,c-1);//left
	 fill(shape,r,c+1);//right
}       
please help.
what kind of errors?
These are the errors I am getting now:
`void fill(char (*)[200], int, int)':
pointer to a function used in arithmetic
assignment of read-only location
cannot convert `char' to `void ()(char (*)[200], int, int)' in assignment
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include "simpio.h"

const int MAX_ROW = 200;
const int MAX_COL = 200;

void getshape(char shape[MAX_ROW][MAX_COL], int row, int column);
void fill(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
    int r, c;
    char shape[MAX_ROW][MAX_COL];
    int row=0, column=0;
    printf("Enter row and column :  ");
    row=GetInteger();
    column=GetInteger();
    getshape(shape, row, column);
    fill(shape, row, column);
    getchar();
    return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape[row][column]=getchar();
     }
}
void fill(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     int r, c;
     printf("Enter the interior point(where the program will start filling)\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
     if (r < 0 || r > row || c > column || c < 0) return;
     if (shape[r][c] == '*' || shape[r][c] == '#') return;
     fill[r][c] = '*';
	
	 fill(shape,r+1,c);//down
	 fill(shape,r-1,c);//up
	 fill(shape,r,c-1);//left
	 fill(shape,r,c+1);//right
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    char shape[MAX_ROW][MAX_COL];//you define it in main

    void fill(char shape[MAX_ROW][MAX_COL], int row, int column)
                //make another name for shape[MAX_ROW][MAX_COL] and for int row and column
                //like void fill(char passShape[MAX_ROW][MAX_COL], int prow, int pcolumn)
                //the variables are confusing and also....

    void fill(...){

	 fill(shape,r+1,c);//down
	 fill(shape,r-1,c);//up
	 fill(shape,r,c-1);//left
	 fill(shape,r,c+1);//right
         //you call the function inside its function?
         //hmmm... my suggestion is to separate the text output...
    }
         


Edit: oops so the recursive fill you are saying is this one?

1
2
3
4
5
6
7
    void fill(...){
         ...
	 fill(shape,r+1,c);//down
	 fill(shape,r-1,c);//up
	 fill(shape,r,c-1);//left
	 fill(shape,r,c+1);//right
    }
Last edited on
The program is supposed to be recursive. I changed the name of fill since its a function in the algorithm library but I'm getting the same errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include "simpio.h"

const int MAX_ROW = 200;
const int MAX_COL = 200;

void getshape(char shape[MAX_ROW][MAX_COL], int row, int column);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
    int r, c;
    char shape[MAX_ROW][MAX_COL];
    int row=0, column=0;
    printf("Enter row and column :  ");
    row=GetInteger();
    column=GetInteger();
    getshape(shape, row, column);
    fillArray(shape, row, column);
    getchar();
    return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape[row][column]=getchar();
     }
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     int r, c;
     printf("Enter the interior point(where the program will start filling)\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
     if (r < 0 || r > row || c > column || c < 0) return;
     if (shape[r][c] == '*' || shape[r][c] == '#') return;
     fillArray[r][c] = '*';
	
	 fillArray(shape,r+1,c);//down
	 fillArray(shape,r-1,c);//up
	 fillArray(shape,r,c-1);//left
	 fillArray(shape,r,c+1);//right
}       
not that...
what am I saying are these variables

 
char shape[MAX_ROW][MAX_COL], int row, int column


for example:
1
2
3
void getshape(char p_shape[MAX_ROW][MAX_COL], int prow, int pcolumn)

void fillArray(char f_shape[MAX_ROW][MAX_COL], int frow, int fcolumn)


What do you mean?
I "guess" the errors occur in your variable declaration, you redeclared again the variable in the functions.


These are the errors I am getting now:
`void fill(char (*)[200], int, int)':
pointer to a function used in arithmetic
assignment of read-only location //<---
cannot convert `char' to `void ()(char (*)[200], int, int)' in assignment

<-- since you declare the variables in main function
1
2
    char shape[MAX_ROW][MAX_COL];
    int row=0, column=0;


then redeclare it in the function

1
2
3
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)

void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)


EDIT: try using pointers to make your code more efficient

Last edited on
Actually I fixed the program so the errors are gone. However, The program repeats this part of the program:

1
2
3
4
5
printf("Enter the interior point where the program will start filling\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
Here is my full code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include "simpio.h"

const int MAX_ROW = 200;
const int MAX_COL = 200;

void getshape(char shape[MAX_ROW][MAX_COL], int row, int column);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
    int r, c;
    char shape[MAX_ROW][MAX_COL];
    int row=0, column=0;
    printf("Enter row and column :  ");
    row=GetInteger();
    column=GetInteger();
    getshape(shape, row, column);
    shape[row][column] = '*';
    fillArray(shape, row, column);
    getchar();
    return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape[row][column]=getchar();
     }
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     int r, c;
     printf("Enter the interior point where the program will start filling\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
     if (r < 0 || r > row || c > column || c < 0) return;
     if (shape[r][c] == '*' || shape[r][c] == '#') return;
     shape[r][c] = '*';
	
	 fillArray(shape,r+1,c);//down
	 fillArray(shape,r-1,c);//up
	 fillArray(shape,r,c-1);//left
	 fillArray(shape,r,c+1);//right
} 
Of course!

why??

because of these codes...

1
2
3
4
	 fillArray(shape,r+1,c);//down
	 fillArray(shape,r-1,c);//up
	 fillArray(shape,r,c-1);//left
	 fillArray(shape,r,c+1);//right 


my suggestion is to put the text part in another function before calling this one in main
1
2
3
    //put here printFillArrayText() <--something like that.
    //now you should pass the two other variables r and c.
    fillArray(shape, row, column);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

void text()
{
    printf("Enter the interior point where the program will start filling\n");
}

int getRow()
{
     printf("Enter the row number\n");
     r=GetInteger();
     return r;
}

int getColumn()
{
     printf("Enter the column number\n");
     c=GetInteger();
     return c;
}

int main()
{
     int rger, cget;
     ...
     ...
     text();
     rget = getRow();
     cget = getColumn();
     fillArray(shape,row,column,rget,cget); 
     ...
}


Hope this helps.
Last edited on
I made an attempt to do as you told me but the same thing happens.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include "simpio.h"

const int MAX_ROW = 200;
const int MAX_COL = 200;

void getshape(char shape[MAX_ROW][MAX_COL], int row, int column);
int interior();
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
    int r, c;
    char shape[MAX_ROW][MAX_COL];
    int row=0, column=0;
    printf("Enter row and column :  ");
    row=GetInteger();
    column=GetInteger();
    getshape(shape, row, column);
    shape[row][column] = '*';
    interior();
    fillArray(shape, row, column);
    getchar();
    return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape[row][column]=getchar();
     }
}
int interior()
{
    int r, c;
     printf("Enter the interior point where the program will start filling\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
     return r;
     return c;
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
     int r=interior();
     int c=interior();
     if (r < 0 || r > row || c > column || c < 0) return;
     if (shape[r][c] == '*' || shape[r][c] == '#') return;
     shape[r][c] = '*';
	
	 fillArray(shape,r+1,c);//down
	 fillArray(shape,r-1,c);//up
	 fillArray(shape,r,c-1);//left
	 fillArray(shape,r,c+1);//right
}
1
2
3
4
5
6
7
8
9
10
11
int interior()
{
    int r, c;
     printf("Enter the interior point where the program will start filling\n");
     printf("Enter the row number\n");
     r=GetInteger();
     printf("Enter the column number\n");
     c=GetInteger();
     return r;
     return c;//<-- you can't return two values.
}


EDIT: could you post your simpio.h file so that I can compile the code you've posted
Last edited on
Pages: 123