I had to program a similar problem using goto statements. Now we are asked to rewrite our code without using goto statement. I don't know how to begin doing this program. I paste in the previous program code using the goto.
// Eight Queens problem using one dimesional array and goto statement
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int q[8];
q[0] = 0;
int c = 0;
int count = 0;
NC: //cout << "Next column\n" << "Column = " << c << endl;
c++;
if (c == 8) goto print;
q[c] = -1;
NR: //cout << "Next row\n" << "Row = " << q[c] << "\nColumn = " << c << endl;
q[c]++;
if (q[c] == 8) goto backtrack;
for(int i = 0; i < c; i++){
if(q[i] == q[c] || abs(q[c] - q[i]) == (c - i))
goto NR;
}
goto NC;
backtrack:
//cout << "Backtrack" << endl;
//cout <<"Column = " << c << endl;
c--;
if(c == -1) return 0;
goto NR;
print:
//cout << "print" << endl;
++count;
cout << count << endl;
for(int i = 0; i <= 7; i++){
cout << q[i];
}
cout << endl;
goto backtrack;
return 0;
}
This program is a hint the professor posted for the class to use.
#include <iostream>
#include<cstdlib>
#include <cmath>
using namespace std;
bool ok(int q[], int col){
if the configuration is “bad” return false;
else
return true;
}
void backtrack(int &col){
col--;
if(col==-1) exit(1);
}
void print(int q[]){
static int count =0;
print the array q
}
int main(){
int q[8]; q[0]=0;
int c=1;
// from_backtrack keeps track if we need to reset the row to the
// top of the current colum or not.
bool from_backtrack=false;
// The outer loop keeps looking for solutions
// The program terminates from function backtrack
// when we are forced to backtack into column -1
while(1){
while(c<8){ //this loop goes across columns
// if we just returned from backtrack, use current value of row
// otherwise get ready to start at the top of this column
if(!from_backtrack) // we did not just return from backtrack
Code goes here
from_backtrack=false;
while(q[c]<8){ // place queen in this column
q[c]++;
// if row=8, there is no valid square in this column
// so backtrack and continue the loop in the previous column
Code goes here
//if this position is ok, place the queen
// and move on (break) to the next column,
// otherwise keep looking in this column
Code goes here
}
c++; // placed ok, move to the next column
}
// one complete solution found, print it.
print(q); // board completed, print it out
backtrack(c);
from_backtrack=true;
}
}
And this is an attempted I've made to completed the program
// NoGoto.cpp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
bool attack(int q[], int col){
if(q[i] == q[c] || abs(q[c] - q[i]) == (c - i)) return false;
else
return true;
} // Attack
void backtrack(int & col){
col--;
if(col == -1) exit(1);
} // Backtrack
void print(int q[]){
static int count = 0;
++count;
cout << count << endl;
for(int i = 0; i < 8; i++)
cout << q[i];
cout << endl;
}
int main()
{
int q[8];
q[0] = 0;
int c = 1;
bool from_backtrack = false;
while(1){
while(c < 8){ // this loops across columns
if(!from_backtrack)
attack(q[c],c)
from_backtrack = false;
while(q[c] < 8){ // place queen in this column
q[c]++;
return 0;
}
"I'm having problem writing the code. How [can I] call each function to make it correctly find the solutions?"