C Program to Find Matrix Normal and Trace

C Program to Find Matrix Normal and Trace

A small school table has two rows and two columns. The matrix norm and trace operation should preserve what each row and column means, rather than treating the table as four unrelated values.

Here norm means the Frobenius norm: the square root of the sum of all squared entries. Trace adds the main-diagonal entries of a square matrix.

Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why matrix norm and trace uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Compute only the trace and identify which two elements contribute.

Matrix Norm and Trace: follow the data

  1. Given data

    A small school table has two rows and two columns. The matrix norm and trace operation should preserve what each row and column means, rather than treating the table as four unrelated values.

  2. Rule

    Here norm means the Frobenius norm: the square root of the sum of all squared entries. Trace adds the main-diagonal entries of a square matrix.

  3. Observed result

    Frobenius norm: 5.477; trace: 5

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The old normal wording is kept in the URL, but the operation is named precisely. A normal matrix in linear algebra is a different concept from computing a norm.

The example fixes both dimensions at two so every index can be checked directly. A general matrix interface must carry dimensions, require compatible shapes for its operation and validate storage for each referenced element.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
#include <math.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  double squares = 0.0; int trace = 0;
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) { squares += (double)matrix[row][col] * matrix[row][col]; if (row == col) trace += matrix[row][col]; }
  printf("Frobenius norm: %.3f; trace: %d\n", sqrt(squares), trace);
  return 0;
}

Expected output

Frobenius norm: 5.477; trace: 5
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
#include <math.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  double squares = 0.0; int trace = 0;
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) { squares += (double)matrix[row][col] * matrix[row][col]; if (row == col) trace += matrix[row][col]; }
  printf("Frobenius norm: %.3f; trace: %d\n", sqrt(squares), trace);
  return 0;
}

The squared entries sum to 1+4+9+16=30; the main diagonal sums to 1+4=5.

Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.

Follow the worked calculation

  1. Start with the sample

    The squared entries sum to 1+4+9+16=30; the main diagonal sums to 1+4=5.

  2. Apply the rule

    Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.

  3. Check the result

    Frobenius norm: 5.477; trace: 5

    Frobenius norm: 5.477; trace: 5
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
#include <math.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  double squares = 0.0; int trace = 0;
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) { squares += (double)matrix[row][col] * matrix[row][col]; if (row == col) trace += matrix[row][col]; }
  printf("Frobenius norm: %.3f; trace: %d\n", sqrt(squares), trace);
  return 0;
}

Compute only the trace and identify which two elements contribute.

Worked answer

The complete worked answer uses the same valid two-by-two input. Trace its index rule independently; compare each printed result with the source element or accumulation that produced it.

C17 · complete program

#include <stdio.h>
#include <math.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int trace = matrix[0][0] + matrix[1][1];
  printf("Trace: %d\n", trace);
  return 0;
}

Expected output

Trace: 5
Run in C compiler →

Recognize the important boundaries

The conversion before multiplication avoids an int intermediate product for these entries. Floating-point overflow remains possible for unrestricted large values.

Larger or untrusted dimensions require checked storage-size calculations. Integer products and accumulated sums also need a representable-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What must be known before indexing a general two-dimensional table?
Choose one answer. You can try again.

Answer and explanation

Its dimensions and compatible storage layout.

Correct. A row/column access is valid only within the corresponding bounds.

Use the idea independently

Here norm means the Frobenius norm: the square root of the sum of all squared entries. Trace adds the main-diagonal entries of a square matrix.

Continue with array boundaries.

Continue with function bounds contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Add Two Matrices →