How to resolve the algorithm Old Russian measure of length step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Old Russian measure of length step by step in the J 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 J programming language

Source code in the j programming language

#!/usr/bin/ijconsole
UNIT2MULT=: |:_2 (; ".)&;/\;:'arshin 0.7112 centimeter 0.01 diuym 0.0254 fut 0.3048 kilometer 1000.0 liniya 0.00254 meter 1.0 milia 7467.6 piad 0.1778 sazhen 2.1336 tochka 0.000254 vershok 0.04445 versta 1066.8'

conv=: UNIT2MULT 1 : 0 
 if. 2 ~: # y do. 'ERROR. Need two arguments - number then units'
 else.
  'VALUE UNIT'=. (;~ _&".)&;~/y
  if. _ = VALUE do. 'ERROR. First argument must be a (float) number'
  else.
   try. 
     scale=. ({::~ i.&(<UNIT))~/m
     ((":;:inv y),' to:'),":({.m),.({:m)*L:0 VALUE%scale
   catch.
     'ERROR. Only know the following units: ',;:inv{.m
   end.
  end.
 end.
)

NB. for use from os command line only:
exit echo conv }.ARGV


  

You may also check:How to resolve the algorithm Write entire file step by step in the Tcl programming language
You may also check:How to resolve the algorithm Send email step by step in the Java programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Erlang programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the Haskell programming language