Hi I'm supposed to write a program that has the input 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.
I started doing it but when i got to the fill function I can't think of anything to put in it. Here is what i have so far.

#include <stdio.h>
#include "simpio.h"

void getlength(int row, int column);
void getshape(int row, int column, char shape[][]);
void fill(int row, int column, char shape[][]);
void returnResult(char result);
int main()
{
    int row, column;
    getlength(row, column);
    char shape[row][column];
    getshape(shape);
    fill(row, column, shape);
    char result;
    returnResult(result);
    getchar();
    return 0;
}
void getlength(int row, int column)
{
     printf("Enter the number of rows\n");
     row=GetInteger();
     printf("Enter the number of columns\n");
     column=GetInteger();
}

void getshape(int row, int column, char shape[][])
{
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape=getchar();
     }
}
void fill(int row, int column, char shape[][])
{
     // cant think of anything
}

Recommended Answers

All 83 Replies

If I am reading this correctly, then what you have is a blank square
like so :

#####
#   #
#   #
#####

or something similar. To that. Then the uses specifies the row and the
column or the (x,y) coordinate, per say, that is within the space of
the square. For example it could be this :

#####
#   #
# s #
#   #
#####

the "s" represents the start coordinate. Now you need to fill the
rectangle with some characters? Before I say something, Are these
assumptions correct?

Yes firstperson that is correct. I am supposed to fill in the shape with characters say asterisks.

Ok. Did you know learn about recursion?

Yes. It's where the function calls itself(and in main).

Ok, give me 1 quick example of recursion. And FYI, I am going somewhere with this.

Ok, give me 1 quick example of recursion. And FYI, I am going somewhere with this.

here is a program that is recursive

#include <stdio.h>

unsigned long factorial(unsigned n);
int main()
{
    int f;
    f=factorial(4);
    printf("%d ", f);
    getchar();
    return 0;
}
unsigned long factorial(unsigned n)
{
         if (n==0) {return (1);}
         else
         {
             return (n*factorial(n-1));
         }
}

Ok you know that for every recursive function we need a base case, right?
Otherwise the stack will overflow, memory overloaded?

So before we get to the base case. Do you have any idea on how
the fill function might work. For example we could linearly go through the
whole array and look for a space and replace it with asterisk, but thats
not allowed.

Yes I know that the function needs a base case. But no I'm not sure of one. Unless using

if (isspace(shape[row][column]==true))

is ok?

Ok think about it.

1) We have a wall, '#', where if we reach that wall we need not to draw
2) We have a (row,col) inside the rectangle, which will be our starting point
3) What we need to do is fill the whole rectangle.

One way to do this is use a recursive function.
From our starting point, we should move left , right, up, and down,
while filling the array as we move. And if we hit a wall, '#', then we
should return. From this statement can you deduce what the base
case should be?

I'm sorry but i can't think of one.

Would it be ok if I used for loops?

Well the base case should be something like this :

if coordinate is invalid return;
else if value at index (i,j) == Wall, return;
else ...

How is this?

if (r < 0 || r > row || c > column || c < 0) return;
     else if (shape[r][c] == '*' || shape[r][c] == '#') return;
     else
     {
         shape[r][c] = '*'; 
     }

However, I see one problem: the values don't change and I'm still working on that.

here's the code I have so far.

#include <stdio.h>
#include "simpio.h"

void getlength(int row, int column);
void getshape(int row, int column, char shape[][]);
void getinterior(int r, int c);
void fill(int row, int column, char shape[][]);
void returnResult(char result);
int main()
{
    int row, column;
    int r, c;
    getlength(row, column);
    char shape[row][column];
    getshape(shape);
    getinterior(r, c);
    fill(row, column, shape);
    char result;
    returnResult(result);
    getchar();
    return 0;
}
void getlength(int row, int column)
{
     printf("Enter the number of rows\n");
     row=GetInteger();
     printf("Enter the number of columns\n");
     column=GetInteger();
}

