pointers and dynamic arrays

R

repairman2003

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 = 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
 
G

Gianni Mariani

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 = 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 ?
 
S

Salt_Peter

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 = 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
{
...
};
 
R

repairman2003

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[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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top