To Michael Jones

Thanks that you don't reply me directly the 8 queens problem. Yes in fact what I want is some guideline.

Before I posted the message I have tried one method, but there are too many trials that will need too much time to run the programme.

In fact I'm a c++ beginner, and now I write a new programme for the 8 queens problem.

But the complier just leave 'segmentation fault' to me and do nothing, I don't know whether it is related to my programme, can you have a look please?




#include


const int size=8;

int x[size]={1}, counter=0; //eg x[2]=6 means in colomn 2 the queen is placed at row 6.

int check(int,int);

void solution();


int main(){

check(1,1);

solution();

return 0;}


int check(int n,int row){

if (n==size+1) return 0;

if (row==size+1)

{check(n-1,x[n-1]+1); return 0;}

for (int i=0; i<=size-1; i++){<br>
if (x[i]==row || x[i]-(n-i)==row || x[i]+(n-i)==row)

{check(n,row+1); return 0;}}

x[n]=row;

check(n+1,1);

return 0;}


void solution(){

counter++;

for (int i=0;i<=size-1;i++)<br>
cout<<x[i];<br>
cout<<endl;}<p>
Thanks a lot.


Laybe





Comments

  • I know I'm not Michael Jones, but I think I can see why you are getting segmentation faults.


    A C/C++ style array, if it is size big (int array[size]), then the slots start from 0, and end at size-1. Your check function will end up checking negative numbers (If you have a debugger, step through and watch the n variable to see what I mean) This means that you are accessing outside the array, and when you hit the right area of memory, BOOM seg fault! There are some other places you act like the array is from 1 to size, as well. Also, when initializing the array, you might want to use a for loop, like

    for (int j=0;j
    x[j]=0; //or 1

    }


    I don't know offhand how to solve the 8 Queens problem, so I can't offer some guidelines. Maybe when MJ gets back, he can help with some.


    MLINK


    : Thanks that you don't reply me directly the 8 queens problem. Yes in fact what I want is some guideline.

    : Before I posted the message I have tried one method, but there are too many trials that will need too much time to run the programme.

    : In fact I'm a c++ beginner, and now I write a new programme for the 8 queens problem.

    : But the complier just leave 'segmentation fault' to me and do nothing, I don't know whether it is related to my programme, can you have a look please?


    :

    : #include


    : const int size=8;

    : int x[size]={1}, counter=0; //eg x[2]=6 means in colomn 2 the queen is placed at row 6.

    : int check(int,int);

    : void solution();


    : int main(){

    : check(1,1);

    : solution();

    : return 0;}


    : int check(int n,int row){

    : if (n==size+1) return 0;

    : if (row==size+1)

    : {check(n-1,x[n-1]+1); return 0;}

    : for (int i=0; i<=size-1; i++){<br>
    : if (x[i]==row || x[i]-(n-i)==row || x[i]+(n-i)==row)

    : {check(n,row+1); return 0;}}

    : x[n]=row;

    : check(n+1,1);

    : return 0;}


    : void solution(){

    : counter++;

    : for (int i=0;i<=size-1;i++)<br>
    : cout<<x[i];<br>
    : cout<<endl;}<p>
    : Thanks a lot.


    : Laybe





  • : Thanks that you don't reply me directly the 8 queens problem. Yes in fact what I want is some guideline.


    Glad to hear it.




    : Before I posted the message I have tried one method, but there are too many trials that will need too much time to run the programme.


    Yes, that's a problem, isn't it. =) With computers getting ever faster, it's becoming entirely too easy to depend on that. I prefer more elegant solutions myself.




    : In fact I'm a c++ beginner, and now I write a new programme for the 8 queens problem.




    Its an interesting puzzle.




    : But the complier just leave 'segmentation fault' to me and do nothing, I don't know whether it is related to my programme, can you have a look please?




    As MLINK suggested, and as I posted in a different message, a segmentation fault or a general protection fault typically occurs when you write to or read from memory that doesn't belong to you. Using invalid pointers or going off the ends of the array qualify in this regard. Check your array bounds.


    For an array of size n, you can only use indices 0 through n-1 inclusively without being 'logically wrong'. Sadly, even if you go outside of this range, you may never find out because you may simply be overwriting memory that you do have access to. Even if it doesn't yell at you about it, it's still 'logically wrong' to go outside the boundaries of an array.


    Your code is too cluttered in this environment for me to decipher and I've got work to do. Mail me with your properly formatted easy-to-read code if you absolutely can't find the array problem yourself. I suggest you look long and hard at your arrays before you do, though. I won't be able to respond quickly.





    :

    : #include


    : const int size=8;

    : int x[size]={1}, counter=0; //eg x[2]=6 means in colomn 2 the queen is placed at row 6.

    : int check(int,int);

    : void solution();


    : int main(){

    : check(1,1);

    : solution();

    : return 0;}


    : int check(int n,int row){

    : if (n==size+1) return 0;

    : if (row==size+1)

    : {check(n-1,x[n-1]+1); return 0;}

    : for (int i=0; i<=size-1; i++){<br>
    : if (x[i]==row || x[i]-(n-i)==row || x[i]+(n-i)==row)

    : {check(n,row+1); return 0;}}

    : x[n]=row;

    : check(n+1,1);

    : return 0;}


    : void solution(){

    : counter++;

    : for (int i=0;i<=size-1;i++)<br>
    : cout<<x[i];<br>
    : cout<<endl;}<p>
    : Thanks a lot.


    : Laybe





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories