Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help on code for dynamic matrix using pointers

Reply
Thread Tools

Help on code for dynamic matrix using pointers

 
 
Josh
Guest
Posts: n/a
 
      10-18-2004
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)%+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;
}
 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      10-18-2004
(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)%+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!

HTH & Kind regrads,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
 
Reply With Quote
 
 
 
 
Gandu
Guest
Posts: n/a
 
      10-19-2004
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.


(Josh) wrote in message news:<. 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)%+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;
> }

 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      10-19-2004
"Gandu" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Re: Matrix operations on character matrix element? Terry Reedy Python 0 04-02-2009 12:12 AM
Matrix*Vector and Vector*Matrix Holgerson C++ 3 10-26-2007 07:38 AM
Matrix composed by two matrix lvcargnini VHDL 3 07-05-2006 07:21 AM
Re: Matrix DTS and Matrix 2 DTS? PeterTHX DVD Video 0 08-03-2003 05:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57