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 class in C++

2 Answers

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

using namespace std;

#define N 3

class calc
{
	private:
		int arr2d[N][N];
	public:
		void print();
		calc();
};
calc::calc()
{
	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;
		}
	}
}
void calc::print()
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
			cout << setw(4) << arr2d[i][j];
		cout << endl;
	}
}

int main()
{
	calc c;

	c.print();

	return 0;
}


/*
run:

4   7   8
2   3   7
3   1   8

*/

 



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

using namespace std;

#define N 3

class calc
{
	private:
		int arr2d[N][N] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
	public:
		void print();
};
void calc::print()
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
			cout << setw(4) << arr2d[i][j];
		cout << endl;
	}
}

int main()
{
	calc c;

	c.print();

	return 0;
}


/*
run:

1   2   3
4   5   6
7   8   9

*/

 



answered Mar 1, 2016 by avibootz
...