Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to declare, initialize and print two dimensional (2D) int array in C++

3 Answers

0 votes
#include <iostream>
#include <iomanip>

using namespace std;

#define N 3

class calc
{
  public:
	void print(int arr2d[][N]);
};
void calc::print(int arr2d[][N])
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
			cout << setw(4) << arr2d[i][j];
		cout << endl;
	}
}

int main()
{
	int arr2d[N][N] = { { 1, 8, 5 },{ 6, 9, 1 },{ 9, 7, 6 } };
	calc c;

	c.print(arr2d);

	return 0;
}


/*
run:

1   8   5
6   9   1
9   7   6

*/

 



answered Feb 29, 2016 by avibootz
0 votes
#include <iostream>
#include <iomanip>
#include <random>  

using namespace std;

#define N 3

class calc
{
  public:
	void print(int arr2d[][N]);
};
void calc::print(int arr2d[][N])
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
			cout << setw(4) << arr2d[i][j];
		cout << endl;
	}
}

int main()
{
	int arr2d[N][N];
	calc c;

	random_device rd;
	mt19937 random_generator(rd());
	uniform_int_distribution<int> uni(1, 9);

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			auto n = uni(random_generator); 
			arr2d[i][j] = n;
		}
	}

	c.print(arr2d);

	return 0;
}


/*
run:

2   2   3
2   7   5
6   9   7

*/

 



answered Feb 29, 2016 by avibootz
0 votes
#include <iostream>
#include <iomanip>

using namespace std;

#define N 3

class calc
{
  public:
	void print(int arr2d[][N]);
};
void calc::print(int arr2d[][N])
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
			cout << setw(4) << arr2d[i][j];
		cout << endl;
	}
}

int main()
{
	int arr2d[N][N]{ {0} };
	calc c;

	c.print(arr2d);

	return 0;
}


/*
run:

0   0   0
0   0   0
0   0   0

*/

 



answered Feb 29, 2016 by avibootz
...