How to resolve the algorithm Map range step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Map range step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

Given two ranges:   where:

Write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map values from the range   [0, 10]   to the range   [-1, 0].

Show additional idiomatic ways of performing the mapping, using tools available to the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Map range step by step in the ALGOL 68 programming language

Source code in the algol programming language

# maps a real s in the range [ a1, a2 ] to the range [ b1, b2 ]           #
# there are no checks that s is in the range or that the ranges are valid #
PROC map range = ( REAL s, a1, a2, b1, b2 )REAL:
    b1 + ( ( s - a1 ) * ( b2 - b1 ) ) / ( a2 - a1 );

# test the mapping #
FOR i FROM 0 TO 10 DO
    print( ( whole( i, -2 ), " maps to ", fixed( map range( i, 0, 10, -1, 0 ), -8, 2 ), newline ) )
OD

  

You may also check:How to resolve the algorithm String case step by step in the Swift programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Pierpont primes step by step in the Factor programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Oz programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Fe programming language