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.

7
  • 2
    What do you think p = new int[x,y]; does? Commented Apr 26, 2012 at 14:40
  • 2
    You might want to have a look at stackoverflow.com/questions/936687/… Commented Apr 26, 2012 at 14:41
  • @als, p = new int[x,y] simply declares a two dimensional array Commented Apr 26, 2012 at 14:42
  • @user963499 Think again :) Commented Apr 26, 2012 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]. Commented Apr 26, 2012 at 14:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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 ?

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.