Distance Conversion Algorithm
-
Upload
rscm752292 -
Category
Documents
-
view
219 -
download
0
Transcript of Distance Conversion Algorithm
-
7/26/2019 Distance Conversion Algorithm
1/3
Case Study: Converting Miles to Kilometers
PROBLEM
Your summer surveying job requires you to study some maps that gives distances in
kilometers and some that use miles. You and your coworkers prefer to deal in metric
measurements. Write a program that performs the necessary conversion.
ANALYSIS
The first step in solving this problem is to determine what you are asked to do. You must
convert from one system of measurement to another, but are you supposed to convertfrom kilometers to miles, or vice versa? The problem states that you prefer to deal in
metric measurements, so you must convert distance measurements in miles to kilometers.
Therefore, the problem input is distance in miles and the problem output is distance inkilometers. To write the program, you need to know the relationship between miles and
kilometers. onsulting a metric table shows that one mile equals !."#$ kilometers.
The data requirements and relevant formulas are listed below. milesidentifies the
memory cell that will contain the problem input and kmsidentifies the memory cell thatwill contain the program result, or the problem output.
ata Re!uirements
Pro"lem In#ut
miles % the distance in miles
Pro"lem Out#ut
kms % the distance in kilometers
Relevant $ormula1 mile = 1.609 kilometers
-
7/26/2019 Distance Conversion Algorithm
2/3
ESI%N
&e't, formulate the algorithm that solves the problem. (n this case, flowchart is being
used as the algorithm to solve the problem.
Read Distance_in_Miles
Convert the distance to kilometers
Distance_in_KM =
Distance_in_Miles * 1.609
Begin
Disla! Distance_in_KM
"nd
IMPLEMEN&A&ION
To implement the solution, you must write the algorithm as a python program. To do this,
you must first tell the python compiler about the problem data requirements ) that is,what memory cell names you are using and what kind of data will be stored in each
memory cell. &e't, convert each algorithm step into one or more python statements. (f an
algorithm step has been refined, you must convert the refinements, not the original step,into python statements.
*igure below shows the python program along with a sample e'ecution. *or easy
identification, the program statements corresponding to algorithm steps are in color as isthe input data typed in by the program user.
-
7/26/2019 Distance Conversion Algorithm
3/3
$igure Miles'to'Kilometers Conversion Program
% onverts distance in miles to kilometers
% onversion constant
+-/01(20 3 !."#$
% (nput distance in miles
miles 3 #
% 4utput distance in kilometers
kms 3 #
% 5et the distance in milesprint 60nter the distance in miles6,
miles 3 input76869
% onvert the distance to kilometerskms 3 +-/01(20 : miles
% ;isplay the distance in kilometers
print 6That equals 6, kms
Sam#le Run
0nter the distance in miles 8!#.##
That equals !".#$
&ES&IN%