Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   An intereting rider in C (http://www.velocityreviews.com/forums/t556710-an-intereting-rider-in-c.html)

 erfan 12-02-2007 07:14 AM

An intereting rider in C

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,8),(5,6),(6,9),(6,5),(8,9),(8,5)
(9,8),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 12-02-2007 08:16 AM

Re: An intereting rider in C

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,8),(5,6),(6,9),(6,5),(8,9),(8,5)
> (9,8),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 12-02-2007 03:19 PM

Re: An intereting rider in C

In article <fitpl2\$673\$1@registered.motzarella.org>,
santosh <santosh.k83@gmail.com> 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 - dakidd@sonic.net - 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 12-02-2007 04:20 PM

Re: An intereting rider in C

On 12月2日, 下午11时19分, Don Bruder <dak...@sonic.net> wrote:
>
> Aw, quit with the critique and just do his homework for him like he