Thread: The cats, dogs and trees game

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    The cats, dogs and trees game

    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.

    So far i have done this :
    Code:
    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);
    }
    }
    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Wow, what went wrong with your last thread from a week ago (also initially posted to the wrong forum*) where you asked exactly the same question and got 3 pages of help?

    dogs chasing cats up trees

    If you have not gotten the idea yet: WE DON'T DO YOUR HOMEWORK FOR YOU.

    * hint: C != C# != C++
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Sorted out now.
    For back history of this problem, read dogs chasing cats up trees

    @sadam

    fscanf(fp, "%f %f", &X1, &Y1);
    fscanf(fp, "%f %f", &X2, &Y2);
    How about more meaningful variable names like

    fscanf(fp, "%f %f", &catX, &catY);
    fscanf(fp, "%f %f", &dogX, &dogY);
    Last edited by Salem; 03-28-2011 at 12:51 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dogs chasing cats up trees
    By sadam in forum C Programming
    Replies: 40
    Last Post: 03-28-2011, 12:46 AM