Well, I've been working on a project that can store coordinate points in an array. Unfortunately, I'm not a very good coder, so this is the best I could come up with with my limited knowledge. I have gotten an error when I try to print this, saying that "no operator "<<" matches these operands on the line cout << pnt[30] << " ";. If anybody could help, that would be great :).
Oh thank you! I can't believe I didn't think of that! But I'm having one more error- it displays the same random number for all the point. How can I make it so that it displays random numbers for all the points?
The lines that contain pnt[30] are incorrect. The array is of 30 elements, so this means the elements go from index 0 to index 29. Besides, you probably want pnt[n].
Yes. I suppose that you would define "closest" as the point closest according to Pythagoras. So just run through the array of points calculating pythagoras between the point of interest and the point in turn in the array. Then just settle for the point that yields the lowest distance.
hehe, you are making a big mess. To determine the minimum (or maximum) from a group of items is rather simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
double minDistance; //You need a double for the distance because the rounding to integer can make you err the choice.
int minIndex = -1;
//The fancy way of initializing minDistance is to set it to the highest possible number (#include <limits>):
minDistance = std::numeric_limits<double>::max;
//Now just loop through the array of points.
double distance = 0;
for (int n = 0; n < 30; n++)
{
distance = DistanceFunction(pnt[n], MyCharacter);
if (distance < minDistance)
{
minDistance = distance;
minIndex = n;
}
}
//At this point you know that minIndex is the index of the point nearest to MyCharacter.