I have an assignment until next week and i don't know hot to complete it

In the program in c we have to help a cat escape from a dog which has double speed. The program works as follows :
the data of the program are read from the file CatAndDog.dat (the first line has the cat's coordinates, the second has the dog's ones, the third has the number of the trees that the cat can climbs on (max 1000) and the next n lines has the coordinates of the trees)
For example :
2.0 2.0
1.0 1.0
4
0.0 1.0
1.5 1.5
2.5 2.9
0.0 0.5

After doing this we have to check if the cat can escape by climbing on any tree. But this is the difficult for me because if there are two or more trees we should prefer the tree that is closer to initial position of the cat. Otherwise The cat cannot esacape.

The distance between two points on a plain is the square root of the sum of the squares of the x and y distances. (Pythagorean theorem: "The square of the hypotenuse is equal to the sum of the squared of the other two sides") So you just need to calculate the distance to each of the trees in turn, saving the smallest one you've seen so far. When you've looked at all the trees, the one that is still smallest is the one to work with.

wow that's great!! but how to write this by checking each one every time?? :{

Just do it in a loop: As you read the file, check the distance from the cat to the tree at each line. (You can avoid the square root call until the last part if you wish: The square of the distance will be larger if and only if the distance is itself larger.)

I think you should also consider the fact that the dog is twice fast.

Besides the cat, you should also calculate distances of the trees from the dog.

Select only the tree for which,

(distance of dog - 2 * distance of cat) is maximum.

The dog won't be able to catch the cat.

pfff it's a tricky exercise, i don't know what to do :(

I think you should also consider the fact that the dog is twice fast.

Besides the cat, you should also calculate distances of the trees from the dog.

Select only the tree for which,

(distance of dog - 2 * distance of cat) is maximum.

The dog won't be able to catch the cat.

Good point, though you don't need a maximum, just more than zero, so you can stop looking as soon as you find that: A time savings. And you can avoid the square root entirely: You need that (2*cat_distance) < (dog_distance) We know the distances are positive, so we can square both sides of the inequality without changing it: (2*cat_distance)*(2*cat_distance) < (dog_distance)*(dog_distance) Which simplifies to 4*(cat_distance)^2 < (dog_distance)^2

int main()
{

FILE *fp;

if((fp = fopen("CatAndDog.dat","r")) == NULL)
printf("File could not be opened\n")

fscanf(fp, "%d %d", &a, &b);
fscanf(fp, "%d %d", &c, &d);
fscanf(fp, "%d", &e);
}

What i don't know is how to read the coordinations of the tree and how to find if the cat can escape or not

Based on the replies above, I think they already told you whether the cat can escape or not.

Uhmmm, is that the actual code you just posted? I'm pretty sure that will not compile.

I'm with yan0. What are a,b,c,d? Where declared? And why %d to read a floating point value from the file? Your variables should be declared something like this (feel free to steal this code if you wish)

typedef struct {
  double x;
  double y;
} Point;

Point catLocation;
Point dogLocation;
Point currentTreeLocation;
int numberOfTrees;
int currentTreeNumber;

You are not going to be able to finish this assignment with the amount of C knowledge that you are showing us. Please get local help for yourself: A tutor, or see your professor or teaching assistant and talk about what you can do to catch up.

the a,b,c,d,e are the coordinators of the cat, dog and trees. i also have done a loop to read all the coodrinators of the trees. What i don't know how to do is how to check if the cat can escape or not and also how to pick the closest tree to the cat if there are two or more.

Just as previously stated, there are many ways to do that. I think what you should work out is how to understand your problem.

Since you know the coordinates of the cat and the dog and the trees, all you have to do is decide which tree cat should climb. It really doesn't matter whether it's the closest tree or not (it might be that the dog is behind the closest tree). What matters is selecting the "first" safe tree.

To better visualize a solution, plot the trees in a sheet of paper (like a map), and divide the paper horizontally and vertically. Place the dog anywhere, and place the cat in the center.

Now ask yourself:
[] Given the current setup, which part of the paper (with a tree of course) would you want the cat to run? Why?

[] If you move the cat to another place in the map (with the trees and the dog in the same place), would you choose the same tree? Why? If not, Which tree would you choose and why?

[] If you can answer the questions above, you should be able to create a general solution to your problem. You should also have realized by now that there are cases in which the cat is totally screwed (sorry cat). :p

does anyone know how am i able to pick up the closest tree to the cat and show me only that tree??

Yes. You will too, after you go back and carefully read the posts that are already in this thread. I will summarize:

  1. Open the file for reading
  2. Read in the cat location
  3. Read in the dog location
  4. Read in the number of trees
  5. Mark a "lose" condition as default
  6. In a loop, read in each tree location and...
    1. calculate the distance from that tree to the dog
    2. calculate the distance from that tree to the cat
    3. calculate whether the cat is close enough to that tree to "win"
    4. (If you need to keep track: If this is the 'best tree yet', save it for later)
    5. If the cat "wins" mark a "win" condition and break the loop
  7. // This is the end of the loop
  8. Close the file
  9. If the cat did "win" print out whatever a "win" message is
  10. Else the cat did not "win": Print out whatever a "lose" message is
  11. End the program

int main()
{
FILE *fp;
if((fp = fopen("CatAndDog.dat","r")) == NULL)
printf("File could not be opened\n")

fscanf(fp, "%f %f", &X1, &Y1);
fscanf(fp, "%f %f", &X2, &Y2);
fscanf(fp, "%d", &num_of_trees);

for (i=0; i<num_of_trees; i++)
{
fscanf(fp, "%f %f", &x, &y);
XTree = x;
YTree = y;
}
fclose(fp)

for (i=0; i<number_of_trees; i++)
{
double dx1 = X1 - x;
double dy1 = Y1 - y;
double dist1 = sqrt(dx*dx + dy*dy);
}
for (j=0; j<number_of_trees; j++)
{
double dx2 = X2 - x;
double dy2 = Y2 - y;
double dist2 = sqrt(dx*dx + dy*dy);
}
Is it ok so far?

Except for the missing declarations and the too-short names, it seems fine so far. "X2" should be spelled "dogX" or something similar, for one example. Please use the (CODE) button when you post code: It shows line numbers, keeps indentation and does code coloring: All good things.

int main()
{
FILE *fp;
if((fp = fopen("CatAndDog.dat","r")) == NULL)
printf("File could not be opened\n")

fscanf(fp, "%f %f", &X1, &Y1);
fscanf(fp, "%f %f", &X2, &Y2);
fscanf(fp, "%d", &num_of_trees);

for (i=0; i<num_of_trees; i++)
{
fscanf(fp, "%f %f", &x, &y);
XTree [i] = x;
YTree [i] = y;
}
fclose(fp)

for (i=0; i<number_of_trees; i++)
{
double dx1 = X1 - x[i];
double dy1 = Y1 - y[i];
double dist1 = sqrt(dx*dx + dy*dy);
}
for (j=0; j<number_of_trees; j++)
{
double dx2 = X2 - x[i];
double dy2 = Y2 - y[i];
double dist2 = sqrt(dx*dx + dy*dy);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.