What is the problem with 2d array in C++ code?

Hello,

I wrote the below code and I ran the code in "Code Blocks" IDE.

if I enter m = 2 and n = 3 , the program cannot get 6 element. I attached an image.

https://i.ibb.co/r3rzPY6/Screenshot-2022-01-18-081053.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

int main()
{   int m, n;
	int a[m][n],b[m][n],c[m][n];
	cout<<"Enter m(row) & n(col) : ";
	cin>>m>>n;
	
	for(int i=0;i<m;i++)
	{for(int j = 0;j<n;j++)
	{cout<<"Enter a("<<i<<","<<j<<") = ";
		cin>>a[i][j];
	}}
     cout <<"________________";
   	for(int i=0;i<m;i++)
	{for(int j = 0;j<n;j++)
	{cout<<"Enter b("<<i<<","<<j<<") = ";
		cin>>b[i][j];
	}}
	 cout <<"________________";
	for(int i=0;i<m;i++)
	{for(int j = 0;j<n;j++)
	{
	c[i][j] = a[i][j] + b[i][j];
    }}
	
	for(int i = 0;i < m;i++)
	{
    for(int j = 0;j < n;j++)
    {
    	cout<<c[i][j]<<setw(3);
	}   cout <<endl;
	}
	
	return 0;
	}	
1. That is not legal C++. In C++, the size of an array must be defined using a compile-time constant.

2. Even if you're using some extension of C++ that allows variable-length arrays, you're defining the arrays before the user inputs the values. At line 6, m and n haven't had any values specified, so this is undefined behaviour.
If'n you want a variable sized 2D container the 'easiest' way to achieve this is using std::vector. Instantiating the 2D std::vector after getting the dimension(s) from the user.

Along with a couple of std::ostream::operator<< overloads displaying 1D and 2D std::vectors becomes as easy as displaying Plain Old Data like an int or double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <numeric>

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v);

int main()
{
   // creating a sized 1Dvector
   std::vector<int> a1DVector(5);

   std::cout << "Displaying a sized 1D vector:\n";
   std::cout << a1DVector.size() << '\n' << a1DVector << "\n\n";

   std::cout << "Creating a 2-dimensional vector, enter row size: ";
   int row_size { };
   std::cin >> row_size;

   std::cout << "Enter column size: ";
   int col_size { };
   std::cin >> col_size;
   std::cout << "\n";

   // create a 2 dimensional int vector with known dimensions
   std::vector<std::vector<int>> a2DVector(row_size, std::vector<int>(col_size));

   // let's display the 2D vector
   std::cout << "Displaying the 2D vector:\n";
   std::cout << a2DVector;
}

// displays the contents of a 1D or inner vector of a 2D vector
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

// displays the a 2D vector, which calls the preceding function
// to print the indivual inner vector.
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
   for (auto const& x : v)
   {
      os << x << '\n';
   }

   return os;
}


With a 2D std::vector accessing individual elements is the same as a regular array, using operator[]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>

int main()
{
   std::cout << "Creating a 2-dimensional vector, enter row size: ";
   int row_size { };
   std::cin >> row_size;

   std::cout << "Enter column size: ";
   int col_size { };
   std::cin >> col_size;
   std::cout << "\n";

   // create a 2 dimensional int vector with known dimensions
   std::vector<std::vector<int>> a2DVector(row_size, std::vector<int>(col_size));

   for (size_t row { 0 }; row < row_size; ++row)
   {
      for (size_t col { 0 }; col < col_size; ++col)
      {
         std::cout << a2DVector[row][col] << ' ';
      }
      std::cout << '\n';
   }
}
Topic archived. No new replies allowed.