How to resolve the algorithm Define a primitive data type step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Define a primitive data type step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Define a primitive data type step by step in the UNIX Shell programming language

Source code in the unix programming language

typeset -i boundedint
function boundedint.set {
    nameref var=${.sh.name}
    if (( 1 <= .sh.value && .sh.value <= 10 )); then
        # stash the valid value as a backup, in case we need to restore it
        typeset -i var.previous_value=${.sh.value}
    else
        print -u2 "value out of bounds"
        # restore previous value
        .sh.value=${var.previous_value}
    fi
}

boundedint=-5; echo $boundedint
boundedint=5;  echo $boundedint
boundedint=15; echo $boundedint


  

You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Erlang programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Picat programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Groovy programming language