Sunday, May 10, 2009

Urgent C++ Help?

I need to code up the famous eightqueens problem w/O using goto statements.





#include %26lt;iostream%26gt;


using namespace std;








bool okay(int q[], int col){


for (int i=0 ; i %26lt; col; i++)


if( q[col] == q[i] || (col-i) == abs(q[col]-q[i])) return false;


return true;


}





void print(int q[], int s){








}


void backtrack(int q[], int col){








}


int main () {


int q[8] , c , i , j;


int cntr = 0;


q[0] = 0;


c =0;


c++;


while (c ==8) {


cntr++;


print(q,8);


break;


cout %26lt;%26lt; "Solution# " %26lt;%26lt; cntr %26lt;%26lt; ": ";


continue;


}


q[c] = -1;





q[c]++;





while ( q[c] == 8 ){ break;


backtrack(q,c);





c--;


if ( c== -1) exit (0);


continue;


/***************************************...


for (i -0 ; i %26lt;c ;i ++)


if (q[i] ==q[c] || c-i == abs(q[c] - q[i]) )


****************************************...


//put in ok function


okay(q,c);


cout %26lt;%26lt; "Solution# " %26lt;%26lt; cntr %26lt;%26lt; ": ";


for ( i= 0 ; i %26lt;8 ; i++ )


cout %26lt;%26lt; q[i] %26lt;%26lt; " " ;


cout %26lt;%26lt; endl;


break;


c--;


if ( c == -1 ) exit (0);


}


return 0;


}

Urgent C++ Help?
Not to be harsh but this is a mess. Worrying about gotos is the least of your problems. You have written a program that effectively does nothing.





For starters:





int main ()


{


............





c =0;


c++; //why not just c = 1?





while (c ==8) //will never execute because c == 1!





q[c] = -1;


q[c]++; //again, why not just q[c] = 0?





while ( q[c] == 8 ) //again, won't execute because it is 0!





At least get this much running. Comment your code so you (and maybe us) have some idea where you are going with this.
Reply:Does not look easy. May be you can search project assignment website like http://askexpert.info/


No comments:

Post a Comment