Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:10:47+00:00 2026-06-04T03:10:47+00:00

So recursion is not my strong point, and I have been challenged to make

  • 0

So recursion is not my strong point, and I have been challenged to make a recursive floodFill function that fills a vector of a vector of ints with 1’s if the value is zero. My code keeps segfaulting for reasons beyond me. Perhaps my code will make that sound more clear.

This is the grid to be flood filled:

vector<vector<int> > grid_; 

It belongs to an object I created called “Grid” that is basically a set of functions to help manipulate the vectors. The grid’s values are initialized to all zeros.

This is my flood fill function:

void floodFill(int x, int y, Grid & G)
{

if (G.getValue(x,y))
{
    G.setValue(x,y,1);
    if(x < G.getColumns()-1 && x >= 0 && y < G.getRows()-1 && y >= 0)
    floodFill(x+1,y,G);

    if(x < G.getColumns()-1 && x >= 0 && y < G.getRows()-1 && y >= 0)
    floodFill(x,y+1,G);

    if(x < G.getColumns()-1 && x >= 0 && y < G.getRows()-1 && y >= 0)
    floodFill(x-1,y,G);

    if(x < G.getColumns()-1 && x >= 0 && y < G.getRows()-1 && y >= 0)
    floodFill(x,y-1,G);

}       
}  

The intention here is to have the function check if a point’s value is zero, and if it is, change it to one. Then it should check the one above it for the same. It does this until it either finds a 1 or hits the end of the vector. Then it tries another direction and keeps going until the same conditions as above and so on and so forth until its flood filled.

Can anyone help me fix this? Maybe tell me whats wrong?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:10:49+00:00Added an answer on June 4, 2026 at 3:10 am
    if(x < G.getColumns()-1 && x >= 0 && y < G.getRows()-1 && y >= 0)
        floodFill(x-1,y,G);
    

    won’t work, since you can access index -1 of the underlying vector if x == 0

    Same goes for floodFill(x,y-1,G);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursion function that parses an object/array with a global variable. If
I have been messing around with recursion today. Often a programming technique that is
is there any way to have a tail-recursive function inside CPS not throwing a
Often people say that it's not recommended to use recursive functions in python (recursion
I know that recursion is sometimes a lot cleaner than looping, and I'm not
It was pointed out to me that the statement below is not recursion. I
I need a recursive function, that would print a class signature, including class name
I think I have the recursion working properly, but I'm not able to get
I think allowing recursion can be very handy sometimes, not because I can code
Using recursion, find an index that cuts an array in two parts so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.