0

i want the user to input values in a 2 dimensional array. he can also choose the size of each dimension

    int main()
{
    int x;
    int y;
    int *p;
    cout<<"How many items do you want to allocate in dimension x?"<<endl;
    cin>>x;
    cout<<"How many items do you want to allocate in dimension y?"<<endl;
    cin>>y;
    p = new int[x,y];
    for(int i=0; i<x; i++)    //This loops on the rows.
    {
        for(int j=0; j<y; j++) //This loops on the columns
        {
            int value;
            cout<<"Enter value: "<<endl;
            cin>>value;
            p[i,j] = value;
        }
    }
    drill1_4 obj;
    obj.CopyArray(p,x,y);

}

and then, ill output the two dimensional array via

class drill1_4
{
public:
    void CopyArray(int*,int,int);
private:
    int *p;
};

void drill1_4::CopyArray(int* a,int x,int y)
{
    p = a;
    for(int i=0; i<x; i++)    //This loops on the rows.
    {
        for(int j=0; j<y; j++) //This loops on the columns
        {
            cout << p[i,j]  << "  ";
        }
        cout << endl;
    }
    getch();
}

logic seems fine, but lets say, if the user enters the numbers, the array should look like this

1 2

3 4

but instead, it looks like this:

3 3

4 4

The array is not displaying it correctly.

vvavepacket
  • 1,882
  • 4
  • 25
  • 38
  • 2
    What do you think `p = new int[x,y];` does? – Alok Save Apr 26 '12 at 14:40
  • 2
    You might want to have a look at http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-using-new – sonicwave Apr 26 '12 at 14:41
  • @als, p = new int[x,y] simply declares a two dimensional array – vvavepacket Apr 26 '12 at 14:42
  • @user963499 [Think again :)](http://en.wikipedia.org/wiki/Comma_operator) – Sergey Kalinichenko Apr 26 '12 at 14:43
  • 7
    Let me be a bit more explicit that @Als: `new int[x,y]` almost certainly isn't doing what think or want. It does not get you a two-dimensional array. Likewise, where you use `p[i,j]` it's not doing what you think/want either. In both cases, you're using the comma operator, so `x,y` evaluates `x`, throws it away, then evaluates `y`, which becomes the result of the expression, so `p[x,y]` is equivalent to `p[y]`. – Jerry Coffin Apr 26 '12 at 14:44
  • that's right...it's effectively p = new int[y]; – ManiP Apr 26 '12 at 14:45
  • @sonicwave tnx! it solves my problem! – vvavepacket Apr 26 '12 at 14:49

1 Answers1

1

I don't know if you figured out the answer to your problem. The comments above tell you where you went wrong. Here is a possible answer.

#include <cstdio>

#include <iostream>
using namespace std;

class drill1_4
{
public:
    void CopyArray(int**,int,int);
private:
    int **p;
};

void drill1_4::CopyArray(int** a,int x,int y)
{
    p = a;
    for(int j=0; j<y; ++j)    //This loops on the rows.
    {
        for(int i=0; i<x; ++i) //This loops on the columns
        {
            cout << p[i][j]  << "  ";
        }
        cout << endl;
    }
    cin.get();
}


   int main()
{
    int x;
    int y;
    int **p;
    cout<<"How many items do you want to allocate in dimension x?"<<endl;
    cin>>x;
    cout<<"How many items do you want to allocate in dimension y?"<<endl;
    cin>>y;
    p = new int*[x];
    for (size_t i = 0; i < x; ++i)
        p[i] = new int[y];

    for(int j=0; j<y; ++j)    //This loops on the rows.
    {
        for(int i=0; i<x; ++i) //This loops on the columns
        {
            int value;
            cout<<"Enter value: "<<endl;
            cin>>value;
            p[i][j] = value;
        }
    }
    drill1_4 obj;
    obj.CopyArray(p,x,y);
}

I am not sure I understood what you meant by x-dimmension. If you meant x running horizontally than I think that i and j for-loops shout be reversed as x will represent columns.

Roman Kutlak
  • 2,684
  • 1
  • 19
  • 24
  • @Roman - In your code, Shouldn't the for loop >> p = new int*[y]; for (size_t i = 0; i < x; ++i) have i < y as the test condition ? – goldenmean Apr 28 '12 at 18:52