C++ program to swap or transpose a matrix by using multi-dimensional arrays

C++ program to swap or transpose a matrix by using multi-dimensional arrays. Using this program to print the transpose of the matrix. A matrix is a rectangular array of numbers that are arranged in rows and columns. The numbers are referred to as the matrix’s elements or entries. Engineering, physics, economics, and statistics, as well as various disciplines of mathematics, all use matrices.

#include<iostream>
using namespace std;
int row,col;
int main()
{
int val,row,col;
cin>>row;
cin>>col;
int a[row][col];
cout<<"\n a: \n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>val;
a[i][j]=val;
}
}
int b[col][row];
for(int j=0;j<row;j++)
{
for(int i=0;i<col;i++)
b[i][j]=a[j][i];
}
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
a[i][j]=b[i][j];
}
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
}
return 0;
}

Output:

2
2

a:
34
45
56
67


34 56
45 67

What is matrix and how to implement in c++ program

A matrix is a set of numbers organized into a predetermined number of rows and columns. The numbers are usually real numbers. Complex numbers can be found in matrices in general, but we won’t encounter them here. An example of a three-row, three-column matrix is as follows: Row 1 is at the top of the list.

What is transpose matrix and how to implement in c++ program

“A Matrix that is generated by changing all the rows of a given matrix into columns and vice versa,” according to the definition. Given a matrice of order 43, find the solution. The transpose of a matrix is achieved by swapping rows and columns. A matrix is “flipped” over its diagonal. The rows and columns are switched around. The value in the first row and third column, for example, ends up in the third row and first column.

What is namespace in c++ program

A namespace is a C++ feature that is not available in C. A namespace is a declarative zone that gives the identifiers (names of types, functions, variables, and so on) within it a scope. It is permissible to have many namespace blocks with the same name. The named scope is used for all declarations within those blocks.