How to resolve the algorithm Vector products step by step in the Modula-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Vector products step by step in the Modula-2 programming language

Table of Contents

Problem Statement

A vector is defined as having three dimensions as being represented by an ordered collection of three numbers:   (X, Y, Z). If you imagine a graph with the   x   and   y   axis being at right angles to each other and having a third,   z   axis coming out of the page, then a triplet of numbers,   (X, Y, Z)   would represent a point in the region,   and a vector from the origin to the point. Given the vectors: then the following common vector products are defined:

Given the three vectors:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vector products step by step in the Modula-2 programming language

Source code in the modula-2 programming language

MODULE VectorProducts;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;

PROCEDURE WriteReal(r : REAL);
VAR buf : ARRAY[0..31] OF CHAR;
BEGIN
    RealToStr(r, buf);
    WriteString(buf)
END WriteReal;

TYPE Vector = RECORD
    a,b,c : REAL;
END;

PROCEDURE Dot(u,v : Vector) : REAL;
BEGIN
    RETURN u.a * v.a
         + u.b * v.b
         + u.c * v.c
END Dot;

PROCEDURE Cross(u,v : Vector) : Vector;
BEGIN
    RETURN Vector{
        u.b*v.c - u.c*v.b,
        u.c*v.a - u.a*v.c,
        u.a*v.b - u.b*v.a
    }
END Cross;

PROCEDURE ScalarTriple(u,v,w : Vector) : REAL;
BEGIN
    RETURN Dot(u, Cross(v, w))
END ScalarTriple;

PROCEDURE VectorTriple(u,v,w : Vector) : Vector;
BEGIN
    RETURN Cross(u, Cross(v, w))
END VectorTriple;

PROCEDURE WriteVector(v : Vector);
BEGIN
    WriteString("<");
    WriteReal(v.a);
    WriteString(", ");
    WriteReal(v.b);
    WriteString(", ");
    WriteReal(v.c);
    WriteString(">")
END WriteVector;

VAR a,b,c : Vector;
BEGIN
    a := Vector{3.0, 4.0, 5.0};
    b := Vector{4.0, 3.0, 5.0};
    c := Vector{-5.0, -12.0, -13.0};

    WriteVector(a);
    WriteString(" dot ");
    WriteVector(b);
    WriteString(" = ");
    WriteReal(Dot(a,b));
    WriteLn;

    WriteVector(a);
    WriteString(" cross ");
    WriteVector(b);
    WriteString(" = ");
    WriteVector(Cross(a,b));
    WriteLn;

    WriteVector(a);
    WriteString(" cross (");
    WriteVector(b);
    WriteString(" cross ");
    WriteVector(c);
    WriteString(") = ");
    WriteVector(VectorTriple(a,b,c));
    WriteLn;

    ReadChar
END VectorProducts.


  

You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Rust programming language
You may also check:How to resolve the algorithm Include a file step by step in the Ruby programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the 11l programming language
You may also check:How to resolve the algorithm String length step by step in the LSE64 programming language