Two Dimensional Vector Fill using recursive functions?

Page 1 of 1

3 Replies - 4245 Views - Last Post: 11 February 2009 - 06:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86211&amp;s=9893a16c28933595f65819914a7131e5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Euraty

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 07-February 09

Two Dimensional Vector Fill using recursive functions?

Posted 11 February 2009 - 04:14 PM

Hi, for my programming assignment, I have to input a picture that looks like this...

.....xx........
....x..xx......
....x....xxxx..
...x........xxx
..x............
...x........xx.
...x..xxx...x.x
...x..x..x.x...
...xxxx...x....
...............

into a two dimensional vector, which I was able to do. Now however, I am supposed to ask the user for a starting point (which I did), and then fill the "." with"x" (using a recursive function) as long as I stay inside the borders of the "x"s. Like this...

.....xx........
....xxxxx......
....xxxxxxxxx..
...xxxxxxxxxxxx
..xxxxxxxxxxxxx
...xxxxxxxxxxx.
...xxxxxx...x.x
...xxxx..x.x...
...xxxx...x....
...............

Here is my code so far...

```#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void input(int &pixelrow, int &pixelcolumn);
void AreaFill(vector<vector<char> >&picture, int pixelrow, int pixelcolumn);
void display(vector<vector<char> >picture);

int main()
{
ifstream infile("picture.txt");
if(!infile)
{
cout<<"File could not be opened."<<endl;
exit(1);
}
vector<vector<char> >picture(1);
string line;
int rows=0, pixelrow, pixelcolumn;
while(getline(infile, line))
{
picture[rows].resize(line.size());
for(int x=0; x<line.size(); x++)
picture[rows][x]=line[x];
picture.resize(picture.size()+1);
rows++;
}

display(picture);
input(pixelrow, pixelcolumn);

AreaFill(picture, pixelrow, pixelcolumn);

display(picture);
}

void input(int &pixelrow, int &pixelcolumn)
{
cout<<"Enter starting pixel location"<<endl;
cin>>pixelrow>>pixelcolumn;
}

void AreaFill(vector<vector<char> >picture, int pixelrow, int pixelcolumn)
{
if(pixelrow>0 && pixelcolumn>0 && pixelcolumn<picture[0].size() && pixelrow<picture.size() && picture[pixelrow][pixelcolumn]!='x')
picture[pixelrow][pixelcolumn]='x';
AreaFill(picture, pixelrow-1, pixelcolumn);
AreaFill(picture, pixelrow, pixelcolumn+1);
AreaFill(picture, pixelrow+1, pixelcolumn);
AreaFill(picture, pixelrow, pixelcolumn-1);
}

void display(vector<vector<char> >picture)
{
for(int x=0; x<picture.size(); x++)
{
for(int y=0; y<picture[x].size(); y++)
cout<<picture[x][y];
cout<<endl;
}
}

```

I get 4 errors, one for each time that I call the AreaFill function (my recursive function). The error says...
"call of overloaded `AreaFill(std::vector<std::vector<char, std::allocator<char> >, std::allocator<std::vector<char, std::allocator<char> > > >&, int, int&)' is ambiguous"

This is my first program using recursive functions so I am not sure if I am approaching this right. Any help would be greatly appreciated. Thanks.

Is This A Good Question/Topic? 0

Replies To: Two Dimensional Vector Fill using recursive functions?

#2 krum110487

• D.I.C Regular

Reputation: 39
• Posts: 291
• Joined: 07-February 09

Re: Two Dimensional Vector Fill using recursive functions?

Posted 11 February 2009 - 05:12 PM

this is the steps I believe it is taking...

it runs areafill, checks the if statement, then changes or not, then it ALWAYS goes to:

"AreaFill(picture, pixelrow-1, pixelcolumn);"

there is no condition that will make it go to any of the other recursive calls, and I don't believe there is a terminating call, meaning, once this is running, there is nothing that will stop it.

you need something like...

