I have an object array with 4 objects. Each of these objects is assigned x and y coordinates. When the user inputs their own coordinates the program finds the closest object. Then the program needs to update that object with the income. This is 12*distance. I am struggling with how to get the program to add the income to the correct object.
The program is for a towtruck company. The customer enters their coordinates and the program finds the closest truck to send out. Then the revenue for the truck is calculated by doing 12*distance. This needs to be added to the truck that was closest. The program keeps running until the user hits 0. Then a summary for each of the 4 trucks is printed out with the name of the driver and how much money they made that day.
closest = -1; //-1 is an invalid distance
int dx = -1; //invalid array index for this code. you can use negative index, but you need special setup to do that.
for(i = 0; i < 4; i++)
{
d = distance_to_truck(truck[i]); //assuming you know how to get this value?
if (d > closest)
{
closest = d; //save the distance to the current closest truck
dx = i; //save the index of the current closest truck
}
}
//truck[dx] is the closest, if dx != -1
truck[dx] += 12*something;