%--------------------------------------------------------------------
%   csc 148f, 1996, Assignment 0: Divisibility by 11
%   name:            >> Insert your name here <<
%   student number:  >> Insert your student number here <<
%   lecturer:        >> Insert your lecturer's name here <<
%   lecture time:    >> Insert your lecture time here <<
%   tutor:           >> Insert your tutor's name here <<
%--------------------------------------------------------------------

% >> Insert comments here to describe the program. <<



% Constants
%--------------------------------------------------------------------

% The maximum length of the integers to be tested.

const MaxIntLen := 50



% Types
%--------------------------------------------------------------------

% An array to store input, which may be too large for a normal int.
% Each array element stores a single digit of the integer, with the
% most significant digit in array position 1.

type IntArray : array 1 .. MaxIntLen of int



% GetInt
%--------------------------------------------------------------------
% Reads the next token from the standard input and stores up to
% MaxIntLen digits of it in BigInt.  Sets IntLength to the number of
% digits stored.  Leading zeros, if any, are stored.
% Preconditions: There must be a token in the standard input.

proc GetInt (var BigInt : IntArray, var IntLength : int)

    % First read the integer as a string, then pluck the digits off,
    % convert them to ints, and store them in the array.
    var input : string
    get input
    IntLength := min (length (input), MaxIntLen)
    for i : 1 .. IntLength
        BigInt (i) := strint (input (i))
    end for

end GetInt



% PutInt
%--------------------------------------------------------------------
% Prints the given BigInt to the standard output.
% Leading zeros, if any, are printed.
% Preconditions: BigInt must contain values from positions 1 to
%                IntLength inclusive.

proc PutInt (BigInt : IntArray, IntLength : int)

    for i : 1 .. IntLength
        put BigInt (i) ..
    end for

end PutInt



% SubtractLastDigit
%--------------------------------------------------------------------
% Subtracts BigInt(IntLength) from the number represented by
% BigInt[1..(IntLength-1)].
% Preconditions: IntLength must be > 2, and
%                BigInt must not have leading zeros.

proc SubtractLastDigit (var BigInt : IntArray, IntLength : int)

    % >> Insert body of procedure here. <<

end SubtractLastDigit



% StripLeadingZero
%--------------------------------------------------------------------
% If the leading digit of the given integer is zero, it is stripped
% off.
% Preconditions: BigInt must contain values from positions 1 to
%                IntLength inclusive, and IntLength must be >= 1.

proc StripLeadingZero (var BigInt : IntArray, var IntLength : int)

    % >> Insert body of procedure here. <<

end StripLeadingZero



% DivisibleBy11
%--------------------------------------------------------------------
% Returns true if BigInt is divisible by 11, and false otherwise.
% Uses the method described in the assignment handout.
% Preconditions: BigInt must contain values from positions 1 to
%                IntLength inclusive, IntLength must be >= 1, and
%                BigInt must not have leading zeros.

function DivisibleBy11 (BigInt : IntArray, IntLength : int) : boolean

    % >> Insert body of function here. <<

end DivisibleBy11



% Main Program
%--------------------------------------------------------------------

% The current integer being tested, and its length.
var BigInt : IntArray
var IntLength : int

% Read and test integers until eof, printing appropriate messages.
loop
    get skip
    exit when eof
    GetInt (BigInt, IntLength)

    put "The number " ..
    PutInt (BigInt, IntLength)
    if DivisibleBy11 (BigInt, IntLength) then
        put " is divisible by 11."
    else
        put " is not divisible by 11."
    end if
end loop