void getshape(int row, int column, char shape[][])
{
     char end='.';
     printf("Enter a shape and when your done enter '.'\n");
     while ((end=getchar()) != '.')
     {
           shape=getchar();
     }
}
void getinterior(int r, int 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();
}
void fill(int row, int column, char shape[][])
{
     if (r < 0 || r > row || c > column || c < 0) return;
     else if (shape[r][c] == '*' || shape[r][c] == '#') return;
     else
     {
         shape[r][c] = '*';
     }
}

So our base case checks if the index is valid, i.e if its positive and within the bounds of the array. Another base case we have is to check
if the value at Array[j] hits the wall or if its already occupied.
In real code This would be our code so far :

void Fill(char Array[R][C], int i, int j )
{
	if(i < 0 || j < 0 || i > R || j > C)  //if !(positive and within bounds)
		return;
       //if we hit a wall or if its already filled 
	if(Array[i][j] == '#' || Array[i][j] == '*') 
		return;

  //logic goes here

In the logic goes here what do you think we need to do? Also for your char shape[][], the column needs to be specified, so you need to do something like this , char shape[][COL], where COL is a const variable declared somewhere in global or something.

Change the value(that isn't out of bounds and isn't the wall) to a asterisk.

And I wouldn't use an else?

Yes that part of it. You have the part that does the actual filling.

So you have added this to the function.

void Fill(char Array[R][C], int i, int j )
{
	if(i < 0 || j < 0 || i > R || j > C) 
		return;
	if(Array[i][j] == '#' || Array[i][j] == '*') 
		return;
	
	Array[i][j] = '*';

  //move position
}

Now as you can see, we need to move the position. Since this is an
array we can only move up , down , left, or right. How would
you move the array while making sure that it will be filled? (Hint : recursion )

>> And I wouldn't use an else?
It does not matter.

I'm not sure because the normal r+1, j+1, r-1, j-1, etc wouldn't work since the length and width can change depending on the user.

I'm not sure because the normal r+1, j+1, r-1, j-1, etc wouldn't work since the length and width can change depending on the user.

Well we know that we have to start within the the walls. It would
not make sense that we start at the position of the wall. So for now
say we started at the middle of the hollow array. How would you
make it so that we transverse in all direction, left,right,up and down?

I'm not sure.

You know we have to now move around the array and fill it.
We can do something like this :

void Fill(char Array[R][C], int i, int j )
{
	if(i < 0 || j < 0 || i > R || j > C) 
		return;
	else if(Array[i][j] == '#' || Array[i][j] == '*') 
		return;
	
	Array[i][j] = '*';
	
	Fill(Array,i+1,j);
        //more Fill(...)
}

The code Fill(Array,i+1,j) fills the array down from its starting position.
Now we need to also fill it left, right, and up. What do you think
the definition for LEFT, or RIGHT, or UP should look like.

fill(i+2, j) is down? wouldn't fill(i-2, j) be down?

No realize that (i+1,j) , (i+2,j) ... was showing you a sequence.
In code the sequence is generated by the code.

Remember that in array, moving down amounts to increasing the y,
while moving up amounts to decreasing the y. Its not like the
Cartesian coordinate system.

In array its like so :

//column
   0 1 2 
{ a b c } 0 //row
{ d e f } 1 //row
{ h i  j } 2 //row

See that row 2 is lower than row 1, thus as row increases, we are
going down the array.

oops dory forgot about that.

Ok so now we know that , Fill(Array,i+1,j); is down. So whats up? Hint the opposite.

up: fill(i-1, j)
left: fill(i, j-1)
right: fill(i, j+1)
down: fill(i+1, j)

Good. Now combine everything together to get this :

void Fill(char Array[ROW][COL], int row, int col )
{
	if(row < 0 || col < 0 || row > ROW || col > COL) 
		return;
	else if(Array[row][col] == '#' || Array[row][col] == '*') 
		return;
	
	Array[row][col] = '*';
	
	Fill(Array,row+1,col);//down
	Fill(Array,row-1,col);//up
	Fill(Array,row,col-1);//left
	Fill(Array,row,col+1);//right
}

Ok thanks for helping me now I just have to fix the errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.