#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>

#define TRUE 1
#define FALSE 0

int t=1;
int place(int k,int n);
int queen(int k,int n);
int x[5];//index for column and value at that index is row number

int place(int k,int n)
{
int j;
for(j=1;j<k;j++)
{
if((x[j]==x[k])||(k-j)==abs(x[j]-x[k]))
{
//printf("hi\n");
t=0;
return t;
}
else
{
//printf("hello");
t=1;
return t;
}
}
}

int queen(int k,int n)
{
 int j;//here it is row loop index
 int a;
int t=5;
for(j=1;j<=4;j++)
{
//t=place(k,j);
//printf("%d",t);
//t=place(k,n);
//printf("%d",t);
if(t==1)
{
x[k]=j;
if(k==n)
{
for(a=1;a<=n;a++)
printf("%d",x[a]);
printf("\n");
return 0;
}
else
{
//printf("%d",x[j]);
queen(k+1,n);
}
}
}
}


int main()
{
static int n=4;
int i;
queen(1,n);
}

hello there are various useless printf statement not of use plzz just ignore and try to give me the correct solution through this code (MEANS THE BACTRACKING APPRAOCH)plzz tell me what i have done wrong with this code so that i am not getting correct value from function place i am new to programming

Edited 3 Years Ago by Dani: Fixed formatting

Attachments
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>

#define TRUE 1
#define FALSE 0

int t=1;
int place(int k,int n);
int queen(int k,int n);
int x[5];//index for column and value at that index is row number

int place(int k,int n)
{
int j;
for(j=1;j<k;j++)
{
if((x[j]==x[k])||(k-j)==abs(x[j]-x[k]))
{
//printf("hi\n");
t=0;
return t;
}
else
{
//printf("hello");
t=1;
return t;
}
}
}

int queen(int k,int n)
{
 int j;//here it is row loop index
 int a;
int t=5;
for(j=1;j<=4;j++)
{
//t=place(k,j);
//printf("%d",t);
t=place(k,n);
printf("%d",t);
if(t==1)
{
x[k]=j;
if(k==n)
{
for(a=1;a<=n;a++)
printf("%d",x[a]);
printf("\n");
return 0;
}
else
{
printf("%d",x[j]);
queen(k+1,n);
}
}
}
}


int main()
{
int arr[5][5];
static int n=4;
int i;
for(i=0;i<=4;i++)
{
arr[0][i]=0;
arr[i][0]=0;
}
queen(1,n);
}

what is this program supposed to do?
Just in terms of main, you only call queen. When i look into queen, all of your place statements have been commented out. With t = 5, absolutely nothing in queen will run. What I'm trying to say is, I can't figure out what your program is supposed to be doing. Having that would help, as well as what function place is supposed to be doing. Maybe some input and desired output would be useful, and what little magic you have your function doing to the input.

Ok, some things I've found that should help to get started in fixing this:
1) the array x is, in no way, defined. What's in there is garbage values, basically, whatever value the program that had that memory space before you left there. My comp teacher gave a good example. Imagine going to the gym and getting a locker. However, the person who had that locker before you left all their old stuff in it. It's your locker now, but it's filled with their useless stuff. You have to clean it out and put your stuff in. The same goes with c variables. You need to fill the array x with whatever data you want it to have.
2) when you called queen() out of main(), you passed the param k as 1. when this was passed to place() in either of your place() calls in queen()'s for loop, k was equal to 1.
In place()'s for loop, the loop only runs if j (which is 1) is less than k (which is 1). Do you see the problem? Your for loop in place() never actually runs because both j and k are 1.

Comments
good discussion

To have your code look like code (and be easily read), highlight your code, and click on the [code] icon in the editing window.

You need the code tag without the backslash char: '/', before the program starts, and the code tag WITH the backslash char, placed after the program ends.

The above procedure will do it for you, automatically.

Welcome to the Forum! ;)

Edited 6 Years Ago by Adak: n/a

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define R 50

int x[R], count;

int place(int k,int i)
{
	int j;
	for(j=1;j<k;j++)
	{
		if( x[j]==i || abs(x[j]-i)==abs(j-k) )
			return 0;
	}
	return 1;
}
void NQueens(int k,int n)
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		if(place(k,i)==1)
		{
			x[k]=i;
			if(k==n)
			{
				count++;
				cout<<"Solution "<<count<<".\n";
				cout<<" Row:Column = ";
				for(j=1;j<=n;j++)
					cout<<j<<":"<<x[j]<<" ";
				cout<<endl;
			}
			else NQueens(k+1,n);
		}
	}
}

int main()
{    
    int n;

	cout<<"  *** 8-Queens Chess Problem ***\n";
	do
	{	cout<<"\nEnter number of Queens(0 to quit) : ";
		cin>>n;

		count=0;
		NQueens(1,n);
	}while(n);
	return 0;
}

Edited 6 Years Ago by Nick Evan: Added code-tags

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define R 50

int x[R], count;

int place(int k,int i)
{
	int j;
	for(j=1;j<k;j++)
	{
		if( x[j]==i || abs(x[j]-i)==abs(j-k) )
			return 0;
	}
	return 1;
}
void NQueens(int k,int n)
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		if(place(k,i)==1)
		{
			x[k]=i;
			if(k==n)
			{
				count++;
				cout<<"Solution "<<count<<".\n";
				cout<<" Row:Column = ";
				for(j=1;j<=n;j++)
					cout<<j<<":"<<x[j]<<" ";
				cout<<endl;
			}
			else NQueens(k+1,n);
		}
	}
}

int main()
{    
    int n;

	cout<<"  *** 8-Queens Chess Problem ***\n";
	do
	{	cout<<"\nEnter number of Queens(0 to quit) : ";
		cin>>n;

		count=0;
		NQueens(1,n);
	}while(n);
	return 0;
}

Edited 6 Years Ago by Nick Evan: Added code-tags

This article has been dead for over six months. Start a new discussion instead.