2

My goal is dynamically allocate 2 dimensional array such that it prompts the user to input the size of the row and column of the matrix array they want to create. After dynamically allocating the size of the rows and columns, the user will input the values of whatever they wish. The following is my C++ code:

#include <iostream>
using namespace std;

int main()
{

int* x = NULL;
int* y = NULL;
int numbers, row, col;
cout << "Please input the size of your rows: " << endl;
std::cin >> row;
cout << "Please input the size of your columns: " << endl;
std::cin >> col;
x = new int[row]; 
y = new int[col];
cout << "Please input your array values: " << endl;
for (int i = 0; i<row; i++)
{
    for (int j = 0; j<col; i++)
    {
        std::cin >> numbers; 
        x[i][j] = numbers;
    }
}

cout << "The following is your matrix: " << endl;
for (int i = 0; i < row; i++)
{
    for (int j = 0; j<col; j++)
    {
        std::cout << "[" << i << "][" <<j << "] = " << x[i][j] << std::endl;
    }
}

delete[] x;
delete[] y;
system("pause");
return 0;
}

Unfortunately, when I run this code on Visual Studios, it is giving me compile errors.

4
  • You want std::vector<std::vector<int>>. Commented Sep 20, 2017 at 1:20
  • x[ i ][ j ]? link Commented Sep 20, 2017 at 1:30
  • You have created two 1D arrays, and then tried to use them as though they were magically connected. Remove the user I/O parts of the code, and concentrate on learning how to construct a 2D array. Commented Sep 20, 2017 at 1:32
  • Possible duplicate of How do I declare a 2d array in C++ using new? Commented Sep 20, 2017 at 1:44

3 Answers 3

6

Here is how to allocate a 2D array (10 rows and 20 columns) dynamically using the c++11 new and delete operators

Code:

int main()
{

//Creation
int** a = new int*[10]; // Rows

for (int i = 0; i < 10; i++)
{
    a[i] = new int[20]; // Columns
}

//Now you can access the 2D array 'a' like this a[x][y]

//Destruction
for (int i = 0; i < 10; i++)
{
    delete[] a[i]; // Delete columns
}
delete[] a; // Delete Rows
return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

It should be delete[] a not delete a. Additionally I think you should have mentioned that using new and delete is obsolete and containers like std::vector or std::array should be prefered
thx Im editing it though In vs 2015 the memory view was looking valid after deallocation
That is really weird. What does it display when you replace all delete[]s with delete?
@Fureeish Since there is no destructor to call in this instance, both delete[] and delete work with Visual Studio, although using delete instead of delete[] here is Undefined Behavior according to the language standard.
@Fureeish My bad it was caused by the memory viewer not beeing able to automatically convert names to their actual data layout in the case of pointers, I just told visual studio to output the 10 pointer of "a', followed by the 20 pointers to values for each pointer of 'a' just like this in the watch Window : 'a,10,20'
2

I solved it:

#include <iostream>
//#include <vector>

using namespace std;

int main() {
int row, col;
cout << "Please enter the rows size: " << endl;
cin >> row;
cout << "Please enter the column size: " << endl;
cin >> col;

cout << "Please enter the numbers you want to put into a 2D array (it should 
look like a matrix graph)." << endl;
cout << "Press enter after each number you input: " << endl;

int** map = new int*[row];
for (int i = 0; i < row; ++i)
    map[i] = new int[col];

for (int i = 0; i < row; i++) {
    for (int j = 0; j < col; j++) {
        cin >> map[i][j];
    }

}

cout << endl;
//Print
for (int i = 0; i < row; i++) {
    for (int j = 0; j < col; j++) {
        cout << map[i][j] << " ";
    }
    cout << endl;

}
cout << endl;


// DON'T FORGET TO FREE
for (int i = 0; i < row; ++i) {
    delete[] map[i];
}
delete[] map;
system("pause");
return 0;
}

Comments

0
using namespace std;

int
main ()
{
  int sum = 0;
  int *row = new int (0);
  int *col = new int (0);

  cout << "enter rows" << endl;
  cin >> *row;
  cout << "enter column" << endl;
  cin >> *col;
  int *array = new int[*row][*col] for (int i = 0; i < (*row); i++)
    {
      for (int j = 0; j < (*col); j++)
    {
      cout << "enter element" << endl;
      cin >> *col;
      col++;
      sum = sum + (*col);
    }
      cout << " norm is " << sum;
      sum = 0;
      row++;
    }
  return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.