Menu Close

Program to display m*n matrix using vector STL in C++

In this tutorial, We are going to learn about matrix representation by using vector STL (Standard Template Library) in C++ language.

There are many ways to create matrix. We will be using vector in given below example.

Method 1:

#include <iostream>

#include <vector>

#include <stdio.h>

using namespace std;


int main()
{

    int row, column, matrix_val;

    cout<<"Enter row size: ";

    cin>>row;


    cout<<"\nEnter column size: ";

    cin>>column;

    
    // Declare vector
    vector<vector<int> > arr(row, vector<int>(column));

    
    for (auto index_1 = 0; index_1 < row ; index_1++) {

        for (auto index_2 = 0; index_2 < column; index_2++){

            printf("\nEnter matrix value for position row: %d, column: %d:  ", index_1, index_2);

            cin>>matrix_val;

            arr[index_1][index_2] = matrix_val;
        }
    }


    // Print matrix
    for (auto index_1 = 0; index_1 < row ; index_1++) {

        for (auto index_2 = 0; index_2 < arr[index_1].size(); index_2++){

            cout<<arr[index_1][index_2]<<" ";

        }

        cout<<"\n";

    }

}

Output:

 Enter row size: 2
 Enter column size: 2
 Enter matrix value for position row: 0, column: 0:  1
 Enter matrix value for position row: 0, column: 1:  2
 Enter matrix value for position row: 1, column: 0:  3
 Enter matrix value for position row: 1, column: 1:  4
 Matrix is:

  1 2 
  3 4 

Method 2:

#include <iostream>

#include <vector>

#include <stdio.h>

using namespace std;


int main()
{
    int row, column, matrix_val;


    cout<<"Enter row size: ";

    cin>>row;


    cout<<"\nEnter column size: ";

    cin>>column;

    
    // Declare vector
    vector<vector<int> > arr(row);

    
    for (auto index_1 = 0; index_1 < row ; index_1++) {

        arr[index_1] = vector<int>(column);

        for (auto index_2 = 0; index_2 < column; index_2++){

            printf("\nEnter matrix value for position row: %d, column: %d:  ", index_1, index_2);

            cin>>matrix_val;

            arr[index_1][index_2] = matrix_val;

        }

    }


    // Print matrix
    for (auto index_1 = 0; index_1 < row ; index_1++) {

        for (auto index_2 = 0; index_2 < arr[index_1].size(); index_2++){

            cout<<arr[index_1][index_2]<<" ";

        }

        cout<<"\n";

    }

}

Output:

 Enter row size: 2
 Enter column size: 2
 Enter matrix value for position row: 0, column: 0:  1
 Enter matrix value for position row: 0, column: 1:  2
 Enter matrix value for position row: 1, column: 0:  3
 Enter matrix value for position row: 1, column: 1:  4
 Matrix is:

  1 2 
  3 4 

Method 3:

#include <iostream>

#include <vector>

#include <stdio.h>

using namespace std;


int main()
{

    int row, column;


    cout<<"Enter row size: ";

    cin>>row;


    cout<<"\nEnter column size: ";

    cin>>column;

    
    // Declear vector
    vector<vector<int> > arr(row);

    
    for (auto index_1 = 0; index_1 < row ; index_1++) {

        for (auto index_2 = 0; index_2 < column; index_2++){

            printf("\nEnter matrix value for position row: %d, column: %d:  ", index_1, index_2);

            int matrix_val;

            cin>>matrix_val;

            arr[index_1].push_back(matrix_val);

        }

    }


    // Print matrix
    for (auto index_1 = 0; index_1 < row ; index_1++) {

        for (auto index_2 = 0; index_2 < column; index_2++){

            cout<<arr[index_1][index_2]<<" ";

        }
 
       cout<<"\n";

    }

}

Output:

 Enter row size: 2
 Enter column size: 2
 Enter matrix value for position row: 0, column: 0:  1
 Enter matrix value for position row: 0, column: 1:  2
 Enter matrix value for position row: 1, column: 0:  3
 Enter matrix value for position row: 1, column: 1:  4
 Matrix is:

  1 2 
  3 4 

To learn more about golang, Please refer given below link.

https://techieindoor.com/go-lang-tutorial/

References:

https://en.wikipedia.org/wiki/C%2B%2B

Posted in C++

Leave a Reply

Your email address will not be published. Required fields are marked *