Program to Check Identity Matrix in C Language

Program-to-Check-Identity-Matrix-in-C-Language

Program Description:

Write a Program to Check Identity Matrix in C programming. The program will accept a Matrix from the user and check if it is an Identity Matrix.

Prerequisites:

What is an Identity Matrix?

The Identity Matrix is a Square Matrix ( n x n) and the main diagonal elements of an Identity matrix are equal to 1 and the remaining elements are equal to Zero(0).

Here is an example of an Identity Matrix

identity-matrix-example

If you look at the above Identity Matrix, you will notice that all of the main diagonal elements have a value of 1 and the remaining elements have a value of 0.

The Identity Matrix is also called the Unit Matrix.

Program Excepted Input and Output:

Input:

Output:

Check Identity Matrix in C Program Explanation:

Here is the step-by-step instructions for the Program to Check Identity Matrix in C

  1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns in the Matrix. (Change these values as per your requirement)
  2. Start the Program by declaring a Matrix (2D Arrays) called Matrix X. The row size and column size of the matrix is ROWS and COLUMNS constants respectively.
  3. Prompt the user to provide the desired Row size and Column Size, and store them in the variables rows and columns respectively.
  4. An Identity Matrix is a Square Matrix, So the number of rows must be equal to columns. So check if the given matrix is a square matrix. ( Use (rows != columns))
  5. Check for array bounds using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the present limits.
  6. Take the user input for Input Matrix i.e Matrix-X, We need to use two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  7. Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<rows (that is for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix element and Read the element using the scanf function and Update the X[i][j] element.
      • Repeat the above step for all elements in the Matrix-X
  8. To check if the given Matrix is an Identity Matrix, We need to go through all elements of the Matrix and check all if all diagonal elements are equal to One and other elements are equal to Zero.
  9. Create Outer For Loop ( Iterates over rows) – for(i=0; i<rows; i++)
    • Then Create the Inner For loop (Iterates over columns) – for(j=0; j<columns; j++)
      • Check if the diagonal elements are equal to One using ( (i==j) && (X[i][j] != 1) ) condition, If any element is not equal to One(1), Then Matrix is not an Identity matrix.
      • Similarly, Check if all other elements are equal to Zero using ( (i!=j) && (X[i][j] != 0) ) condition.If any element is not equal to Zero(0), Then Matrix is not an Identity matrix.
  10. If the step-9 is completed, Then the Matrix is an Identity Matrix. Print the result on the console.

Program to Check Identity Matrix in C Language:

Let’s convert the above algorithm into the C Code.

Program Output:

Compile and Run the Program.

Test 1: Normal Cases:

check-identity-matrix-in-c-program-output

The above matrix X is an Identity Matrix. All diagonal elements are equal to One and the remaining elements are equal to Zero.

Here diagonal elements are one but one of the other elements ( X[2][1]) also has a value as One, So it is not an Identity Matrix.

Test 2: Negative Test Cases:

When a user enters a rectangular matrix.

The Identity Matrix is a Square Matrix, So the row size and column size should match, Otherwise, program displays an error message Indentity Matrix is Square Matrix, Rows and Columns Must be Equal

Test 3: Out of bounds

As we can see from the above output, The given row and column sizes are out of present limits. So program displayed an error message Invalid Rows or Columns size, Please Try again

Program to Check Identity Matrix using Functions in C:

We used only one function (i.e main() function) to take the user input and check the identity matrix. It is recommended to use sub-functions for each specific task. As the function improves readability and code debuggability. Here are the advantages of using functions in the program

Let’s rewrite the above program to use functions for each specific task.

Here is the rewritten version of the Check Identity Matrix program in C.

We have defined two custom functions in the above program(except main() function)

  1. The readMatrix() function – Used to read the elements from the user and update the Matrix X
    • Function Prototypevoid readMatrix(int rows, int columns, int X[rows][columns])
    • The readMatrix() function takes three formal arguments which are rows, columns, and Matrix X.
    • This function uses Two For loops to iterate over the given matrix A and updates its elements by using the scanf function – scanf(“%d”, &X[i][j]);
    • The readMatrix() function doesn’t return anything back to the caller. So return type is void.
    • As the arrays are passed by the address/reference, All the changes made to the Matrix-X in the readMatrix() function will be visible in the Caller Function (i.e main() function).
  2. The isIdentityMatrix() function is used to check if the given matrix is an Identity matrix.
    • Function Prototype : int isIdentityMatrix(int rows, int columns, int X[rows][columns])
    • The isIdentityMatrix() function takes three formal arguments similar to above readMatrix() function, Arguments include rows, columns, and matrix X.
    • This function checks the all elements in the Matrix-X and returns One(1) if the Matrix-X is an Identity Matrix, Otherwise, it returns Zero(0)

Call the isIdentityMatrix() function from the main() function and display the output based on the return value of the function.

Program Output:

Let’s compile and Run the program.

check-identity-matrix-using-functions-program-output

Let’s try a few more examples (including negative cases)

The program is generating the excepted output.

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply