Link to home
Start Free TrialLog in
Avatar of Simon Leung
Simon Leung

asked on

[C++] How to solve problem about allocating 2D array

In the following I would like to print an arrow base on user input. However, error prompts out even though it successfully print the arrow. Why is this happening? How can I solve it? 

#include<iostream>
using namespace std; int main() {    int length;    bool isValid = false;    while (isValid == false) {       cout << "Please input the length (integer): " << endl;       cin >> length;       if (length > 3) {          isValid = true;       }    }    char direction;    cout << "Do you want to print U/D/L/R?: " << endl;    cin >> direction;    if ((toupper(direction) != 'U') && (toupper(direction) != 'D') && (toupper(direction) != 'L') && (toupper(direction) != 'R')) {       cout << "Wrong unput!";    }    int size;    size = length * 0.5;    int ** position;    position = new int* [length];    for (int i = 0; i < length; i++) {       position[i] = new int[length];    }    for (int i = 1; i <= length; ++i) {       for (int j = 1; j <= length; ++j) {          position[i][j] = 0;       }    }    if (toupper(direction) == 'L') {       //upper arrow       for (int i = 0; i < size; ++i) {          position[1+i][size-i] = 1;       }       //middle       for (int j = 2; j <= length; ++j) {          position[size][j] = 1;       }       //below       for (int i = 1; i <= size-1; ++i) {             position[size+i][1+i] = 1;       }    }    if (toupper(direction) == 'R') {       //upper arrow       for (int i = size; i > 0; i--) {          //cout << i << endl;          position[i][size + 1 + i] = 1;       }       //middle       for (int i = 1; i < length; ++i) {          position[size][i] = 1;       }       //below       for (int i = 1; i <= size - 1; ++i) {          position[size + i][length - i] = 1;       }    }    if (toupper(direction) == 'D') {       //upper arrow       for (int i = size; i > 0; i--) {          //cout << i << endl;          position[size + 1 + i][i] = 1;       }       //middle       for (int i = 1; i < length; ++i) {          position[i][size] = 1;       }       //below       for (int i = 1; i <= size - 1; ++i) {          position[length - i][size + i] = 1;       }    }    if (toupper(direction) == 'U') {       //upper arrow       for (int i = 0; i < size; ++i) {          position[size - i][1 + i] = 1;       }       //middle       for (int j = 2; j <= length; ++j) {          position[j][size] = 1;       }       //below       for (int i = 1; i <= size - 1; ++i) {          position[1 + i][size + i] = 1;       }    }    //result    for (int i = 1; i <= length; ++i) {       for (int j = 1; j <= length; ++j) {          cout << "i " << i << endl;          cout << "j " << j << endl;          if (j == length) {             if (position[i][j] == 1) {                cout << "* " << endl;             }             else {                cout << "  " << endl;             }          }          else {             if (position[i][j] == 1) {                cout << "* ";             }             else {                cout << "  ";             }          }       }    }        return 0; }

Open in new window

Avatar of Boris Petrovic
Boris Petrovic
Flag of Croatia image

Hi Simon,
At a first glance, I can see that you are not using array indexes correctly:
    for (int i = 1; i <= length; ++i) {
        for (int j = 1; j <= length; ++j) {
            position[i][j] = 0;
        }
    }

Open in new window

Array's index starts at 0, and ends at length-1.
For example, if you have an array with 4 elements, you will access the elements from 0 to 3.
This is present multiple times in the code, for example:
for (int i = size; i > 0; i--)

Open in new window

You should start from size-1 and test for i >= 0.
Another thing you'll need to address is indexing into that array.

A two dimensional array created at compile time is different than a dynamic array.

int A[4][5];

That creates an array of 20 integers.  The compiler understands the dimensions of it and does some behind the scenes magic.  A[0][0] is the first element.  A[0][1] the second.  A[0][4] is the 5th element and the last item in the first row.  A[1][0] is the sixth element in memory.  The compiler translates the location of any element of A[x][y] as x*4+y.

In your two dimensional array the compiler has no idea how you intend to define or use the array so you'll need to compute the location of each element.

If you know the first dimension (number of rows) you can create a macro to do the math making coding easier and easier to read.

#define positionX(a,b) position[a*WIDTH][b]

Then instead of referring to position[x][y] refer to positionX(x,y)

Good Luck!
Kent
ASKER CERTIFIED SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial