9. Car race - the solution


In order to determine which car is in the leading position after T seconds of watching the race, it is best to first determine the distance traveled by the car. Since cars move evenly, the connection S = V * T is valid. The assumption is that the cars did not squeak as soon as the start signal was given, but were delayed for some time T1 <T2 <T3 <T
So the actual travel time of the 1st car is T-T1, the second T-T2 and the third T-T3, so the roads are crossed
S1=V1*(T-T1)
S2=V2*(T-T2)
S3=V3*(T-T3)

When these values ​​are found they can be compared using the if-else if-else if command.

​Solution:​#include <stdio.h>
#include <stdlib.h>

int main()
{
double T1,T2,T3,T,V1,V2,V3;
double S1,S2,S3;
    printf("Enter the delay times of cars 1,2 and 3. Note that T1 <T2 <T3\n");
    scanf("%lf%lf%lf",&T1,&T2,&T3);
    printf("Enter car speeds 1,2 and 3 respectively. Then enter the race time we are watching\n");
    scanf("%lf%lf%lf%lf",&V1,&V2,&V3,&T);

/*Calculation of the traveled distances of the car according to the formula for uniform movement s = v * t.
    The time here represents the actual time of movement of the car, which is obtained when we subtract from the race time
     car delay time*/
    S1=V1*(T-T1);
    S2=V2*(T-T2);
    S3=V3*(T-T3);

/*Determining the position of the car after time T*/
    if(S1>S2 && S1>S3)
        printf("The first car is in the leading position");
    else if(S2>S1 && S2>S3)
        printf("The second car is in the leading position");
    else if(S3>S1 && S3>S2)
        printf("The third car is in the leading position");
    else if(S1==S2 && S1>S3)
        printf("The first and second cars are in the leading position");
    else if(S1==S3 && S1>S2)
        printf("The first and third cars are in the leading position");
    else if(S2==S3 && S2>S1)
        printf("The second and third cars are in the leading position");
    else if(S1==S2 && S1==S3)
        printf("All three cars are in the leading position");
    return 0;
}
To see more examples from the topic of branching in the program click on the image next to it additional example, selection statements