How to resolve the algorithm Vector step by step in the Tcl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Vector step by step in the Tcl programming language
Table of Contents
Problem Statement
Implement a Vector class (or a set of functions) that models a Physical Vector. The four basic operations and a pretty print function should be implemented.
The Vector may be initialized in any reasonable way.
The four operations to be implemented are:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Vector step by step in the Tcl programming language
Source code in the tcl programming language
namespace path ::tcl::mathop
proc vec {op a b} {
if {[llength $a] == 1 && [llength $b] == 1} {
$op $a $b
} elseif {[llength $a]==1} {
lmap i $b {vec $op $a $i}
} elseif {[llength $b]==1} {
lmap i $a {vec $op $i $b}
} elseif {[llength $a] == [llength $b]} {
lmap i $a j $b {vec $op $i $j}
} else {error "length mismatch [llength $a] != [llength $b]"}
}
proc polar {r t} {
list [expr {$r * cos($t)}] [expr {$r * sin($t)}]
}
proc check {cmd res} {
set r [uplevel 1 $cmd]
if {$r eq $res} {
puts "Ok! $cmd \t = $res"
} else {
puts "ERROR: $cmd = $r \t expected $res"
}
}
check {vec + {5 7} {2 3}} {7 10}
check {vec - {5 7} {2 3}} {3 4}
check {vec * {5 7} 11} {55 77}
check {vec / {5 7} 2.0} {2.5 3.5}
check {polar 2 0.785398} {1.41421 1.41421}
You may also check:How to resolve the algorithm Empty program step by step in the FunL programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Fortran programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Frink programming language
You may also check:How to resolve the algorithm Arithmetic numbers step by step in the Delphi programming language