The Trace and the Norm of a Matrix

Medium

Read the rows and columns of a matrix followed by its elements row by row. Print the matrix under the heading matrix A:, one row per line with single spaces. Then, if the matrix is square, print trace = 5 with the sum of its diagonal computed by a user-defined function long trace(int m[MAX][MAX], int n); otherwise print trace: not defined for a 2 by 3 matrix with the actual dimensions. Finally print norm = 5.48, the square root of the sum of the squares of every element to two decimal places, computed by a user-defined function double norm(int m[MAX][MAX], int rows, int cols) that accumulates in double and casts each element before squaring. Read and print with readMatrix and printMatrix. Each dimension must be between 1 and 10 (MAX); otherwise print dimensions must be between 1 and 10 and return 1. If any number cannot be read, print invalid input and return 1.

Success Criteria

Your code must pass 7 test case(s) to complete this exercise. 3 hint(s) are available if you need help.

Sign in to track your progress

You can work on exercises as a guest, but sign in to track your progress and save your submissions.

This platform is built by its community

Every lesson, project, and tool on HelloC++ is funded by sponsors. Join them and help shape what we build next.

Become a Patron

How did you find this exercise?

Your rating helps us improve the content.