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 3226442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:27:26+00:00 2026-05-17T16:27:26+00:00

I have written a function in C++ code for the eight queens problem .

  • 0

I have written a function in C++ code for the eight queens problem. The program is supposed to print out all 92 possible solutions. I only can run up to 40. Don’t know where the problem is. Try to debug but I’m still stuck.

#include "stdafx.h"
#include <cmath>
#include <iostream>
using namespace std;

bool ok(int board[8][8]){
    for(int c = 7; c > 0; c--){
        int r = 0;
        while(board[r][c] != 1 ){
            r++;
        } // while loop

        for(int i = 1; i <= c; i++){
            if(board[r][c-i] == 1)
                return false;
            else if (board[r-i][c-i] == 1)
                return false;
            else if (board[r+i][c-i] == 1)
                return false;
        } // for loop

    } // for loop
        return true;
} // ok

void print(int board[8][8], int count){
    cout << count << endl;
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            cout << board[i][j];
        } // for loop 
        cout << endl;

    } // for loop

    cout << endl;
} // print board

int main (){

    int board[8][8]={0};
    int count = 0;
    for(int i0 = 0; i0 < 8; i0++)
       for(int i1=0; i1 < 8; i1++)
          for(int i2 = 0; i2 < 8; i2++)
         for(int i3 = 0; i3 < 8; i3++)
            for(int i4 = 0; i4 < 8; i4++)
           for(int i5 = 0; i5 < 8; i5++)
              for(int i6 = 0; i6 < 8; i6++)
                 for(int i7 = 0; i7 < 8; i7++){
                board[i0][0]=1;
                            board[i1][1]=1;
                            board[i2][2]=1;
                            board[i3][3]=1;
                            board[i4][4]=1;
                            board[i5][5]=1;
                            board[i6][6]=1;
                            board[i7][7]=1;

                            if(ok(board))print(board, ++count);

                            board[i0][0]=0;
                            board[i1][1]=0;
                            board[i2][2]=0;
                            board[i3][3]=0;         
                            board[i4][4]=0; 
                            board[i5][5]=0;
                            board[i6][6]=0;
                            board[i7][7]=0;

                                }
    return 0;
}
  • 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-17T16:27:27+00:00Added an answer on May 17, 2026 at 4:27 pm

    Your problem is in the ok function. It has three errors, all relating to the bounds of your matrix. The first error (which will if anything cause you to receive too many solutions), is here:

    for(int c = 7; c > 0; c--){
    

    This will never check column 0. The test should be c >= 0.

    The other two errors, which cause unpredictable behavior, are here:

        for(int i = 1; i <= c; i++){
            if(board[r][c-i] == 1)
                return false;
            else if (board[r-i][c-i] == 1)
                return false;
            else if (board[r+i][c-i] == 1)
                return false;
        } // for loop
    

    This can cause the ok function to return an arbitrary number of false negatives. In my case, compiling and running your program with these two errors produced no solutions. It is only by chance that it produces 40 solutions for you.

    The problem is again with bounds. The i variable is moving from 1 up to and including c, so c-i moves down from c-1 to 0, as intended.

    However, you are not checking that r-i and r+i remain within the bounds of the matrix. Consider the case that r = 7 and i = 4. Then, r+i = 11, which runs past the end of the row. Similarly, if r = 0 and i is anything other than 0, r-i will be negative and run past the beginning of the row.

    You need to add additional checks to make sure that the row values used in the tests within this loop are within the range 0 to 7. You can take advantage of the short-circuiting behavior of the logical operators in C++ to do this, for example:

     else if (<test> && board[r-i][c-i] == 1)
    

    will examine board[r-i][c-i] only if <test> is true.

    I’m leaving adding the corrections to these second two errors as an exercise for you, since this is very likely to be a homework assignment (and if it is, you should add the [homework] tag to the question).

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

Sidebar

Related Questions

I have a function I've written that was initially supposed to take a string
I have written a function that gets a given number of random records from
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have written some code in my VB.NET application to send an HTML e-mail
For our online game, we have written tons of PHP classes and functions grouped
I have written an AIR Application that downloads videos and documents from a server.
I have written a site in Prototype but want to switch to jQuery. Any
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content

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.