-->




c++ programs to find transpose of a matrix with function and without function

Leave a Comment
Write two C++ programs which asks user to enter number of rows and colums and calculate its transpose and display the result accordingly.
Note: One with function and one without Function
function prototype:
void calculate_transpose(int matrix[5][5], int rows, int cols);
    
First we have to clear about what is transpose of a matrix.
Its to replace the number of rows with number of colums and vice versa to make a new matrix.
for example
if a matrix 'A' has dimension 2X3  means 2 rows 3 colums then new transpose matrix 'T' will be 3X2

        1  2  3                     1    4    
A=   4  5  6              T=   2   5
                                       3   6

C++ Code:   Compiler Used:  CodeBlocks
Without Functioin



    #include<iostream>
    using namespace std;
    int main()
    {

    int matrix[5][5],transpose_matrix[5][5];
    int i,j,rows,cols;
    // Taking Input In Array

      cout<<"Enter Number of ROWS :";
      cin>>rows;

      cout<<"Enter Number Of COLS  :";
      cin>>cols;
       for( i=0;i<rows;i++){
           for( j=0;j<cols;j++)
           {
               cin>>matrix[i][j];
           }
          }

          cout<<"\n Matrix You Entered\n";

       for( i=0;i<rows;i++){
           for( j=0;j<cols;j++)
           {
               cout<<matrix[i][j]<<"     ";
           }
           cout<<endl;
          }



// Calculating Transpose of Matrix
    cout<<"\n\n\nTranspose of Entered Matrix\n";
       for( i=0;i<rows;i++){
           for( j=0;j<cols;j++)
           {
               transpose_matrix[j][i]=matrix[i][j];
           }
           cout<<endl;
          }


//Displaying Final Resultant Array
       for( i=0;i<cols;i++){
           for( j=0;j<rows;j++)
           {
               cout<<transpose_matrix[i][j]<<"    ";
           }
           cout<<endl;
          }

   return 0;
    }



Sample input Output
C++ program transpose example of a matrix




With Function C++ Code:

In this the same code is used but passing the 2D array to a function with size of rows and cols



    #include<iostream>
    using namespace std;

    void calculate_transpose(int matrix[5][5], int rows, int cols);
    int main()
    {

    int matrix[5][5];
    int i,j,rows,cols;
    // Taking Input In Array

      cout<<"Enter Number of ROWS :";
      cin>>rows;

      cout<<"Enter Number Of COLS  :";
      cin>>cols;
       for( i=0;i<rows;i++){
           for( j=0;j<cols;j++)
           {
               cin>>matrix[i][j];
           }
          }

          cout<<"\n Matrix You Entered\n";

       for( i=0;i<rows;i++){
           for( j=0;j<cols;j++)
           {
               cout<<matrix[i][j]<<"     ";
           }
           cout<<endl;
          }


//**********CALLING FUNCTION************//
      calculate_transpose(matrix,rows,cols);


   return 0;
    }

 void calculate_transpose(int matrix[5][5], int rows, int cols)
{    int i,j;
    int transpose_matrix[5][5];
    cout<<"\n\n\nTranspose of Entered Matrix\n";
       for(i=0;i<rows;i++){
           for( j=0;j<cols;j++)
           {
               transpose_matrix[j][i]=matrix[i][j];
           }
           cout<<endl;
          }

       for(i=0;i<cols;i++){
           for( j=0;j<rows;j++)
           {
               cout<<transpose_matrix[i][j]<<"   ";
           }
           cout<<endl;
          }

}



Find more examples here:


0 comments:

Post a Comment