Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Essentially what this program does is locate 8 queens on the chessboard without

ID: 3625897 • Letter: E

Question

Essentially what this program does is locate 8 queens on the chessboard without them killing one another. I tried it out but need help in fixing the code.

#include<iostream>
#include<cmath>
using namespace std;

bool ok(int * q,int i){
        for(int k=0; k<1;k++)
                if((q[k]==q[i] || (abs(q[k]-q[i])==i-k))
                        return false;
        else
                return true;
}
        void print( int * q){
                static int count=0;
                cout<<++count<<endl;
                for(int k=0;k<8; k++)
                        cout<<q[k];
                        cout<<endl;
}
void move(int* q, int i) {
        if(i==8){
                print(q);
                return;
}
        for(int j=0;j<8;j++){
                q[i]=j;
                if(ok(q,i))
        }
}

int main() {
        int q[8];
        move(q,0);
        system("pause")
}

The errors I got when I tried to compile were:recursive.cpp: In function ‘bool ok(int*, int)’:
recursive.cpp:8: error: expected `)' before ‘return’
recursive.cpp: In function ‘void move(int*, int)’:
recursive.cpp:27: error: expected primary-expression before ‘}’ token
recursive.cpp:27: error: expected `;' before ‘}’ token
recursive.cpp: In function ‘int main()’:
recursive.cpp:34: error: expected `;' before ‘}’ token

Explanation / Answer

Dear, i can't understood the program logic, so i cleared compiler mistakes only. #include <iostream>
#include <cmath>
using namespace std;

bool ok(int * q,int i){
        for(int k=0; k<1;k++)
                if((q[k]==q[i]) || (abs(q[k]-q[i])==i-k)) // un balanced braces
                        return false;
        else
                return true;
}
        void print( int * q){
                static int count=0;
                cout<<++count<<endl;
                for(int k=0;k<8; k++)
                        cout<<q[k];
                        cout<<endl;
}
void move(int* q, int i) {
        if(i==8){
                print(q);
                return;
}
        for(int j=0;j<8;j++){
                if(ok(q,i))
                     q[i]=j;
        }
}

int main() {
        int q[8];
        move(q,0);
        system("pause"); //you missed semicolon here
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote