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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jack black
  • 195
  • 1
  • 2
  • 11
  • You want `std::vector>`. – Jarod42 Sep 20 '17 at 01:20
  • x[ i ][ j ]? [link](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Manvir Sep 20 '17 at 01: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. – Beta Sep 20 '17 at 01:32
  • Possible duplicate of [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – 1201ProgramAlarm Sep 20 '17 at 01:44

3 Answers3

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;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0xd34dc0de
  • 493
  • 4
  • 10
  • 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 – Fureeish Sep 20 '17 at 01:33
  • thx Im editing it though In vs 2015 the memory view was looking valid after deallocation – 0xd34dc0de Sep 20 '17 at 01:36
  • That is really weird. What does it display when you replace all `delete[]`s with `delete`? – Fureeish Sep 20 '17 at 01:40
  • @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. – 1201ProgramAlarm Sep 20 '17 at 01:49
  • @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' – 0xd34dc0de Sep 20 '17 at 01:50
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;
}
jack black
  • 195
  • 1
  • 2
  • 11
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;
}