pointers and dynamic arrays

Discussion in 'C++' started by repairman2003@gmail.com, Apr 21, 2007.

  1. Guest

    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
    , Apr 21, 2007
    #1
    1. Advertising

  2. 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 = 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
    >
    Gianni Mariani, Apr 21, 2007
    #2
    1. Advertising

  3. Salt_Peter Guest

    On Apr 21, 1:50 am, ""
    <> 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 = 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
    {
    ...
    };
    Salt_Peter, Apr 21, 2007
    #3
  4. Guest

    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
    , Apr 21, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    374
  2. Replies:
    1
    Views:
    635
    -berlin.de
    Mar 28, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,110
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,089
    Francesco
    Nov 6, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page