Problem statement
Implement the following function:
int FastestCar(int t, int di, int d2, int d3);
The function accepts four positive integers 't', 'd1', 'd2'
and 'd3' as its argument. 3 cars cover distances 'd1',
'd2' and 'd3' in time 't'. Implement the function to find
the fastest car by calculating their speed and return
the
speed of the fastest car.
Speed = Distance / Time
Answers
Answered by
6
Explanation:
Problem statement
Implement the following function:
int FastestCar(int t, int di, int d2, int d3);
The function accepts four positive integers 't', 'd1', 'd2'
and 'd3' as its argument. 3 cars cover distances 'd1',
'd2' and 'd3' in time 't'. Implement the function to find
the fastest car by calculating their speed and return
the
speed of the fastest car.
Speed = Distance / Time
Answered by
42
Flowing program of the given function is in python programming language.
Explanation:
- python programming language offers a function 'max()' that gives the maximum value from the input list. Hence, here we save the speeds of the cars in a list 's' and return 'max(s)'.
- def FastestCar( t, d1, d2, d3 ):
- s=[d1, d2, d3]
- for i in range(len(s)):
- s[i]=s[i]/t
- return max(s)
- t=input("Enter time:")
- d1=input("Enter distance covered by first car:")
- d2=input("Enter distance covered by second car:")
- d3=input("Enter distance covered by third car:")
- print("the speed of the fastest car is " ,FastestCar(t, d1, d2, d3))
Similar questions