How to resolve the algorithm Display a linear combination step by step in the Modula-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Display a linear combination step by step in the Modula-2 programming language

Table of Contents

Problem Statement

Display a finite linear combination in an infinite vector basis

(

e

1

,

e

2

, … )

{\displaystyle (e_{1},e_{2},\ldots )}

. Write a function that, when given a finite list of scalars

(

α

1

,

α

2

, … )

{\displaystyle (\alpha ^{1},\alpha ^{2},\ldots )}

, creates a string representing the linear combination

i

α

i

e

i

{\displaystyle \sum {i}\alpha ^{i}e{i}}

in an explicit format often used in mathematics, that is: where

α

i

k

≠ 0

{\displaystyle \alpha ^{i_{k}}\neq 0}

The output must comply to the following rules:

Show here output for the following lists of scalars:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Display a linear combination step by step in the Modula-2 programming language

Source code in the modula-2 programming language

MODULE Linear;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;

PROCEDURE WriteInt(n : INTEGER);
VAR buf : ARRAY[0..15] OF CHAR;
BEGIN
    FormatString("%i", buf, n);
    WriteString(buf)
END WriteInt;

PROCEDURE WriteLinear(c : ARRAY OF INTEGER);
VAR
    buf : ARRAY[0..15] OF CHAR;
    i,j : CARDINAL;
    b : BOOLEAN;
BEGIN
    b := TRUE;
    j := 0;

    FOR i:=0 TO HIGH(c) DO
        IF c[i]=0 THEN CONTINUE END;

        IF c[i]<0 THEN
            IF b THEN WriteString("-")
            ELSE      WriteString(" - ") END;
        ELSIF c[i]>0 THEN
            IF NOT b THEN WriteString(" + ") END;
        END;

        IF c[i] > 1 THEN
            WriteInt(c[i]);
            WriteString("*")
        ELSIF c[i] < -1 THEN
            WriteInt(-c[i]);
            WriteString("*")
        END;

        FormatString("e(%i)", buf, i+1);
        WriteString(buf);

        b := FALSE;
        INC(j)
    END;

    IF j=0 THEN WriteString("0") END;
    WriteLn
END WriteLinear;

TYPE
    Array1 = ARRAY[0..0] OF INTEGER;
    Array3 = ARRAY[0..2] OF INTEGER;
    Array4 = ARRAY[0..3] OF INTEGER;
BEGIN
    WriteLinear(Array3{1,2,3});
    WriteLinear(Array4{0,1,2,3});
    WriteLinear(Array4{1,0,3,4});
    WriteLinear(Array3{1,2,0});
    WriteLinear(Array3{0,0,0});
    WriteLinear(Array1{0});
    WriteLinear(Array3{1,1,1});
    WriteLinear(Array3{-1,-1,-1});
    WriteLinear(Array4{-1,-2,0,-3});
    WriteLinear(Array1{-1});

    ReadChar
END Linear.


  

You may also check:How to resolve the algorithm Negative base numbers step by step in the Action! programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Scala programming language
You may also check:How to resolve the algorithm String length step by step in the Elena programming language
You may also check:How to resolve the algorithm Mutex step by step in the E programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the J programming language