
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to convert given Matrix to a Diagonal Matrix in C++
Given with the matrix of size nxn the task it to convert any type of given matrix to a diagonal matrix.
What is a diagonal Matrix
Diagonal matrix is the nxn matrix whose all the non-diagonal elements are zero and diagonal elements can be any value.
Given below is the diagram of converting non-diagonal elements to 0.
$$\begin{bmatrix}1 & 2 & 3 \4 & 5 & 6 \7 & 8 & 9 \end{bmatrix}\:\rightarrow\:\begin{bmatrix}1 & 0 & 3 \0 & 5 & 0 \7 & 0 & 9 \end{bmatrix}$$
The approach is to start one loop for all non-diagonal elements and another loop for diagonal elements and replace the value of non-diagonals with zero and leave diagonals elements unchanged.
Example
Input-: matrix[3][3] = {{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }} Output-: {{ 1, 0, 3}, { 0, 5, 0}, { 7, 0, 9}} Input-: matrix[3][3] = {{ 91, 32, 23 }, { 40, 51, 26 }, { 72, 81, 93 }} Output-: {{ 91, 0, 23}, { 0, 51, 0}, { 72, 0, 93}}
ALGORITHM
Start Step 1-> define macro for matrix size as const int n = 10 Step 2-> Declare function for converting to diagonal matrix void diagonal(int arr[][n], int a, int m) Loop For int i = 0 i < a i++ Loop For int j = 0 j < m j++ IF i != j & i + j + 1 != a Set arr[i][j] = 0 End End End Loop For int i = 0 i < a i++ Loop For int j = 0 j < m j++ Print arr[i][j] End Print \n End Step 2-> In main() Declare matrix as int arr[][n] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } Call function as diagonal(arr, 3, 3) Stop
Example
#include <iostream> using namespace std; const int n = 10; //print 0 at diagonals in matrix of nxn void diagonal(int arr[][n], int a, int m) { for (int i = 0; i < a; i++) { for (int j = 0; j < m; j++) { if (i != j && i + j + 1 != a) arr[i][j] = 0; } } for (int i = 0; i < a; i++) { for (int j = 0; j < m; j++) { cout << arr[i][j] << " "; } cout << endl; } } int main() { int arr[][n] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; diagonal(arr, 3, 3); return 0; }
Output
IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT
0 2 0 4 0 6 0 8 0
- Related Articles
- C program to interchange the diagonal elements in given matrix
- Program to check diagonal matrix and scalar matrix in C++
- Convert a single column matrix into a diagonal matrix in R.
- Program to print a matrix in Diagonal Pattern.
- How to convert a vector into a diagonal matrix in R?
- Golang program to print right diagonal matrix
- Swift Program to Print Diagonal Matrix Pattern
- Program to find diagonal sum of a matrix in Python
- Program to count number of operations to convert binary matrix to zero matrix in C++
- Print numbers in matrix diagonal pattern in C Program.
- How to create a block diagonal matrix using a matrix in R?
- Swift Program to print the left diagonal matrix
- Swift Program to print the right diagonal matrix
- Golang program to print the left diagonal matrix
- Program to sort each diagonal elements in ascending order of a matrix in C++

Advertisements