### Ta

Hi guys, today I got program for homework to write and I need help with it.
I will try to explain what it asks as simple as possible.
We got two cars. First car is in city A, second is in city B. Distance between cities is 900km. First car goes at speed of 75km/h. Second car goes at speed of 60km/h.
First thing we have to do is calculate how much time it will take for cars to reach each other when cars are going to opposite cities(First car to city B and second car to city A). I done this but I included this too in case it has to do something with upcoming task part.
Ok, so second part of this task. We have same cars, same cities, same numbers in the original state. We have to calculate how much time it would take for first car to reach second one if both are going same direction.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` `````` #include #include using namespace std; int main() { int first_car,second_car,distance,time=0,time2=0,distance2; fstream fd ("/Volumes/UDISK/duomenu_nuskaitymas_is_failo/duomenu_nuskaitymas_is_failo_2/duom2.txt"); fd >> first_car; fd >> second_car; fd >> distance; distance2=distance; while (distance>0) { distance=distance-(first_car+second_car); time++; } while(first_car

I have problems doing second while loop to calculate number of hours. Help is much appreciated.
P.S Please don't pay attention to comment it's written in native language. I had to translate code to English so you guys can understand.
Last edited on
I sort of see where you're trying to go with this, you're iterating with time incrementing 1 hour at a time. However, before you do it this way again, I would suggest that you can do it without using a while loop.

 ``12345`` ``````while(first_car

This is an infinite loop. time2 will keep being incremented, but first_car and second_car are never going to be change.

My question is, do you need to use while loops? This seems more like a physics problem than a programming problem. In which case, I would prefer not to use while loops for solutions that can expressed in closed form.

Let's do the problem where they are going the same direction:
 ``12345678`` ``````int A_position = 0; int B_position = 900; int car1_position_initial = position_A; int car2_position_initial = position_B; int car1_speed = 75; // A towards B, @ 75 km/h int car2_speed = 60; // same direction, @ 60 km/h ``````

Linear Motion states that:
position = speed * time
or, equivalently, position_final = speed * time + position_initial

You have two different speeds (but same direction), and two different starting positions.
You must set the final positions equal to each other.

Your equation for Car 1 is:
position_final = 75 * time + 0
Your equation for Car 2 is:
position_final = 60 * time + 900

Set these equations equal to each other and solve for time:
75 * time + 0 = 60 * time + 900
(75 - 60) * time = 900 - 0
time = (900 - 0) / (75 - 60) = 60 hours.

The cars will meet after 60 hours.

Now you just need to plug in the appropriate variables for 75 km/h, 60 km/h, 0 km, and 900 km into your program.
Last edited on
Topic archived. No new replies allowed.