Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Monday, 10 March 2014

Trace of a square matrix - C++ Program source code

Trace of a square matrix is the sum of elements of the principal diagonal of the matrix.

So without any intro, let's move on to code:
Code:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int mat[100][100],tr=0,i,j,m,n;
    cout<<"Enter the order of the matrix : \n";
    cin>>m>>n;
    while(m!=n){
        cout<<"Not a square matrix!\n";
        exit(0);
    }
    cout<<"Enter the elements of the matrix : \n";
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            cin>>mat[i][j];
        }
    }
    cout<<"The matrix is : \n";
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            cout<<mat[i][j];
            cout<<"\t";
        }
        cout<<endl;
    }
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            if(i==j){
                tr = tr + mat[i][j];
            }
        }
    }
    cout<<"The trace is "<<tr;
}

Working:
First we input the order of the matrix. If number of columns is not equal to number of rows, the matrix is not a square matrix and will not have a trace. So if they aren't same, the program terminates using exit(0) [in the header <cstdlib>]. Now inside a nested for loop, if an element is on the principal diagonal, then the trace tr, initially set to zero, adds itself to the element. At the end of the loop, tr will be equal to the sum of diagonal elements. 
A couple of sample outputs is given below:




Happy Coding!
:)


Sunday, 23 February 2014

Transpose of a Matrix - C++ Program Source Code

Here is a program to find the transpose of a matrix. Here is an example for transposal of a matrix:

Example for a matrix transposal


Code:
#include<iostream.h>
#include<conio.h>
void main()
{
    int i,j,m,n,matrix[50][50],transpose[50][50];
    cout<<"Enter the order of the matrix\n";
    cin>>m>>n;
    cout<<"Enter the elements of the matrix \n";
    for(i=0;i<m;i++)
    {
       for(j=0;j<n;j++)
       {
            cin>>matrix[i][j];
       }
    }
    cout<<"The entered matrix is \n";
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
           cout<<matrix[i][j];
           cout<<"  ";
        }
        cout<<"\n";
    }
    //Definition of transpose
    for(i=0;i<n;i++)
    {
       for(j=0;j<m;j++)
       {
            transpose[i][j]=matrix[j][i];
       }
    } 
    cout<<"The transpose is \n";
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
           cout<<transpose[i][j];
           cout<<"  ";
        }
        cout<<"\n";
    }
}

For newer IDEs, visit this page.

Working:

First the user is asked to enter the order and subsequently the elements of a matrix. The given matrix is displayed for formality. The transpose has the number of rows is equal to the number of columns of the first matrix and number of columns equal to the number of rows of the first matrix. It is then defined appropriately and displayed. Here is the output based on the above example.



Happy Coding!

:)

You might also like