Skip to content

Code Examples

Abdelrahman Aly Abounegm edited this page Sep 3, 2020 · 2 revisions

Some code examples

2D array and matrix multiplication

routine main(time : integer) is
    type Array1D is array [ 4 ] integer;
    type Array2D is array [ 4 ] Array1D;
    // initialization
    var array1 : Array2D;
    var array2 : Array2D;
    var array3 : Array2D;
    // arrays filling
    for i in 1..4 loop
        for j in 1..4 loop
            array1[i][j] := i + j;
            array2[j][i] := i - j;
        end
    end
    // multiplication of array1 and array2
    for i in 1..4 loop
        for j in 1..4 loop
            // (i, j) - coordinates in the new array
            for k in 1..4 loop
                // k - itterator for "parent" cols and rows
                array3[i][j] := array3[i][j] + array1[i][k] * array2[k][j];
            end
        end
    end
end

Recursional factorial

routine factorial(number: integer) : integer
is
    if num /= 1 then
        return num * factorial(num - 1);
    else
        return 1;
    end
end

routine main(num : integer) : integer is
    var a is factorial(num);
    return a;
end

Stack (bracing example)

routine main(input: integer) : integer is
    // initialization
    var counter : integer is 0;

    // input is binary encoding 0-left 1-right brace,
    // the goal is to validate if the expression is valid (input is read from right to left)

    while input > 0 loop
        if input % 2 = 0 then
            counter := counter + 1;
            input := input / 2;
        else
            counter := counter - 1;
            input := (input - 1) / 2;
        end
    end
    if counter then
        return counter;
    else
        return 0;
    end
end

A bit of structures and their assignment

routine main(a : integer) : integer is
    type Node is record
        var value       :   real
        var previous    :   Node
        var next        :   Node
        var is_last     :   boolean
        var is_first    :   boolean
    end

    // create first node

    var node : Node;
    node.value := 0.3;
    node.is_last := 1;
    node.is_first := 1;
    // append new node
    var node1 : Node;
    node1.value := 0.7;
    node1.is_last := true;
    node1.is_first := 0;

    node.is_last := false;
    node.next := node1;

    node1.previous := node;

    // insert a node at the middle

    var node2 : Node;
    node2.value := 5.8;
    node2.is_last := false;
    node2.is_first := 0;

    node2.next := node.next;
    node2.previlos := node2.previous;

    node.next := node2;
    node1.previous := node.next;

    // create some fantom nodes
    var some : Node;
    some.value := 7;
    some.previous.value := 6;
    some.previous := node2;

    return node.next.next.previous.previous.value + true;
end

TODO: recursive structures are not supported

Collatz numbers

type BigArray is array [100000] integer

var nums: BigArray  // initialized with zeros

routine collatz (n: integer) is
    var i is 1;
    while nums[i] /= 1 loop
        if nums[i] % 2 = 0 then
            nums[i+1] := nums[i] / 2
        else
            nums[i+1] := nums[i] * 3 + 1
        end
        i := i + 1
    end
    print(i)
end

Dijkstra

// algorithm from https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/

var INFINITY is 999999; var x is 5

routine dijkstra(graph: array array integer, src: integer):
                array [3] integer is
    
    var dist: array [3] integer; // The output array. dist[i] will hold the shortest distance from src to i

    var sptSet: array [3] boolean; // sptSet[i] will be true if vertex i is included in shortest path tree or shortest distance from src to i is finalized 
  
    // Initialize all distances as INFINITE
    for i in 1..graph.length loop
        dist[i] := INFINITY
    end
  
    // Distance of source vertex from itself is always 0 
    dist[src] := 0 
  
    // Find shortest path for all vertices 
    for count in 1..graph.length-1 loop
        // Pick the minimum distance vertex from the set of vertices not 
        // yet processed. u is always equal to src in the first iteration. 
        var u is minDistance(dist, sptSet)

        // Mark the picked vertex as processed 
        sptSet[u] := true 
  
        // Update dist value of the adjacent vertices of the picked vertex. 
        for v in 1..graph.length loop 

            // Update dist[v] only if is not in sptSet, there is an edge from 
            // u to v, and total weight of path from src to  v through u is 
            // smaller than current value of dist[v]
            // NOTE: no `not` in grammar
            if not sptSet[v] and graph[u][v] /= -1 and dist[u] /= INFINITY 
               and dist[u] + graph[u][v] < dist[v] then
               dist[v] := dist[u] + graph[u][v]
           end
        end
    end
    return dist
