473,382 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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;
}
Dec 2 '07 #1
3 1402
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.

Dec 2 '07 #2
In article <fi**********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:

<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
asked! :)

--
Don Bruder - da****@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/~dakiddfor more info
Dec 2 '07 #3
On 12ÔÂ2ÈÕ, ÏÂÎç11ʱ19·Ö, Don Bruder <dak...@sonic.netwrote:
>
Aw, quit with the critique and just do his homework for him like he
asked! :)

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~
Dec 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim:. | last post by:
Hi Can someone tell me how I generate a new XML file that I can then use with an XSLT file. I am trying to use an XML file generated from Microsoft Project or even better generate an XML file from...
6
by: Milo Woodward | last post by:
Does anyone know how to retrieve the number of records an XML file contains with a vb.net method? I can read through an entire XML file, import records into SQL Server, etc. However, I don't want...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
3
by: Cheryl | last post by:
I need a sample that shows how can loop through the attributes and display the data.(.net / c#) <?xml version="1.0" encoding="UTF-8"?> <riders> <rider> <prename id=>Lance</prename>...
3
by: Ram | last post by:
Hi, I am trying to set up a DB for a race series where a riders best 6 of 10 rides count towards a league position. I have a table of Riders ( say 300 ) I want to use a form to select which...
3
by: Jenny | last post by:
Hi all, I'm using a second page to write dynamical generated images into the outputstream. This avoids using tmp-files on disc. My code-behind in the start aspx file is: 'Use second file to...
1
by: jpatchak | last post by:
Hi Guys, Sorry if this is a really stupid question. I am trying to upsize my Access database to SQL server. When I used the Access upsizing wizard, some of my queries didn't get upsized so I am...
1
G Jones
by: G Jones | last post by:
For the life of me I can't figure out this problem. I get this Microsoft VBScript compilation error '800a0400' on the form processor and everything looks fine. Maybe you someone can point out my...
3
by: Hank stalica | last post by:
So I have this class with a private data member: const Floor* const destFloor; I need to initialize f, which I'm trying to do in the constructor initializer list: Rider::Rider(const...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.