Saturday 23 August 2014

C++ program to find transpose of a sparse matrix

Transpose of a sparse matrix can be found by interchanging row to column and column to row

C++ program to find transpose of a sparse matrix

#include<iostream>
#include<iomanip>
using namespace std;
void show(int (&spare)[20][3])
{
    cout<<"RowNO\tColNo\tValue";
    for(int i=0;i<=spare[0][2];i++)
        cout<<endl<<spare[i][0]<<setw(8)<<spare[i][1]<<setw(8)<<spare[i][2];
}
int main()
{
    int temp,spare[20][3],row,col,terms;
    cout<<"Enter number of rows and columns : ";
    cin>>spare[0][0]>>spare[0][1];
    cout<<"Enter number of non-zero terms in the matrix :";
    cin>>spare[0][2];
    for(int i=1;i<=spare[0][2];i++)
    {
        cout<<"Enter the row, column and value of non zero term ";
        cin>>spare[i][0]>>spare[i][1]>>spare[i][2];
    }
    show(spare);
    for(int i=1;i<=spare[0][2];i++)
    {
        temp=spare[i][0];
        spare[i][0]=spare[i][1];
        spare[i][1]=temp;
    }
    cout<<endl<<endl<<"Transpose of matrix "<<endl;
    show(spare);
    return 0;
}

No comments:

Post a Comment