need - Programmers Heaven

Howdy, Stranger!

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

Categories

need

sadamissadamis Posts: 3Member
please delee this

Comments

  • 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);
    [/code]
    [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
    please delete this
Sign In or Register to comment.