Building speed formula for different units of measurement – CSKC
Sign In

Search

Code Learning #CPP#1822    siteicon   siteicon  

Building speed formula for different units of measurement

Formula building can be done for different units using different divisors.

Learning Objectives

  • Making simple formulas.
  • Reuse of left hand side variable for recalculation with new units in a formula.

Source Code

TC++ #1822

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

s=d/t;
The variables d and t were taken from the user with a suitable prompt to give them in meters and seconds. This formula will thus give speed filled in variable s in m/s

cout<<“Speed (in m/s) = “<<s<<endl;
This will print the speed in m/s as deduced by the formulas above.

s=(d/1000)/(t/3600);
Since the s above has already been printed for value in m/s we can again build a new formula for km/hr speed and fill its value in s. For km/hr formula we simply divide meter value in d with 1000 as 1 km=1000 m and t with 3600 as 1 hour = 3600 seconds. For this division we do it in separate brackets so that precedence of operator rules are properly followed.

cout<<“Speed (in km/hr) = “<<s<<endl;
With this we print the speed in km/hr as the s will now contain value based on recalculation based on new formula.

Notes

  • Reuse of variables should normally be done only when it is being used for similar purposes.
  • Reuse of variables may be avoided if the use of variables is required for displaying all values together.


Suggested Filename(s): speed.cpp, speed-in-two-units.cpp



Share

Created: 20-Dec-2017 02:35 | Updated: 15-Sep-2018 10:44|