Search
 
SCRIPT & CODE EXAMPLE
 

CPP

2-Dimensional array in c++

int row,col;
cout << "please input how many rows and columns you want accordingly: ";
cin>>row>>col;
//create array in heap.
int **arr=new int*[row];
for(int i=0;i<row;i++)
{
    arr[i]=new int[col];
}
//getting value from user.
for(int i=0;i<row;i++)
{
    for(int j=0;j<col;j++)
    {
        cout<<"Enter a number ";
        cin>>arr[i][j];
    }
}
//display Elements.
    for(int i=0;i<row;i++)
{
    for(int j=0;j<col;j++)
    {
        cout<<arr[i][j]<<" ";
    }
}

return 0;
Comment

2d array c++

int a[2][3]= {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    cout << a[1][1]; // Output is 5
Comment

2D vector in cpp

// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n = 3;
    int m = 4;
  
    /*
    We create a 2D vector containing "n"
    elements each having the value "vector<int> (m, 0)".
    "vector<int> (m, 0)" means a vector having "m"
    elements each of value "0".
    Here these elements are vectors.
    */
    vector<vector<int>> vec( n , vector<int> (m, 0)); 
  
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout<< endl;
    }
      
    return 0;
}
Comment

2D vector in cpp

// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n = 3;
    int m = 4;
  
    /*
    We create a 2D vector containing "n"
    elements each having the value "vector<int> (m, 0)".
    "vector<int> (m, 0)" means a vector having "m"
    elements each of value "0".
    Here these elements are vectors.
    */
    vector<vector<int>> vec( n , vector<int> (m, 0)); 
  
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout<< endl;
    }
      
    return 0;
}
Comment

2d array c++

//* 2D arrays
int n, m;
cout << "Enter row and column: ";
cin >> n >> m;
int arr[n][m];
for (int i = 0; i < n; i++)
{
  for (int j = 0; j < m; j++)
  {
    cin >> arr[i][j];
  }
}

cout << "
Showing array
";

for (int i = 0; i < n; i++)
{
  for (int j = 0; j < m; j++)
  {
    cout << arr[i][j] << " ";
  }
  cout << endl;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ how to add something at the start of a vector 
Cpp :: time of a loop in c++ 
Cpp :: int max c++ 
Cpp :: print a string with printf in c++ 
Cpp :: back() in c++ 
Cpp :: Accpt array input in single line in cpp 
Cpp :: sort vector struct c++ 
Cpp :: cpp string slice 
Cpp :: built in function in c++ for binary to decimal 
Cpp :: find the missing number 
Cpp :: how to reverse a string in c++ 
Cpp :: power of two c++ 
Cpp :: pointer in return function c++ 
Cpp :: c++ input 
Cpp :: string vector to string c++ 
Cpp :: C++ Limit of Integer 
Cpp :: c++ contains 
Cpp :: Search Insert Position leetcode solution in cpp 
Cpp :: descending order c++ 
Cpp :: c++ compile to exe command line 
Cpp :: How do I read computer current time in c++ 
Cpp :: c++ switch statement 
Cpp :: create matrix cpp 
Cpp :: c++ remove chars from string 
Cpp :: how to find even and odd numbers in c++ 
Cpp :: why do we use pointers in c++ 
Cpp :: stack class implementation to file unix-style in c++ 
Cpp :: cpp gui 
Cpp :: cpp oop 
Cpp :: set size of a vector c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =