Help on code for dynamic matrix using pointers

Discussion in 'C++' started by Josh, Oct 18, 2004.

  1. Josh

    Josh Guest

    Howdy
    i was recently given a program to do. I have to create a 2d matrix
    with pointers i have the whole idea down with pointers but there is a
    problem with one of them i have the code written down at bottom so any
    help will help. The problem is that after it allocates memory for the
    array of pointers it seems like it loops an extra time, giving it one
    more element. Thanks

    int **ptr1, *matrixInfo;
    int size;

    cout << "Enter the number of columns.\n";
    cin >> size;

    ptr1 = new int*[size];
    matrixInfo = new int[size+1];

    matrixInfo[0] = size;

    for(int i = 1; i < size; i++){ //creates matrixinfo
    matrixInfo = ((((rand()%size)*2)%8)+2);
    }
    system("PAUSE");

    for(int k = 0; k < (size+1); k++){ //displays
    matrixinfo
    cout << matrixInfo[k];
    }

    for (int s = 0; s <= size; s++)
    {
    ptr1 = new int[matrixInfo[s+1]]; //allocating size of
    pointer

    for (int w = 0; w <= matrixInfo[w+1]; w++){
    ptr1[w] = rand()%size*2%8+2; //storing random values
    cout << ptr1[w] <<endl;
    }
    cout << endl;

    }

    cout << endl;

    for(int j = 1; j <= size ; j++){
    for(int i = 0; i <= matrixInfo[j]; i++){ //display values of
    ptr
    cout <<ptr1[j] << endl;
    }
    cout << endl;
    }
    Josh, Oct 18, 2004
    #1
    1. Advertising

  2. (Josh) writes:

    > Howdy
    > i was recently given a program to do. I have to create a 2d matrix
    > with pointers i have the whole idea down with pointers but there is a
    > problem with one of them i have the code written down at bottom so any
    > help will help. The problem is that after it allocates memory for the
    > array of pointers it seems like it loops an extra time, giving it one
    > more element. Thanks
    >
    > int **ptr1, *matrixInfo;
    > int size;
    >
    > cout << "Enter the number of columns.\n";
    > cin >> size;
    >
    > ptr1 = new int*[size];
    > matrixInfo = new int[size+1];
    > matrixInfo[0] = size;
    >
    > for(int i = 1; i < size; i++){ //creates matrixinfo
    > matrixInfo = ((((rand()%size)*2)%8)+2);
    > }
    > system("PAUSE");
    >
    > for(int k = 0; k < (size+1); k++){ //displays
    > matrixinfo
    > cout << matrixInfo[k];
    > }


    > for (int s = 0; s <= size; s++)


    Here, I think, is your problem, change the staement s <= size wich s <
    size.

    > {
    > ptr1 = new int[matrixInfo[s+1]]; //allocating size of
    > pointer
    >
    > for (int w = 0; w <= matrixInfo[w+1]; w++){
    > ptr1[w] = rand()%size*2%8+2; //storing random values
    > cout << ptr1[w] <<endl;
    > }
    > cout << endl;
    >
    > }
    >
    > cout << endl;
    >
    > for(int j = 1; j <= size ; j++){


    Here too!

    HTH & Kind regrads,
    Nicolas

    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
    Nicolas Pavlidis, Oct 18, 2004
    #2
    1. Advertising

  3. Josh

    Gandu Guest

    This is really very simple:
    To create the matrix, first declare a pointer to a pointer as follows:
    int** mat; /* for example */

    To actually create the matrix, i.e., allocate space, use:
    /* numRow is defined somewhere */
    mat = new int*[numRow]; /* create arrays of pointers to arrays */
    for(int i = 0; i < numRow; i++){
    mat = new int[numCol]; /* numCol is defined somewhere */
    }

    After this, a little bit of experimentation while provide the rest
    of the functionality.


    (Josh) wrote in message news:<>...
    > Howdy
    > i was recently given a program to do. I have to create a 2d matrix
    > with pointers i have the whole idea down with pointers but there is a
    > problem with one of them i have the code written down at bottom so any
    > help will help. The problem is that after it allocates memory for the
    > array of pointers it seems like it loops an extra time, giving it one
    > more element. Thanks
    >
    > int **ptr1, *matrixInfo;
    > int size;
    >
    > cout << "Enter the number of columns.\n";
    > cin >> size;
    >
    > ptr1 = new int*[size];
    > matrixInfo = new int[size+1];
    >
    > matrixInfo[0] = size;
    >
    > for(int i = 1; i < size; i++){ //creates matrixinfo
    > matrixInfo = ((((rand()%size)*2)%8)+2);
    > }
    > system("PAUSE");
    >
    > for(int k = 0; k < (size+1); k++){ //displays
    > matrixinfo
    > cout << matrixInfo[k];
    > }
    >
    > for (int s = 0; s <= size; s++)
    > {
    > ptr1 = new int[matrixInfo[s+1]]; //allocating size of
    > pointer
    >
    > for (int w = 0; w <= matrixInfo[w+1]; w++){
    > ptr1[w] = rand()%size*2%8+2; //storing random values
    > cout << ptr1[w] <<endl;
    > }
    > cout << endl;
    >
    > }
    >
    > cout << endl;
    >
    > for(int j = 1; j <= size ; j++){
    > for(int i = 0; i <= matrixInfo[j]; i++){ //display values of
    > ptr
    > cout <<ptr1[j] << endl;
    > }
    > cout << endl;
    > }
    Gandu, Oct 19, 2004
    #3
  4. Josh

    ES Kim Guest

    "Gandu" <> wrote in message
    news:...
    > This is really very simple:
    > To create the matrix, first declare a pointer to a pointer as follows:
    > int** mat; /* for example */
    >
    > To actually create the matrix, i.e., allocate space, use:
    > /* numRow is defined somewhere */
    > mat = new int*[numRow]; /* create arrays of pointers to arrays */
    > for(int i = 0; i < numRow; i++){
    > mat = new int[numCol]; /* numCol is defined somewhere */
    > }


    You can make it more efficient by allocating the whole memory once.

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

    But remember, you may want boost::multi_array and be happy. :)

    --
    ES Kim
    ES Kim, Oct 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,646
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    397
    Holgerson
    Oct 26, 2007
  3. Terry Reedy
    Replies:
    0
    Views:
    542
    Terry Reedy
    Apr 2, 2009
  4. Robert Kern
    Replies:
    0
    Views:
    580
    Robert Kern
    Apr 2, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    652
Loading...

Share This Page