How to resolve the algorithm Sort three variables step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort three variables step by step in the CLU programming language

Table of Contents

Problem Statement

Sort   (the values of)   three variables   (X,   Y,   and   Z)   that contain any value   (numbers and/or literals). If that isn't possible in your language, then just sort numbers   (and note if they can be floating point, integer, or other). I.E.:   (for the three variables   x,   y,   and   z),   where: After sorting, the three variables would hold: For numeric value sorting, use: I.E.:   (for the three variables   x,   y,   and   z),   where: After sorting, the three variables would hold: The variables should contain some form of a number, but specify if the algorithm used can be for floating point or integers.   Note any limitations. The values may or may not be unique. The method used for sorting can be any algorithm;   the goal is to use the most idiomatic in the computer programming language used. More than one algorithm could be shown if one isn't clearly the better choice.

One algorithm could be:

Another algorithm   (only for numeric values):

Show the results of the sort here on this page using at least the values of those shown above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort three variables step by step in the CLU programming language

Source code in the clu programming language

% Sort three variables.
% The variables must all be of the same type, and the type
% must implement the less-than comparator.
sort_three = proc [T: type] (x,y,z: T) returns (T,T,T)
             where T has lt: proctype (T,T) returns (bool)
    if y
    if z
    if y
    return(x,y,z)
end sort_three

% Test it out on three values, when also given a type and a
% formatter.
example = proc [T: type] (x,y,z: T, fmt: proctype (T) returns (string))
          where T has lt: proctype (T,T) returns (bool)
    po: stream := stream$primary_output()
    
    % Print the variables
    stream$putl(po, "x=" || fmt(x) 
                || " y=" || fmt(y)
                || " z=" || fmt(z))
     
    % Sort them 
    x,y,z := sort_three[T](x,y,z)
    
    % Print them again
    stream$putl(po, "x=" || fmt(x) 
                || " y=" || fmt(y)
                || " z=" || fmt(z)
                || "\n")
end example     

% And then we also need formatters, since those are not standardized
% such as '<' is.
fmt_real = proc (n: real) returns (string)
    return(f_form(n,2,2))
end fmt_real

fmt_str = proc (s: string) returns (string)
    return( "'" || s || "'" )
end fmt_str

% Test it out on values of each type
start_up = proc ()
    example[int] (77444, -12, 0, int$unparse)
    example[real] (11.3, -9.7, 11.17, fmt_real)
    example[string] ("lions, tigers and", "bears, oh my!",
                    "(from the \"Wizard of Oz\")", fmt_str)
end start_up

  

You may also check:How to resolve the algorithm Menu step by step in the Brat programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the C programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the Julia programming language
You may also check:How to resolve the algorithm Inverted syntax step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the R programming language