From d42bcf9c42337240fd0ba5b66ef1f65e7477ebac Mon Sep 17 00:00:00 2001 From: array-in-a-matrix Date: Thu, 17 Nov 2022 21:35:45 -0500 Subject: [PATCH] move proc to seperate file --- src/main.nim | 40 ++++++++++++++++++---------------------- src/procedures.nim | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 src/procedures.nim diff --git a/src/main.nim b/src/main.nim index 8e14d1d..4610cdb 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,28 +1,24 @@ -import strutils, sequtils #, strformat +import strutils, sequtils, procedures #, strformat -proc print_matrix(matrix: seq[seq[float]], row: int) = - for i in countup(1, row): - echo matrix[i-1] +# TODO: check if command was either dot or cross product -proc fill_matrix(matrix: var seq[seq[float]], row, col: int) = - for i in countup(1, row): - for j in countup(1, col): - echo "Enter item in row:" - var entry: float = parseFloat(readLine(stdin)) - matrix[i-1].add(entry) - matrix[i-1].delete(0) - echo matrix[i-1] +echo "Enter number of rows in the first matrix:" +let r1: int = parseInt(readLine(stdin)) +echo "Enter number of columns in the first matrix:" +let c1: int = parseInt(readLine(stdin)) +var m1 = newSeqWith(r1, newSeq[float](c1)) -echo "Enter number of rows:" -let row: int = parseInt(readLine(stdin)) +procedures.fillMatrix(m1, r1, c1) -echo "Enter number of columns:" -let col: int = parseInt(readLine(stdin)) +echo "Enter number of rows in the second matrix:" +let r2: int = parseInt(readLine(stdin)) +echo "Enter number of columns in the second matrix:" +let c2: int = parseInt(readLine(stdin)) +var m2 = newSeqWith(r2, newSeq[float](c2)) -var matrix = newSeqWith(row, newSeq[float](col)) +procedures.fillMatrix(m2, r2, c2) - -fill_matrix(matrix, row, col) - -echo "\nMatrix entered is:" -print_matrix(matrix, row) +echo "\nFirst matrix is:" +procedures.printMatrix(m1, r1) +echo "\nSecond matrix is:" +procedures.printMatrix(m2, r2) diff --git a/src/procedures.nim b/src/procedures.nim new file mode 100644 index 0000000..7a85a4c --- /dev/null +++ b/src/procedures.nim @@ -0,0 +1,16 @@ +import strutils + +## prints a matrix to the standard output +proc printMatrix*(matrix: seq[seq[float]], row: int) = + for i in countup(1, row): + echo matrix[i-1] + +## get elements of matrix from standard input +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 in row:" + var entry: float = parseFloat(readLine(stdin)) + matrix[i-1].add(entry) + matrix[i-1].delete(0) + echo matrix[i-1] \ No newline at end of file