answersLogoWhite

0


Best Answer

To calculate speed you need to know the distance travelled and the time taken to travel that distance such that Speed = Distance / Time. The result of this calculation gives the average speed without taking into account any periods of acceleration or deceleration. We can implement this using just one function:

long double speed (const long double distance, const long double duration) {

return distance / time; }

Note that we use the term "duration" rather than "time" purely to aid readability; "time" implies a specific moment in time rather than a duration.

We use a long double to cater for the widest possible range of values. However, if we want to ensure the most efficient calculation over a wide variety of numeric types, we would use a function template instead:

template<typename T>

T speed (const T& distance, const T& duration) {

return distance / duration;

}

The distance and duration variables will typically be taken from input:

int main () {

double distance, duration;

std::cout << "Enter the distance and duration: "

std::cin >> distance >> duration;

std::cout << "Speed = " << speed (distance, duration) << '\n';

}

Note that the actual units of measurement are not important to the function because speed is denoted by the given distance per given time unit. That is, if we travel 100 meters in 10 seconds, then the speed is 10 meters per second, whereas if we travel 100 miles in 10 minutes, then we are travelling at 10 miles per minute. Thus when distance is 100 and duration is 10, the speed is 10. It is up to the caller to convert these units to something more meaningful:

int main () { double distance, duration; std::cout << "Enter the distance in kilometers: ";

std::cin >> distance;

std::cout << "Enter the duration in hours: ";

std::cin >> duration;

std::cout << "Speed = " << speed (distance, duration) << " kilometers per hour\n";

}

Note that the examples do not perform any error-checking upon the input. In production code when entering numbers from input you will typically input strings instead. You will then test the strings are valid before typecasting the strings to a numeric format such as double.

User Avatar

Wiki User

āˆ™ 8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make a speed calculator in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the Example Output of Calculator Program in C plus plus?

example output of c++ calculator


How do you make a scientific calculator in c plus plus?

#include &lt;stdio.h&gt; int main() { printf("the answer is 42"); }


How do you make trigonometric calculator in c plus plus?

include file math.h and you get most C++ functions for trigonometry, for instance,sin(), cos(), tan(), asin(), acos(), and atan() etc.


Term paper for scientific calculator in c plus plus?

You need to write your own term paper...


What is 58 plus 44?

58 + 44 = 102 c: &hearts; (You can use a calculator)


Can you make a website with C plus plus?

You cannot make a website with C++.


What is the speed of sound in air at 42.0 C?

At 14 &deg;C the speed of sound is 339.8 m/s. Here is an easy calculator to di that: http://www.sengpielaudio.com/calculator-speedsound.htm Cheers ebs


Can you make programs of C in visual c plus plus?

Yes.


What is the affective topic for minor project in C plus plus?

A one dimensional game can be one like casino or something like that. You can create program to simulate a battle between humans and zombies. Also You can make a advance calculator.


As air becomes warmer how does the speed of sound in air change?

If the temperature goes down, the speed of sound goes also down. Speed of sound in air is c &asymp; 331 + 0.6 &times; T. T = Temperature in &deg;C. Speed of sound in air at 20&deg;C is c &asymp; 331 + 0.6 &times; 20 = 343 m/s.


Make programme for multification of three no in c plus plus?

result = a * b * c;


When will you make a function inline in c plus plus?

yes,we can make function inline