Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Help on code for dynamic matrix using pointers (http://www.velocityreviews.com/forums/t286308-help-on-code-for-dynamic-matrix-using-pointers.html)

 Josh 10-18-2004 07:01 AM

Help on code for dynamic matrix using pointers

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[i] = ((((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[s] = new int[matrixInfo[s+1]]; //allocating size of
pointer[s]

for (int w = 0; w <= matrixInfo[w+1]; w++){
ptr1[s][w] = rand()%size*2%8+2; //storing random values
cout << ptr1[s][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][i] << endl;
}
cout << endl;
}

 Nicolas Pavlidis 10-18-2004 08:36 AM

Re: Help on code for dynamic matrix using pointers

tool540@yahoo.com (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[i] = ((((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[s] = new int[matrixInfo[s+1]]; //allocating size of
> pointer[s]
>
> for (int w = 0; w <= matrixInfo[w+1]; w++){
> ptr1[s][w] = rand()%size*2%8+2; //storing random values
> cout << ptr1[s][w] <<endl;
> }
> cout << endl;
>
> }
>
> cout << endl;
>
> for(int j = 1; j <= size ; j++){

Here too!

Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pavnic@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|

 Gandu 10-19-2004 02:09 AM

Re: Help on code for dynamic matrix using pointers

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[i] = new int[numCol]; /* numCol is defined somewhere */
}

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

tool540@yahoo.com (Josh) wrote in message news:<3b2ebc2c.0410172301.6609fb7e@posting.google. com>...
> 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[i] = ((((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[s] = new int[matrixInfo[s+1]]; //allocating size of
> pointer[s]
>
> for (int w = 0; w <= matrixInfo[w+1]; w++){
> ptr1[s][w] = rand()%size*2%8+2; //storing random values
> cout << ptr1[s][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][i] << endl;
> }
> cout << endl;
> }

 ES Kim 10-19-2004 02:28 AM

Re: Help on code for dynamic matrix using pointers

"Gandu" <cpptutor2000@yahoo.com> wrote in message
> 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[i] = 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[i] = mat[0] + i * col;

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

--
ES Kim

 All times are GMT. The time now is 01:04 PM.