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
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.

So far i have done this :

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;

 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);

Can anyone finish this for me? i don't know how to found if the cat escapes or not and then printing the tree that the cat can escapes if climbs on it.

First off before you actually code this, you need to think about the maths that is involved. Let us consider it. Using "pen and paper" first, is often the quickest way to write a computer program (and it also gives you all the test cases quickly).

Consider the cat is at a point A, (Ax,Ay) and the Dog is at B, (Bx,By) and there is
one and only one tree at T(Tx,Ty). Additionally, the Dog is V times faster than the cat (note, that V could be 1.0 if they are evenly matched or it could be 0.5 if the dog is getting too old for this sort of thing).

That is the simplest problem. If you can solve this, then it is easy to do the other case, just test to see if the cat wins for the first tree, and then for the next.

The only part of the problem you haven't given us is the Dog strategy, I assume that the Dog is smart and (a) runs towards the "optimal" tree, (b) does not in doing so create an escape path for the cat to another tree.

Then the algorithm is simply the dog wins if

length_of(T-B) / V    <   length_of(T-A)

So you need to code several pieces of information,

(i) to subtract two points
(ii) calculate the length of a vector (use Pythagoras / dot product etc)
(iii) test the condition.
(iv) Encapsulate it in a loop for each tree.

That really says that you want three functions , vectorDiff, vectorLen and isDogWinner.

If you get there, then you will have little difficulty finishing this assignment.

This article has been dead for over six months. Start a new discussion instead.