How to resolve the algorithm Old Russian measure of length step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Old Russian measure of length step by step in the AWK programming language
Table of Contents
Problem Statement
Write a program to perform a conversion of the old Russian measures of length to the metric system (and vice versa).
It is an example of a linear transformation of several variables.
The program should accept a single value in a selected unit of measurement, and convert and return it to the other units: vershoks, arshins, sazhens, versts, meters, centimeters and kilometers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Old Russian measure of length step by step in the AWK programming language
Source code in the awk programming language
# syntax: GAWK -f OLD_RUSSIAN_MEASURE_OF_LENGTH.AWK
BEGIN {
units = "kilometer meter centimeter tochka liniya diuym vershok piad fut arshin sazhen versta milia"
values = "1000.0 1.0 0.01 0.000254 0.00254 0.0254 0.04445 0.1778 0.3048 0.7112 2.1336 1066.8 7467.6"
u_leng = split(units,u_arr," ")
v_leng = split(values,v_arr," ")
if (u_leng != v_leng) {
print("error: lengths of units & values are unequal")
exit(1)
}
print("enter length & measure or C/R to exit")
}
{ if ($0 == "") {
exit(0)
}
measure = tolower($2)
sub(/s$/,"",measure)
for (i=1; i<=u_leng; i++) {
if (u_arr[i] == measure) {
for (j=1; j<=u_leng; j++) {
str = sprintf("%.8f",$1*v_arr[i]/v_arr[j])
sub(/0+$/,"",str)
printf("%10s %s\n",u_arr[j],str)
}
print("")
next
}
}
printf("error: invalid measure; choose from: %s\n\n",units)
}
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the Action! programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the Lua programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Frink programming language
You may also check:How to resolve the algorithm Quine step by step in the Ol programming language
You may also check:How to resolve the algorithm Empty string step by step in the min programming language