Help on code for dynamic matrix using pointers

J

Josh

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;
}
 
N

Nicolas Pavlidis

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
 
G

Gandu

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.
 
E

ES Kim

Gandu said:
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. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top