Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.


sadamissadamis Posts: 3Member
please delee this


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]You need to use arrays here. Once you read the # of trees - you need TWO arrays: one for X coordinates and one for Y coordinates. The arrays can be allocated with malloc(), like so:[/color][code]
    double* XTree = malloc (sizeof (double) * num_trees);
    double* YTree = malloc (sizeof (double) * num_trees);
    [/code][color=Blue]Then you need to read all tree coordinates in a loop:[/color][code]
    for (i=0; i<num_tree; i++)
    fscanf(fp, "%f %f", &x, &y);
    XTree [i] = x;
    YTree [i] = y;
    [/code][color=Blue]Once you did that - close the file and start your calculations. The distance between two points is calculated by following formula:[/color][code]
    double dx = x1 - x2;
    double dy = y1 - y2;
    double dist = sqrt(dx*dx + dy*dy);
    [color=Blue]Then sort trees by ascending distance and pick closest tree (first one, then next ...). You also need to check the dog distance to that tree and verify that cat gets there first. If first tree fails to save the cat - try next tree until tree arrays are over. Double speed of dog means that to save cat the distance of cat to the tree must be LESS then double dog distance to the tree. Once you find that tree - cat saves himself![/color]

  • sadamissadamis Posts: 3Member
Sign In or Register to comment.