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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:42:46+00:00 2026-06-11T00:42:46+00:00

I have written a program for the 8-Queens problem.It prints all possible solutions. queens()

  • 0

I have written a program for the 8-Queens problem.It prints all possible solutions.

queens() finds all the possible solutions.
ok() tells whether the given column and row is safe or not.

The very strange problem is:

‘Count’ won’t increment. I have no idea why.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

int arr[8][8]={0};
int count=0;

int ok(int k,int j)
{
 int i,l;
 int tup[8]={0};

 for(i=0;i<k;i++)
 {
  for(l=0;l<8;l++)
  {
   if(arr[i][l]==1)
   tup[i]=l;
  }
 }

 for(i=0;i<k;i++)
 {
  if((abs(tup[i]-j)==abs(i-k))||(tup[i]==j)||(arr[i][j]==1))
  return 0;
 }

 return 1;

}


void queen(int i)
{
 int j,k,temp;

 if(i==8)
 {
  count++;
  cout<<"Solution no. "<<count<<":\n";

  for(i=0;i<8;i++)
  {
   for(j=0;j<8;j++)
   {
    if(arr[i][j]==1)
    cout<<"Q";
    else
    cout<<"=";
    cout<<" ";
   }
   cout<<"\n";
  }
  cout<<"---------------\n";

  getch();

 }

 for(j=0;j<8;j++)
 {
  if(ok(i,j))
  {
   for(k=0;k<8;k++)
   arr[i][k]=0;
   arr[i][j]=1;
   queen(i+1);
  }
 }

 arr[i][j]=0;

}



int main()
{

 clrscr();    

 queen(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-06-11T00:42:48+00:00Added an answer on June 11, 2026 at 12:42 am

    You should return from the queen function after the i==8 part is done. Currently you continue, and at the end you execute arr[i][j]=0; with i==7 and j==8. This is one past the end of the array, and as count resides immediately after arr in memory, it gets reset to zero.

    Found using gdb: Break after incrementing count, set (hardware) watchpoint on count, and continue to see where count changes value again.

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

Sidebar

Related Questions

I have written a program to find all the possible permutations of a given
I have written a program to insert a row in the contact database which
I have written a program to insert an array in all the rows of
I have written a program in .NET that recurses through all files of a
I have written a function in C++ code for the eight queens problem .
I have written a program that prints a table. I have not included the
I have written php program and uploaded on server. I want run this program
I have written a program in VB6. When I compile it and send it
I have written a program that uses qhttp to get a webpage. This works
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>

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.