```{
if(pixelrow>0 && pixelcolumn>0 && pixelcolumn<picture[0].size() && pixelrow<picture.size() && picture[pixelrow][pixelcolumn]!='x')
picture[pixelrow][pixelcolumn]='x';

if(there is nothing to pixelrow-1)
AreaFill(picture, pixelrow-1, pixelcolumn);
elseif (there is nothing to the right)
AreaFill(picture, pixelrow, pixelcolumn+1);
elseif (etc...)
AreaFill(picture, pixelrow+1, pixelcolumn);
elseif (etc...
AreaFill(picture, pixelrow, pixelcolumn-1);

}
}
```

the terminating call will be when the dot is surronded by x's but with this code I don't think it will work...but I just wanted to give you an idea of what it may be.

although, I am not at home so I can't properly examine the code or test :-(

EDIT:
ok on my drive home from work, I thought about it and I think I have it.

it needs to be something like...

```{
if(pixelrow>0 && pixelcolumn>0 && pixelcolumn<picture[0].size() && pixelrow<picture.size() && picture[pixelrow][pixelcolumn]!='x')
picture[pixelrow][pixelcolumn]='x';

if(up is not an X)
AreaFill(picture, pixelrow-1, pixelcolumn);
elseif (right is not an X)
AreaFill(picture, pixelrow, pixelcolumn+1);
elseif (down is not an X)
AreaFill(picture, pixelrow+1, pixelcolumn);
elseif (left is not an X)
AreaFill(picture, pixelrow, pixelcolumn-1);

}
}
```

I will test this and get back to you!

This post has been edited by krum110487: 11 February 2009 - 05:51 PM

Was This Post Helpful? 0

#3 baavgai

• Dreaming Coder

Reputation: 7183
• Posts: 14,971
• Joined: 16-October 07

Re: Two Dimensional Vector Fill using recursive functions?

Posted 11 February 2009 - 06:10 PM

You're pretty much there. However, if the pixel positions are outside of range, you just want to leave; don't try to do anything else. If the pixel is 'x', also leave.

Here's the test code I tried; it seemed to work.

```#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

// vector vector is a mouthful, be nice to yourself
typedef vector<vector<char> > PictureType;

void AreaFill(PictureType &picture, int pixelrow, int pixelcolumn) {
if(pixelrow<0 || pixelrow>=picture.size()) { return; }
if(pixelcolumn<0 || pixelcolumn>=picture[0].size()) { return; }
if(picture[pixelrow][pixelcolumn]=='x') { return; }

picture[pixelrow][pixelcolumn]='x';
AreaFill(picture, pixelrow-1, pixelcolumn);
AreaFill(picture, pixelrow, pixelcolumn+1);
AreaFill(picture, pixelrow+1, pixelcolumn);
AreaFill(picture, pixelrow, pixelcolumn-1);
}

void display(PictureType &picture) {
for(int x=0; x<picture.size(); x++) {
for(int y=0; y<picture[x].size(); y++) {
cout<<picture[x][y];
}
cout<<endl;
}
}

// makes life easer
void addLine(PictureType &picture, const string &line) {
vector<char> newRow(line.size());
for(int x=0; x<line.size(); x++) {
newRow[x] = line[x];
}
picture.push_back(newRow);
}

int main() {
PictureType picture;

addLine(picture, ".....xx........");
addLine(picture, "....x..xx......");
addLine(picture, "....x....xxxx..");
addLine(picture, "...x........xxx");
addLine(picture, "..x............");
addLine(picture, "...x........xx.");
addLine(picture, "...x..xxx...x.x");
addLine(picture, "...x..x..x.x...");
addLine(picture, "...xxxx...x....");
addLine(picture, "...............");

display(picture);
AreaFill(picture, 4, 4);

cout << endl << endl;
display(picture);

return 0;
}

```

Was This Post Helpful? 0

#4 krum110487

• D.I.C Regular

Reputation: 39
• Posts: 291
• Joined: 07-February 09

Re: Two Dimensional Vector Fill using recursive functions?

Posted 11 February 2009 - 06:25 PM

here is your problem from your code:

your declare statement:
```void AreaFill(vector<vector<char> >&picture, int pixelrow, int pixelcolumn)
```

your function:
```void AreaFill(vector<vector<char> >picture, int pixelrow, int pixelcolumn)
```

you put in the ampersand and then those errors will go away, BUT your code wont work, you can refer to baavgai's code.

good luck!

need anymore help just post.

This post has been edited by krum110487: 11 February 2009 - 06:41 PM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }