## C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only

need Posted by sadamis on 23 Mar 2011 at 4:27 PM
Re: need help!!! Posted by AsmGuru62 on 24 Mar 2011 at 4:51 AM
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:
```double* XTree = malloc (sizeof (double) * num_trees);
double* YTree = malloc (sizeof (double) * num_trees);
```
Then you need to read all tree coordinates in a loop:
```for (i=0; i<num_tree; i++)
{
fscanf(fp, "%f %f", &x, &y);
XTree [i] = x;
YTree [i] = y;
}
```
Once you did that - close the file and start your calculations. The distance between two points is calculated by following formula:
```double dx = x1 - x2;
double dy = y1 - y2;
double dist = sqrt(dx*dx + dy*dy);
```

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!

Re: need help!!! Posted by sadamis on 24 Mar 2011 at 7:23 AM

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic