# Help on code for dynamic matrix using pointers

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

1. ### JoshGuest

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

2. ### Nicolas PavlidisGuest

(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

3. ### GanduGuest

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
4. ### ES KimGuest

"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