How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the Nim programming language

Table of Contents

Problem Statement

Find and display the sum of elements that are below the main diagonal of a matrix. The matrix should be a square matrix.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the Nim programming language

Source code in the nim programming language

type SquareMatrix[T: SomeNumber; N: static Positive] = array[N, array[N, T]]

func sumBelowDiagonal[T, N](m: SquareMatrix[T, N]): T =
  for i in 1..<N:
    for j in 0..<i:
      result += m[i][j]

const M = [[ 1,  3,  7,  8, 10],
           [ 2,  4, 16, 14,  4],
           [ 3,  1,  9, 18, 11],
           [12, 14, 17, 18, 20],
           [ 7,  1,  3,  9,  5]]

echo sumBelowDiagonal(M)


  

You may also check:How to resolve the algorithm Digital root step by step in the F# programming language
You may also check:How to resolve the algorithm System time step by step in the Scheme programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Pointers and references step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the OCaml programming language