How to resolve the algorithm N'th step by step in the BCPL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N'th step by step in the BCPL programming language

Table of Contents

Problem Statement

Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.

Returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th

Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs: 0..25, 250..265, 1000..1025

Note: apostrophes are now optional to allow correct apostrophe-less English.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm N'th step by step in the BCPL programming language

Source code in the bcpl programming language

get "libhdr"

// Generate ASCII string of number n with ordinal suffix
// The string is stored at v.
let nth(n, v) = valof
$(  let sfx = "thstndrd"
    let c, s = 0, ?
    // Generate digits
    $(  c := c + 1
        v%c := n rem 10 + '0'
        n := n / 10
    $) repeatuntil n = 0
    
    // Put digits in correct order 
    for i = 1 to c/2 do
    $(  let d = v%i
        v%i := v%(c+1-i)
        v%(c+1-i) := d
    $)
    
    // The length of the string is the amount of digits + 2
    v%0 := c+2;
    
    // Figure out the proper suffix from the last two digits
    test v%(c-1)='1' | v%c>'3' 
        then s := 0
        else s := 2*(v%c - '0')
    
    v%(c+1) := sfx%(s+1)
    v%(c+2) := sfx%(s+2)
    
    resultis v
$)

let start() be
$(  let buf = vec 10
    for i =    0 to   25 do writef("%S*N", nth(i, buf))
    for i =  250 to  265 do writef("%S*N", nth(i, buf))
    for i = 1000 to 1025 do writef("%S*N", nth(i, buf))
$)

  

You may also check:How to resolve the algorithm Literals/String step by step in the Axe programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Bitmap/PPM conversion through a pipe step by step in the Raku programming language
You may also check:How to resolve the algorithm Image convolution step by step in the OCaml programming language