That's not what you want to do - yes, it can be smartly optimized, but that's a higher level of understanding of the math.
And trust me, you're not there.
Is this still your program?
Code:
int main()
{
FILE *fp;
if((fp = fopen("CatAndDog.dat","r")) == NULL)
printf("File could not be opened\n")
fscanf(fp, "%f %f", &cat_X_coord, &cat_Y_coord);
fscanf(fp, "%f %f", &dog_X_coord, &dog_Y_coord);
fscanf(fp, "%d", &number_of_trees);
for (i=0; i<=number_of_trees; i++)
{
fscanf(fp, "%f %f", &x, &y);
XTree [i] = x;
YTree [i] = y;
}
fclose(fp)
typedef struct {
double x;
double y;
} Point;
Point catLocation;
Point dogLocation;
Point currentTreeLocation;
int numberOfTrees;
int currentTreeNumber;
let's give it a few tweaks. Same input data from #1 post:
2.0 2.0 <== cat
1.0 1.0 <== dog
4 <== trees
0.0 1.0
1.5 1.5
2.5 2.9
0.0 0.5
Code:
//888888888888888888888888888888888888888888
#include <stdio.h>
#include <math.h>
#define MAX 10
typedef struct {
double x;
double y;
} Point;
int main() {
int i;
double distance = 0.0;
FILE *fp;
Point cat;
Point dog;
Point tree;
Point trees[MAX];
int numberOfTrees;
if((fp = fopen("CatNdog.dat","r")) == NULL) {
printf("File could not be opened\n")
return 0;
}
fscanf(fp, "%f %f", &cat.x, &cat.y);
fscanf(fp, "%f %f", &dog.x, &dog.y);
fscanf(fp, "%d", &number_of_trees);
printf("\n\n");
for (i=0; i<=number_of_trees; i++)
{
fscanf(fp, "%f %f", &trees[i].x, &trees[i].y);
printf("%lf, %lf\n", trees[i].x, trees[i].y); //check input
//calculate part will be called or done, here.
}
fclose(fp)
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
That's a bit more how I think it should be approached in C. Study that. If you have any code to calculate the distance from cat to tree, post it up.