473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Apr 21 '07 #1
3 2448
re***********@gmail.com 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
Apr 21 '07 #2
On Apr 21, 1:50 am, "repairman2...@gmail.com"
<repairman2...@gmail.comwrote:
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
{
...
};

Apr 21 '07 #3
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

Apr 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
4
by: Konstantin Shemyak | last post by:
I have a big structure tree. All leaves are scalar values (no pointers). Present are arrays, structures and unions. I want to be able to store/read the content of the structure in/from a file, and...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: buda | last post by:
Hi, given the following code, .... int main( void ) { char *a = { "abc", "def", "ghijkl", "o", "prs" }; // for example .... }
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
2
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
6
by: nembo kid | last post by:
I have these classes: Boss, CommissionWorker, PieceWorker, HourlyWorker all directly derived from Employeer. I create a dynamic object (on heap) for each class:
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.