How to resolve the algorithm Kernighans large earthquake problem step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Kernighans large earthquake problem step by step in the Cowgol programming language

Table of Contents

Problem Statement

Brian Kernighan, in a lecture at the University of Nottingham, described a problem on which this task is based. You are given a a data file of thousands of lines; each of three whitespace separated fields: a date, a one word name and the magnitude of the event. Example lines from the file would be lines like:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Kernighans large earthquake problem step by step in the Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";
include "file.coh";

# Process a file line by line
interface LineCb(line: [uint8]);
sub ForEachLine(fcb: [FCB], cb: LineCb) is
    var buf: uint8[256];
    var ptr := &buf[0];
    
    var length := FCBExt(fcb);
    while length != 0 loop
        var ch := FCBGetChar(fcb);
        [ptr] := ch;
        ptr := @next ptr;
        if ch == '\n' then
            [ptr] := 0;
            ptr := &buf[0];
            cb(&buf[0]);
        end if;
        length := length - 1;
    end loop;
end sub;

# Get magnitude from line
# Cowgol does not support floating point arithmetic, so the integer and 
# fractional parts are returned separately
sub magnitude(line: [uint8]): (i: uint8, frac: uint8) is
    i := 0;
    frac := 0;
    var col: uint8 := 1;
    var space: uint8 := 0;
    # scan ahead to 3rd column
    while col < 3 loop
        var ch := [line];
        line := @next line;
        if ch == 0 then break; end if;
        if ch <= ' ' then
            while ch <= ' ' and ch != 0 loop
                ch := [line];
                line := @next line;
            end loop;
            col := col + 1;
        end if;
    end loop;
    if ch == 0 then
        return; # no 3rd column
    end if;
    line := @prev line;
    
    var n: int32;
    var pos: [uint8];
    # grab integer part
    (n, pos) := AToI(line);
    if pos == line then
        return; # no value
    end if;
    i := n as uint8;
    if [pos] == '.' then
        # grab fractional part
        (n, pos) := AToI(@next pos);
        frac := n as uint8;
    end if;
end sub;

# Print any line that has a magnitude > 6
sub PrintIfGt6 implements LineCb is
    var i: uint8;
    var frac: uint8;
    (i, frac) := magnitude(line);
    if i > 6 or (i == 6 and frac > 0) then
        print(line);
    end if;
end sub;

# Open "data.txt" and scan each line
var quakes: FCB;
if FCBOpenIn(&quakes, "data.txt") != 0 then
    print("Error!\n");
    ExitWithError();
end if;

ForEachLine(&quakes, PrintIfGt6);

  

You may also check:How to resolve the algorithm Taxicab numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the Tcl programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the ERRE programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Aime programming language
You may also check:How to resolve the algorithm String matching step by step in the M2000 Interpreter programming language