Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointers and dynamic arrays

Reply
Thread Tools

pointers and dynamic arrays

 
 
repairman2003@gmail.com
Guest
Posts: n/a
 
      04-21-2007
I'm having some trouble with poitners and dynamic arrays (a matrix).


Given this function I have a few questions.

void func(int* mat, int rows, int columns, char* out)
{
...
}

My first question is in main, how is a matrix defined that can be sent
to this function?

I can do it like:

int** matrix;
matrix = new int*[numRows];
for(int i=0; i< numRows; i++)
matrix[i] = new int[numColumns];

and by sending func(matrix,numRows, numColumns, out). But the only
way I can get it to accept this is by changing the first argument to
accept int** instead of int*. It has to accept int* so I'm not quite
sure how to declare a matrix in main.

My next question is how can I set an element of the matrix to a
location of the char* array?

I can easily use cout<<mat[3][3]; in the func and it'll output that
certain element on to the screen, but I can't get it to go to an
element in the character array.

I know this would be a lot easier to do this with out pointers and
just using arrays and not poitners to arrays so don't tell me that...

Thanks

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      04-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm having some trouble with poitners and dynamic arrays (a matrix).
>
>
> Given this function I have a few questions.
>
> void func(int* mat, int rows, int columns, char* out)
> {
> ...
> }
>
> My first question is in main, how is a matrix defined that can be sent
> to this function?


That would probably be expecting mat as a contiguous array of rows...

i.e. element (i,j) is probably

mat[ i* columns + j ] or mat[ i + j * rows ]

The best way to do this is with a matrix class of which there are plenty
of examples on this NG.

>
> I can do it like:
>
> int** matrix;
> matrix = new int*[numRows];
> for(int i=0; i< numRows; i++)
> matrix[i] = new int[numColumns];


That's not unusual but probably not what is needed here.

>
> and by sending func(matrix,numRows, numColumns, out). But the only
> way I can get it to accept this is by changing the first argument to
> accept int** instead of int*. It has to accept int* so I'm not quite
> sure how to declare a matrix in main.
>
> My next question is how can I set an element of the matrix to a
> location of the char* array?
>
> I can easily use cout<<mat[3][3]; in the func and it'll output that
> certain element on to the screen, but I can't get it to go to an
> element in the character array.



cin >> mat[3][3]; // maybe ?

>
> I know this would be a lot easier to do this with out pointers and
> just using arrays and not poitners to arrays so don't tell me that...
>
> Thanks
>

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      04-21-2007
On Apr 21, 1:50 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> I'm having some trouble with poitners and dynamic arrays (a matrix).
>
> Given this function I have a few questions.
>
> void func(int* mat, int rows, int columns, char* out)
> {
> ...
>
> }
>
> My first question is in main, how is a matrix defined that can be sent
> to this function?
>
> I can do it like:
>
> int** matrix;
> matrix = new int*[numRows];
> for(int i=0; i< numRows; i++)
> matrix[i] = new int[numColumns];
>
> and by sending func(matrix,numRows, numColumns, out). But the only
> way I can get it to accept this is by changing the first argument to
> accept int** instead of int*. It has to accept int* so I'm not quite
> sure how to declare a matrix in main.
>
> My next question is how can I set an element of the matrix to a
> location of the char* array?
>
> I can easily use cout<<mat[3][3]; in the func and it'll output that
> certain element on to the screen, but I can't get it to go to an
> element in the character array.
>
> I know this would be a lot easier to do this with out pointers and
> just using arrays and not poitners to arrays so don't tell me that...
>
> Thanks


Write a matrix class, makes its a LOT easier and keeps your data
encapsulated.
You can now pass an instance of a matrix by reference.
You might consider a solution with std::vector instead of primitive
arrays.
This one has a rudimentary copy ctor, op(row, col), and out_of_bounds
checking.
type T could be anything, including a matrix.

The code needs work, probably will expose some bugs under scrutiny,
could also benefit from some std algorithms.

#include <iostream>
#include <ostream>
#include <stdexcept>

template< typename T,
const size_t Row,
const size_t Col >
class Matrix
{
T array[Row][Col];
public:
// ctor
Matrix() : array() { }
Matrix(const Matrix& copy)
{
for(size_t r = 0; r < Row; ++r)
{
for(size_t c = 0; c < Col; ++c)
{
array[r][c] = copy.array[r][c];
}
}
}
// member functions
void set(const size_t row, const size_t col, const T& t)
{
check_bounds(row, col);
array[row][col] = t;
}
T get(const size_t row, const size_t col) const
{
check_bounds(row, col);
return array[row][col];
}
T operator()(const size_t row, const size_t col)
{
return get(row, col);
}
private:
void check_bounds(const size_t row, const size_t col) const
{
if( ((0 > row) || !(Row > row)) || ((0 > col) || !(Col > col)) )
{
throw std::runtime_error("access out of bounds!");
}
}
};

int main()
{
Matrix< int, 10, 10 > matrix;
try {

std::cout << "matrix[2][2] = ";
std::cout << matrix.get(2, 2);
std::cout << std::endl;

matrix.set(2, 2, 99);
std::cout << "matrix[2][2] = ";
std::cout << matrix.get(2, 2);
std::cout << std::endl;

Matrix< int, 10, 10 > copy_matrix(matrix);
std::cout << "copy_matrix[2][2] = ";
std::cout << copy_matrix(2, 2); // op( )
std::cout << std::endl;

// errors...
// matrix.get(-1, -1);
// matrix.get(10, 9);
// matrix.get(0, 10);
// matrix.get(10, 10);

Matrix< double, 3, 12 > d_matrix;
Matrix< Matrix< int, 4, 4 >, 6, 8 > matrix_matrix;
}
catch (const std::exception& r_e) {
std::cout << "\nerror: ";
std::cout << r_e.what() << std::endl;
}
}

/*
matrix[2][2] = 0
matrix[2][2] = 99
copy_matrix[2][2] = 99
*/

___
You can provide default template parameters too.

template< typename T = int,
const size_t Row = 10,
const size_t Col = 10 >
class Matrix_10x10
{
...
};

 
Reply With Quote
 
repairman2003@gmail.com
Guest
Posts: n/a
 
      04-21-2007
I know, setting up a matrix class would be easier. That I can do with
no problem, I've set up classes for matrices, vectors, quarternions
and all that fun stuff in the past... They really didn't teach us
much on pointers in school. Everything was STL and data structures,
so all this older c stuff is pretty new to me. I understand the
concept of pointers and how to use them, just never did anything along
these lines. If I can figure out this example then I think everything
could click and I'd be fine.

The only information I had to work with was just what needs to be
passed into the function and what needs to be done with it. The
algorithm I did with ease, just stuck on the format it is supposed to
be in. I made an assumption that the matrix would follow the form of
mat[i][j] where i is the row, and j is the column. I'm not really too
worried with how it's sent, just a curiosity kind of thing.

As for the 2nd question, I guess my intention wasn't as clear as I
thought. I'm just trying to figure out how to append an element of
the matrix to the outbuffer, I came across itoa() a few minutes ago to
convert the element from an int to char but how do I get it to append
each individual element to the outbuffer and I'll need to append a
comma and a space after each one.

Thanks

 
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
casting pointers/arrays to multidimensional arrays Francesco C++ 2 11-06-2009 09:04 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments