Variable not storing as requested
Here is my code:
point * findLongPaths(point * points, double threshold_distance) {
unsigned int i = 0;
int locationToStore = 0;
int pointsAboveThreshold = countPointsAboveThreshold(points, threshold_distance);
//int totalPoint = totalPoints(points);
point * pointsByThreshold = new point[pointsAboveThreshold];
pointValues * pointsToCalculate = new pointValues[pointsAboveThreshold];
//pointValues pointsToCalculate[pointsAboveThreshold];
//point orderedPoints[pointsAboveThreshold];
while (points[i].end != true && i < pointsAboveThreshold) {
point pointOne = points[i];
point pointTwo = points[i + 1];
//Check to see if the distance is greater than the threshold, if it is store in an array of pointValues
double distance = distanceBetweenTwoPoints(pointOne, pointTwo);
if (distance > threshold_distance) {
pointsToCalculate[i].originalLocation = i;
pointsToCalculate[i].distance = distance;
pointsToCalculate[i].final = pointTwo;
pointsToCalculate[i].stored = false;
//If the final point has been calculated, break the loop
if (pointTwo.end == true) {
pointsToCalculate[i].end = true;
break;
} else {
pointsToCalculate[i].end = false;
i++;
continue;
}
}
}
if (points[0].end == true && pointsAboveThreshold == 0) {
point emptyPoint;
emptyPoint.x = 0.0;
emptyPoint.y = 0.0;
emptyPoint.end = true;
pointsByThreshold[0] = emptyPoint;
return pointsByThreshold;
}
//Find the point with the lowest distance
i = 1;
//double lowest = 0.0;
//EDITED
pointValues pointWithLowest;
pointWithLowest = pointsToCalculate[0];
while (pointsToCalculate[i].end != true) {
for (int j = 0; pointsToCalculate[j].end != true; j++) {
if (pointsToCalculate[j].stored == true) {
j++;
continue;
} else {
if (pointsToCalculate[j].distance < pointWithLowest.distance) {
pointWithLowest = pointsToCalculate[j];
j++;
continue;
} else if (pointsToCalculate[j].distance == pointWithLowest.distance) {
if (pointWithLowest.originalLocation > pointsToCalculate[j].originalLocation) {
pointWithLowest = pointsToCalculate[j];
j++;
continue;
}
} else {
pointWithLowest.stored = true;
pointsByThreshold[locationToStore] = pointWithLowest.final;
locationToStore++;
break;
}
}
}
i++;
}
delete[] pointsToCalculate;
return pointsByThreshold;
}
For some weird reason when I go to store i
in the line pointsToCalculate[i].originalLocation = i;
, it's always storing as 0. I have ran breakpoints over it and it shows that the va开发者_运维问答lue of i
was incremented in the while loop but it's still storing originalLocation
to 0. When I check the values in runtime, it shows that the i
in pointsToCalculate[i] is
1or
2, depends on how many times I have ran through the loop and it also shows that
= i;is also
1or
2` depending on the loop.
Anyone know why this is? It's for an assignment that's due in a few hours and I've been working on it for a very long time. Still, just can't figure it out.
Thanks, Brandon
if distance <= threshold_distance
,the i
won't increment, and while loop will loop forerver
精彩评论