Connecting Tech Pros Worldwide Help | Site Map

pointers and dynamic arrays

 
LinkBack Thread Tools Search this Thread
  #1  
Old April 21st, 2007, 05:55 AM
repairman2003@gmail.com
Guest
 
Posts: n/a
Default pointers and dynamic arrays

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


  #2  
Old April 21st, 2007, 07:05 AM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: pointers and dynamic arrays

repairman2003@gmail.com wrote:
Quote:
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.
Quote:
>
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.
Quote:
>
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 ?
Quote:
>
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
>
  #3  
Old April 21st, 2007, 06:05 PM
Salt_Peter
Guest
 
Posts: n/a
Default Re: pointers and dynamic arrays

On Apr 21, 1:50 am, "repairman2...@gmail.com"
<repairman2...@gmail.comwrote:
Quote:
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
{
...
};

  #4  
Old April 21st, 2007, 06:15 PM
repairman2003@gmail.com
Guest
 
Posts: n/a
Default Re: pointers and dynamic arrays

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.