end

routine main () is
    var graph is array [3] array [3] integer
    graph[1][1] := 3; graph[1][2] := 1; graph[1][3] := 2
    graph[2][1] := 5; graph[2][2] := 4; graph[3][3] := 6
    graph[3][1] := 1; graph[3][2] := 4; graph[3][3] := 9

    var distances is dijkstra(graph, 1)
    print(distances)
end

Factorial with tail recursion

routine tailFactorial (x: integer, result: integer): integer is
    if x = 0 then
        return 1
    end
    return tailFactorial(x-1, result*x)
end

routine factorial (x: integer) : integer is
    return tailFactorial(x, 1)
end

routine main () : integer is
    if factorial(5) = 120 then
        return 0
    end

    return 1
end

Sum-square difference

calculates the difference between and

routine main (x: integer): integer is
    var sum is 0
    var squaredSum: integer
    for num in 1..x loop
        sum := sum + num
        squaredSum := squaredSum + num * num
    end

    var sumSquared is sum*sum
    return sumSquared - squaredSum
end

Some summations

var a : integer
var b is 5 + 8
var ans : integer

routine Sum(a : integer) : integer is
    var sum is 0
    for i in 0 .. a loop
        sum := sum + i
    end
    return sum
end

routine main(m : integer) is
    if a <= b
    then
        var sum is 0
        for i in 0 .. 10 loop
            sum := sum + i
        end
        ans := sum
    else
        ans := Sum(10)
    end
end

Some declarations

type int is integer
type bool is boolean
type float is real
type vector is array [5] int
type vr is vector
var a : vr
var b : float

routine main(m : integer) is
    b := 5.2
    var i is 1
    while i <= 5 loop
        a[i] := b
        b := b + 0.5
    end
end

Vector addition

type float is real
type vector is record
    var x : float
    var y : float
end

routine Add_Vr(a : vector, b : vector) : vector is
    var c : vector
    c.x := a.x + b.x
    c.y := a.y + b.y
    return c
end

routine main(m : integer) is
    var a : vector
    var b : vector
    var c : vector
    a.x := 1.0
    a.y := 1.0
    b.x := -1.0
    b.y := -1.0
    c.x := -1.0
    c.y := 1.0
    a := c
    c := Add_Vr(a, b)
end

Simple record

type BigArray is array [100000] integer

type Human is record
    var id                  :   integer     
    var height              :   real
    var hasHighterEducation :   boolean
    var test                :   BigArray
end

routine main (n: integer) is
    var i : Human;
    i.height := 167.8;
    i.id := 12345;
    i.test[5] = 345;
    print(i)
end

Some arithmetics

routine foo(number: integer) : integer
is
   return i*3/5+7*8+6.7
end

routine main(num : integer) : integer is
    var a is foo(num);
    return a-74%3;
end

Infinite loop & type aliases

type int is integer
type bool is boolean
var b : float

routine main(m : integer) :integer is
    b := 5.2
    var i is 1
    while i <= 5 loop
        if i%2 = 0 then
            b := b + 0.5
        end
    end
    return b
end

Weird Collatz? idk

type BigArray is array [100000] integer

var nums: BigArray  // initialized with zeros

routine collatz (n: integer) :integer is
    var i is 1;
    while nums[i] /= 1 loop
        if nums[i] % 2 = 0 then
            nums[i+1] := nums[i] / 2
            if nums[i] % 6 = 0 then
                return i
            else
                return 0
            end
        else
            nums[i+1] := nums[i] * 3 + 1
        end
        i := i + 1
    end
    print(i)
end

Clone this wiki locally