Ch7.3 Notes Part 3: Basic Matrix Loops using Pseudocode

Programming Starts With Pseudocode

When writing a new program for a numerical method, it is common to first write psuedocode using pencil and paper.

  • Pseudocode is a plain language description of the steps in an algorithm or another system.
  • Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine reading.
  • The programming language is augmented with natural language description details, where convenient, or with compact mathematical notation.
  • Read more at https://en.wikipedia.org/wiki/Pseudocode

Pseudocode

When writing pseudocode, we usually need to answer some basic questions, such as:

  • What is the input?
  • What is the output?
  • Are there any parameters give or to be determined?
  • What parameters are created, initialized and used?

Pseudocode for a course like ours typically starts with a specification of input, a declaration of output, initialization of parameters, a computational loop (such as a for loop), an output statement, and a STOP command.

Example 1

Write pseudocode for adding up the values of the entries in \( \mathbf{x} \).

\[ \mathbf{x} = [x_1,x_2,\ldots,x_n] \]

Example 2

Write pseudocode for adding up the values of the entries in the matrix \( A \) using row sums (add across rows).

\[ A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \]

Example 3

Write pseudocode for adding up the values of the entries below the main diagonal in the matrix \( A \), using column sums.

\[ A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \]

Example 4

Write pseudocode for adding up the values of the entries above the main diagonal in the matrix \( A \), using column sums.

\[ A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \]