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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <iostream>
#include <fstream>

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<second_car)
    { 
        
        time2++;
     }

     
     //cout <<"Pirmas su antru susitiks po "<<laikas<<" valandu, o pirmas pavys antra po "<<laikas2<<" valandu."<<endl;
     cout <<" "<<time<<"  "<<time2<<" "<<endl;



}

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.

1
2
3
4
5
while(first_car<second_car)
{ 

time2++;
}

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:
1
2
3
4
5
6
7
8
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.