C program to find normal and trace of Matrix

This C program determines the trace and normal of a given matrix. The main diagonal of a matrix, or the diagonal running from its upper left to its lower right, is referred to as the trace of the matrix. The square root of the total elemental sum represents the matrix's normal.

Approach

  1. Make a matrix and list every element in it.
  2. Take the total of all the array's members and compute its square root to determine the matrix's normal.
  3. Add the key diagonal members to assess the matrix's trace.

Example:

Filename: Normal_Trace.c

Output:

Enter the rows of a matrix: 4
Enter the columns of a matrix:4
Enter the elements of the matrix:
1 2 3 4
5 6 7 8
3 4 5 7
2 4 7 9
The Frobenius Norm: 21.28
The Trace of the matrix: 21.00

Explanation:

The provided C program calculates the Frobenius norm and trace of a matrix. The necessary libraries for input/output and mathematical operations are first included. The system using the inputs of the matrix itself, the number of rows (r), the number of columns (c), and the Frobenius norm, the normal function calculates the Frobenius norm, which measures the "size" of a matrix.

The square root of the total is returned as the norm after iterating through the matrix and adding up the squares of each element. Given a matrix with square corners and its size num, the matrix_Trace function generates the trace of the matrix, which is the sum of its diagonal components. It returns the sum of the diagonal elements after iterating through them.

The program to determine the Frobenius norm of the matrix uses the matrixNormal function, which accepts the dimensions r and c along with the matrix itself. The Frobenius norm of the matrix is provided by printing the result, which is kept in a variable called normal and written to the terminal with a pair of decimal places.

The program uses the matrix_Trace function to calculate the trace value if the matrix is square, providing the dimension c (because it is a square matrix) and the matrices. A variable named traceofmatrix stores the trace value, which is reported to the terminal with a maximum of two decimals and denotes the trace of the matrix.

The program collects the matrix's components in the main function and checks to make sure they are square before tracing them. After that, it gathers the matrix elements the user enters and initializes a matrix array. MatrixNormal is used for calculating and displaying the Frobenius norm. Matrix_Trace is used to compute and display the trace if the matrix is square. To indicate that the program has run successfully, it returns 0.