Answers for "how to print a 2d array in c++"

C++
1

print 2d array in c

// most of the time I forget that there should be matrix[i][j], not matrix[i]
#include <stdio.h>
// Abdullah Miraz
int main(){
    int i, j;

    int matrix[2][3] = {{2,3,4,5}, {7,8,9,1}};
    for(i=0;i< 2 ; i++){
        for(j=0;j<3;j++){
            printf("%d ", matrix[i][j]);
        }
    }
}
Posted by: Guest on July-30-2021
0

print 2d array c++

for( auto &row : arr) {
    for(auto col : row)
         cout << col << " ";
	cout<<endl; 
}
Posted by: Guest on April-04-2021
0

print a 2d vector in c++

// A recursive function able to print a vector
// of an arbitrary amount of dimensions.
template<typename T>
static void show(T vec)
{
  std::cout << vec;
}


template<typename T>
static void show(std::vector<T> vec)
{
  int size = vec.size();
  if (size <= 0) {
    std::cout << "invalid vector";
    return;
  }
  std::cout << '{';
  for (int l = 0; l < size - 1; l++) {
    show(vec[l]);
    std::cout << ',';
  }
  show(vec[size - 1]);
  std::cout << '}';
}
Posted by: Guest on March-23-2021

Browse Popular Code Answers by Language