Howdy, Stranger!

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



sadamissadamis Member Posts: 3
please delee this


  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [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 Member Posts: 3
    please delete this
Sign In or Register to comment.