Compare commits

...

2 commits

Author SHA1 Message Date
array-in-a-matrix 198e16ad7f cleaned up code 2023-01-19 12:41:09 -05:00
array-in-a-matrix 5549aa5e28 sanitize user input 2023-01-19 12:38:06 -05:00

View file

@ -1,5 +1,19 @@
import strutils, sequtils
proc getInt: int =
while(true):
try:
return parseInt(readline(stdin))
except:
echo "Please enter an integer, try again."
proc getFloat: float =
while(true):
try:
return parseFloat(readline(stdin))
except:
echo "Please enter a number, try again."
#? prints a matrix to the standard output
proc printMatrix*(matrix: seq[seq[float]]) =
let row: int = matrix.len
@ -11,8 +25,7 @@ proc fillMatrix*(matrix: var seq[seq[float]], row, col: int) =
for i in countup(1, row):
for j in countup(1, col):
echo "Enter item:"
var entry: float = parseFloat(readLine(stdin)) # TODO: while loop to check if input is valid
matrix[i-1].add(entry)
matrix[i-1].add(getFloat())
matrix[i-1].delete(0)
echo matrix[i-1]
@ -45,14 +58,14 @@ proc calcCross(vector1: array[3, float], vector2: array[3, float]): array[3, flo
let k: float = vector1[0] * vector2[1] - vector1[1] * vector2[0]
result = [i, j, k]
proc dot*() =
proc dot* =
echo "MATRIX DOT PRODUCT"
#? record first matrix
echo "Enter number of rows in the first matrix:"
let r1: int = parseInt(readLine(stdin))
let r1: int = getInt()
echo "Enter number of columns in the first matrix:"
let c1: int = parseInt(readLine(stdin))
let c1: int = getInt()
var m1 = newSeqWith(r1, newSeq[float](c1))
procedures.fillMatrix(m1, r1, c1)
@ -60,9 +73,9 @@ proc dot*() =
#? record second matrix
echo "Enter number of rows in the second matrix:"
let r2: int = parseInt(readLine(stdin))
let r2: int = getInt()
echo "Enter number of columns in the second matrix:"
let c2: int = parseInt(readLine(stdin))
let c2: int = getInt()
var m2 = newSeqWith(r2, newSeq[float](c2))
procedures.fillMatrix(m2, r2, c2)
@ -77,7 +90,7 @@ proc dot*() =
echo "\nResult matrix is:"
printMatrix(m)
proc cross*() =
proc cross* =
echo "VECTOR CROSS PRODUCT"
type
@ -89,12 +102,12 @@ proc cross*() =
echo "Enter numbers in the first vector:"
for i in 0..2:
echo "Enter item:"
v1[i] = parseFloat(readLine(stdin))
v1[i] = getFloat()
echo "Enter numbers in the second vector:"
for i in 0..2:
echo "Enter item:"
v2[i] = parseFloat(readLine(stdin))
v2[i] = getFloat()
#? resultent vector
let v = calcCross(v1, v2)