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

  • SEARCH
  • Home
  • 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 6666181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:48:06+00:00 2026-05-26T02:48:06+00:00

Basically, I have to write a basic program that solves the n-queen problem, which

  • 0

Basically, I have to write a basic program that solves the n-queen problem, which I have done, but it throws a segmentation fault if I input any number >=11.

From what I have read online, this error is usually caused by faulty logic when dealing with memory, but I can’t seem to figure out what I have done wrong.

void generateBoard(int board[],int column,int length,int count)
{
    if(column == 0 && board[0]<length)  //prevents outputting the results infinitely 
    {
        ++board[0];
        generateBoard(board, ++column, length, count);
    } 
    else 
    {
        bool lineNotFound = true;
        int row = board[column];
        while(lineNotFound && row < length)
        {
            ++row; //temporary value for a column value candidate
            lineNotFound = false;
            for(int i = 0; i < column && !lineNotFound; ++i)
            {
                if(board[i] == row || (board[i]+column-i) == row || (board[i]-column+i) == row) // check diagonal and horizontal
                {
                    lineNotFound = true;
                }
                else
                {
                    board[column] = row;
                }
            }
        }
        if(column == length-1 && !lineNotFound) // at last column and valid board
        {
            output(board,length,++count);
            generateBoard(board,column,length,count);
        }
        else if(!lineNotFound) // not at last column, but valid position found
        {
            generateBoard(board,++column,length,count);
        }
        else if(column != 0) //no valid columns, go back a step
        {
            board[column] = 0;
            generateBoard(board,--column,length,count);
        }
    }
}

I realize that is a big chunk of code, but I think it’s necessary to post it all to get an idea of the problem.

Any ideas? :s

I’m new to programming c++, so I don’t know where to start debugging this.

  • 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-05-26T02:48:06+00:00Added an answer on May 26, 2026 at 2:48 am

    Start debugging at the beginning and then let it run till the segmentation fault. When the fault occurs – look at the stack. I would guess that you’re exceeding your stack with the recursion – that’s the main problem with recursion programs.

    You can enlarge your stack and then the fault will occur not with input 11 but with some other number, but recursive programs will always crash on stack issues with the input large enough.

    By the way – make sure your recursion is bounded, i.e.: at some point your function should exit without calling itself further. IMHO, this is better be done at the beginning (although its a matter of style and convenience), because then you’ll see clearly the condition upon which the recursion ends and it will be easier to debug infinite recursions (which also cause seg faults because of stack exhaustion). In your case it is not immediately clear how the recursion ends, and I wouldn’t be surprised that it doesn’t for some inputs.

    clarification

    While on some systems you get a “Stack Overflow” error, on others you would get “Segmentation Fault” for the same thing. I’m guessing you’re on one of the “others”.

    To show that, I just compiled and ran this code:

    int foo(long a)
    {
      return foo(a-1);
    }
    
    
    int main()
    {
       return foo(9999999999L);
    }
    

    On my GCC/Ububtu machine. This program has infinite recursion, which ends up with “segmentation fault” crash.

    You can convert any recursive algorithm into iterative. Instead of recursively calling the function with new variables, use the STL std::stack to push and pop them and run in a loop. Here the details : http://www.cplusplus.com/reference/stl/stack/

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

Sidebar

Related Questions

I have a very simple problem and a solution that will work, but I'm
I basically have to write a clone of the UNIX ls command for a
I have been playing with the Ruby library shoes. Basically you can write a
Basically my problem stems from a desire to have the textbox portion be white,
I basically have a page which shows a processing screen which has been flushed
So I am really stumped because I have basic ideas but I am looking
basically what I am trying to do is, I have an array that looks
I have a C program which has multiple worker threads. There is a main
I have a DLL in C# that encrypts and decrypts string texts (something basic),
So I have a basic application written in C#. It basically writes a file

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.