c problem

W

wildthings

i was trying to complie/run the n queens problem on bloodshod
dev-c++(version4) as a c lang
project but it gives a huge no. of errors which i cant figure out
pls help
the code is:::

#include <iostream.h>
#include <stdlib.h>
#include <windows.h>

int c=0;
int try(int);
void drawboard(void);
int board[8][8];
int good();

int main()
{
int i,j;

for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[j]=0;

try(0);
printf("%d",c);
}

int try(int n)
{
int i;
for(i=0;i<8;i++)
{
board[n]=1;
if(n==7&&good()=1)
{
c++;
drawboard();
return(0);
}
if(n<7&&good()=1&&try(n+1)=1)
return(1);

board[n]=0;
}
return(0);
}

void drawboard()
{
inti,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%d",board[j]);
}
printf("/n");
}

int good()
{
int i,j,p,q;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
for(p=0;p<8;p++)
for(q=0;q<8;q++)
{
if(i==p||i==q||j==q||j==p||abs(i-p)==abs(j-q))
return(0);
else
return(1);
}
}
 
A

Artie Gold

wildthings said:
i was trying to complie/run the n queens problem on bloodshod
dev-c++(version4) as a c lang
project but it gives a huge no. of errors which i cant figure out

And these errors are?
pls help
the code is:::

#include <iostream.h>

Not a C header (not a C++ header either, but I digress....)
#include <stdlib.h>
#include <windows.h>

Platform-specific header; off topic.[bunches of non-indented non-commented code with meaningless identifiers
elided]
Please see: http://www.catb.org/~esr/faqs/smart-questions.html

HTH,
--ag
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

wildthings said:
the code is:::

#include <iostream.h>

C does not have an said:
#include <stdlib.h>
#include <windows.h>

C does not have a said:
int c=0;
int try(int);
void drawboard(void);
int board[8][8];
int good();

this is not a valid prototype.
int main()

better yet, "int main(void)".
{
int i,j;

lack of indentation makes the code unnecessarily hard to read.
for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[j]=0;

try(0);
printf("%d",c);


printf() is varadic, but is called without a prototype in scope.

missing return statement (this is not an error in C99, but it is in C89)
int try(int n)
{
int i;
for(i=0;i<8;i++)
{
board[n]=1;
if(n==7&&good()=1)
{
c++;
drawboard();
return(0);
}
if(n<7&&good()=1&&try(n+1)=1)
return(1);

board[n]=0;
}
return(0);
}

void drawboard()
{
inti,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%d",board[j]);
}
printf("/n");


this is valid C, but probably not what you meant. you probably
intended to call printf("\n"); (note \ not /) from within the outer
loop.
}

int good()
{
int i,j,p,q;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
for(p=0;p<8;p++)
for(q=0;q<8;q++)
{
if(i==p||i==q||j==q||j==p||abs(i-p)==abs(j-q))
return(0);
else
return(1);
}
}

this function does nothing useful; it always returns 0.

DES
 
S

serrand

wildthings said:
i was trying to complie/run the n queens problem on bloodshod
dev-c++(version4) as a c lang
project but it gives a huge no. of errors which i cant figure out
pls help
the code is:::

#include <iostream.h>
#include <stdlib.h>
#include <windows.h>

int c=0;
int try(int);
void drawboard(void);
int board[8][8];
int good();

int main()
{
int i,j;

for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[j]=0;

try(0);
printf("%d",c);
}

int try(int n)
{
int i;
for(i=0;i<8;i++)
{
board[n]=1;
if(n==7&&good()=1)

/* perharps */
if((n==7) && (good()==1))
{
c++;
drawboard();
return(0);
}
if(n<7&&good()=1&&try(n+1)=1) /* same as below */
return(1);

board[n]=0;
}
return(0);
}

void drawboard()
{
inti,j;

/* certainly */
int i, j
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%d",board[j]);
}
printf("/n");
}

int good()
{
int i,j,p,q;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
for(p=0;p<8;p++)
for(q=0;q<8;q++)
{
if(i==p||i==q||j==q||j==p||abs(i-p)==abs(j-q))
return(0);
else
return(1);
}
}


you have some more work to do...

Xavier
 
O

osmium

wildthings said:
i was trying to complie/run the n queens problem on bloodshod
dev-c++(version4) as a c lang
project but it gives a huge no. of errors which i cant figure out
pls help
the code is:::

<snipped>

This compiles on Dev-C with a file extension of .C. You can use
control+shift+i to form meaningful indentation as I did here. Don't write
so much code before compiling on your next project. Just because the
program in its current state does nothing meaningful does not mean you can
not compile it. Syntax errors in C and C++ can be really nasty to find.
Focus on the first error, the others may well be nonsense (side effects)
created by the first error.This uses // comments which Dev-C allows. This
will look a lot nicer in a non-proportional font.

Note that try is a reserved word in C++.

I think the 8 Queens problem is normally solved with recursion. I did not
see any in your code. You may be off to a bad start, logic-wise.

//#include <iostream.h> // does snot exist in C
#include <stdlib.h>
//#include <windows.h> // not used. Its deceptive to include not used
things

int c=0;
int try(int);
void drawboard(void);
int board[8][8];
int good();
//======================
int main()
{
int i,j;

for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[j]=0;
try(0);
printf("%d",c);
system("pause"); // added to your code to see results
}
//---------------------------
int try(int n)
{
int i;
for(i=0;i<8;i++)
{
board[n]=1;
if(n==7&&good()==1) // ==
{
c++;
drawboard();
return(0);
}
if(n<7&&good()==1&&try(n+1)==1) // == twice
return(1);
board[n]=0;
}
return(0);
}
//----------------------
void drawboard()
{
int i,j; // inti is not a type
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%d",board[j]);
}
printf("/n");
}
//---------------------
// Editorial: This function has no parameters but returns a result.
// Describe here, in English, what kind of magic the function does.
// IOW, some things are good and some things are not good.
// How does this function categorize them?
int good()
{
int i,j,p,q;
// Editorial: You could probable use symmetry and change a lot of the 8s
to 4s.
for(i=0;i<8;i++)
for(j=0;j<8;j++)
for(p=0;p<8;p++)
for(q=0;q<8;q++)
{
if(i==p||i==q||j==q||j==p||abs(i-p)==abs(j-q))
return(0);
else
return(1);
}
}
 
F

Flash Gordon

Dag-Erling Smørgrav said:
this is not a valid prototype.

It's not a prototype, but it's a perfectly legal declaration. Admittedly
int good(void);
would be better style.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top