Velocity Reviews > An intereting rider in C

An intereting rider in C

erfan
Guest
Posts: n/a

 12-02-2007
A rider who is on a horse cruse around.the array is 15*15,and he was
located in (7,7).His horse only can go in "/"or"\". eg.when he was
in (7,7),next,he may go (5,,(5,6),(6,9),(6,5),(8,9),(8,5)
(9,,or(9,6) . IF he has three steps,please caculate the location he
would be .
here is my programn,i imitated the 8 Quessn `s way,however,nothing
gain. I am puzzled by the Return function here and there,where should
i straighten it up \$B!)(B

#define MAX 3
static int array[15][15];
static int x,y;
static int a,b;
static int x0=7;
static int y0=7;
int g,h;

/*
void showchess()
{
//printf("%d\n",array[a][b]);

for(int i=0;i<15;i++)
{ for(int j=0;j<15;j++)
{
printf("%3d",array[i][j]);
}
printf("\n");
}
}
*/

int check(int a,int b,int c) /* c is the number of step*/
{
int x,y;
for(x=a-2;x<=a+2;x++)
for(y=b-2;y<=b+2;y++)
{
if ((x-a)*(x-a)+(y-b)*(y-b)==5)
{
c=c+1;
printf(" x=%d,y=%d\n",x,y);
g=x;
h=y;
return 1;
}

}
return 0;
}

void putchess(int x0,int y0,int n)
{
int z;
array[x0][x0]=1;
if(check(x0,y0,n+1)==1)
{
if(n==MAX-1)
printf("ok");
//showchess();
else
putchess(g,h,n+1);

}

}

int main()
{
putchess(x0,y0,0);
return 1;
}

santosh
Guest
Posts: n/a

 12-02-2007
erfan wrote:

> A rider who is on a horse cruse around.the array is 15*15,and he was
> located in (7,7).His horse only can go in "/"or"\". eg.when he was
> in (7,7),next,he may go (5,,(5,6),(6,9),(6,5),(8,9),(8,5)
> (9,,or(9,6) . IF he has three steps,please caculate the location he
> would be .

Three steps in which direction? All combinations of three steps could
place the horse at several places on the board.

> here is my programn,i imitated the 8 Quessn `s way,however,nothing
> gain. I am puzzled by the Return function here and there,where should
> i straighten it up ?
>
> #define MAX 3
> static int array[15][15];
> static int x,y;
> static int a,b;
> static int x0=7;
> static int y0=7;
> int g,h;

Instead of so many file scope objects you should probably aim to
encapsulate most of them into appropriate functions and pass them
around as arguments if necessary. File scope and program scope objects
are just invitations for inadvertent modifications and unnecessary
tie-ups between functions.

Also you should give your variables more descriptive names, particularly
those have a wide scope. It's probably okay for a short duration
counter to be named 'x' or 'y', but such names are terrible for file
scope and function scope objects.

> /*
> void showchess()
> {
> //printf("%d\n",array[a][b]);
> for(int i=0;i<15;i++)
> { for(int j=0;j<15;j++)
> {
> printf("%3d",array[i][j]);
> }
> printf("\n");
> }
> }
> */
>
> int check(int a,int b,int c) /* c is the number of step*/

Are you aware that within this function it's arguments 'a'
and 'b' "shadow" the similarly named file scope objects? Are you sure
that this is what you want? Usually this is an iffy idea.

> {
> int x,y;

Ditto.

> for(x=a-2;x<=a+2;x++)

What's wrong with placing braces around statement blocks for clarity?
Does your instructor particularly favour indecipherable code?

> for(y=b-2;y<=b+2;y++)
> {
> if ((x-a)*(x-a)+(y-b)*(y-b)==5)

Are you sure about all these operations? Why not place a printf()
statement here to dump the values of 'x', 'y', 'a' and 'b' after each
iteration, to check that there is no spurious modification?

> {
> c=c+1;

This seems to achieve nothing. The scope of 'c' is for this function and
after you return below, it is destroyed. Nor do you use this anywhere
else in this function. So what exactly is the purpose of the increment
above?

> printf(" x=%d,y=%d\n",x,y);
> g=x;
> h=y;
> return 1;
> }
> }
> return 0;
> }
>
> void putchess(int x0,int y0,int n)

Again local objects hiding file scope ones.

> {
> int z;

You don't seem to use this anywhere...

> array[x0][x0]=1;
> if(check(x0,y0,n+1)==1)
> {
> if(n==MAX-1)
> printf("ok");

Place a newline to output the string immediately.

> //showchess();
> else
> putchess(g,h,n+1);
>
> }
> }
>
> int main()
> {
> putchess(x0,y0,0);
> return 1;

One is not a portable return value. Portable values are 0, EXIT_SUCCESS
and EXIT_FAILURE. The two macros are defined in stdlib.h and 0 and
EXIT_SUCCESS mean essentially the same thing.

> }

Finally you need to include the stdio.h header for your printf() calls.
Otherwise the code invokes undefined behaviour.

The way your are attacking your problems seems unnecessarily complex to
me. I would first get rid of all unnecessary file scope objects and
make them function scope. File scope objects increase the chances of
inadvertent interactions exponentially. I would also place printf()
calls at strategic places along with perhaps assert() invocations to
check for basic sanity.

Don Bruder
Guest
Posts: n/a

 12-02-2007
In article <fitpl2\$673\$(E-Mail Removed)>,
santosh <(E-Mail Removed)> wrote:

<snippety-snippety-snip>

> The way your are attacking your problems seems unnecessarily complex to
> me. I would first get rid of all unnecessary file scope objects and
> make them function scope. File scope objects increase the chances of
> inadvertent interactions exponentially. I would also place printf()
> calls at strategic places along with perhaps assert() invocations to
> check for basic sanity.

Aw, quit with the critique and just do his homework for him like he

--
Don Bruder - http://www.velocityreviews.com/forums/(E-Mail Removed) - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info

erfan
Guest
Posts: n/a

 12-02-2007
On 12月2日, 下午11时19分, Don Bruder <(E-Mail Removed)> wrote:
>
> Aw, quit with the critique and just do his homework for him like he
>
>

wow,thank you Santosh,and also Don.
that is not my homework,though i am not a good at C,i still like
it,use it to deal with some problems. stupid is as stupid does,i
desire your critique ,and most importantly,our discussion~