Identity matrix in python – C++ Program to Print Identity Matrix

Identity matrix in python: In the previous article, we have discussed about CPP11 – Variadic Template Function | Tutorial & Examples. Let us learn how to Print Identity Matrix in C++ Program.

What is a matrix:

A matrix is a rectangular numeric sequence separated into columns and rows. A matrix element, also known as an entry, is a number that occurs in a matrix.

Example:

The matrix shown above has 5 rows and 4 columns, with entries ranging from 1 to 20.

The dimensions of a matrix reflect the number of rows and columns in this sequence.

Because there are 5 rows and 4 columns, this is referred to as a 5*4 matrix.

We will learn how to print an identity matrix in Python in this lesson. If all of the diagonal elements of a matrix from the upper left corner to the bottom right corner are 1, and all other members are 0, the matrix is said to be an identity matrix.
We also need to understand the terms ‘row’ and ‘column’ in relation to matrices. A matrix with m rows and n columns is referred to as a (m x n) matrix. It is worth noting that the identity matrix is also referred to as the unit matrix. For an identity matrix, the row count equals the column count.

As a result, identity matrices are defined by their size rather than by their row and column. If the size is ‘n,’ it will contain ‘n’ rows and ‘n’ columns. These matrices with equal row and column sizes are also known as square matrices.

Given the dimension of the square matrix ,the task is to print the identity matrix of dimensions n*n in C++

Examples:

Example1:

Input:

given dimension=10

Output:

1 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 
0 0 1 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 0 1

Example2:

Input:

given dimension=7

Output:

1 0 0 0 0 0 0 
0 1 0 0 0 0 0 
0 0 1 0 0 0 0 
0 0 0 1 0 0 0 
0 0 0 0 1 0 0 
0 0 0 0 0 1 0 
0 0 0 0 0 0 1

Program to Print Identity Matrix in C++

We implement the program to print identity matrix in C++ as below

1)Printing identity Matrix in C++

The basic idea behind printing an identity matrix is that its value will be equal to 1 if the current row number and column number are the same. Matrix[0][0] is 1 for the first row, matrix[1][1] is 1 for the second row, and so on. So, to print the matrix, we’ll use two for loops, and if the iterators in both for loops are the same, we’ll print 1. Otherwise, print 0.
Request that the user enter the matrix’s size and save it in variable size or given matrix dimension as static input. The identity matrix’s row and column sizes will be determined by this.

Run one for loop from 0 to the matrix’s size – 1. This for loop is used to print the rows of the matrix. If the current value in this loop is zero, we are working on the first row of the matrix; if it is one, we are working on the second row of the matrix, and so on.
Begin another inner for loop and run it from 0 to the matrix’s size – 1. This inner loop is used to manipulate the matrix’s columns. If the current value in this loop is 0, we are working on the matrix’s first column if it is 1, we are working on the matrix’s second column, and so on.

As a result, for any value of the outer loop, i.e. the loop on step-2, this inner loop will complete entirely. That is, we will first print all column values for the first row, then all column values for the second row, and so on.
Determine whether or not  i equals j. If yes, print 1  otherwise, print 0. This step is used to print 1 in the matrix’s diagonal. Only if the current row index of the outer loop equals the column index of the inner loop are we printing 1.
One new line should be printed. Alternatively, proceed to the next row.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
// function which prints the identity matrix of the given
// dimension
int printIdentityMat(int dimen)
{ // taking two variables rownum and colnum of integer type
    int rownum, colnum;
    // using nested for loops
    for (rownum = 0; rownum < dimen; rownum++) {
        for (colnum = 0; colnum < dimen; colnum++) {
            // if the rownum is equal to colnum then print 1
            if (rownum == colnum)
                cout << 1 << " ";
            // else print 0
            else
                cout << 0 << " ";
        }
        cout << endl;
    }
    return 0;
}

// Driver Code
int main()
{ // given dimensions
    int dimensions = 10;
    // passing the given dimension to printIdentityMat
    // function to print identity matrix
    printIdentityMat(dimensions);
    return 0;
}

Output:

1 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 
0 0 1 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 0 1

Here we printed the identity matrix of size 10.

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Related Programs: