473,396 Members | 1,764 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,396 software developers and data experts.

Dynamic matrix

Hi all
How can I define a dynamic matrix and pass it to a function?

Nov 15 '05 #1
3 2912

<pa****@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi all
How can I define a dynamic matrix and pass it to a function?


/*
caller
*/

/* for the sake of argument, make the matrix 20 x 20 */
int N = 20;
double *ptr = malloc(N * N * sizeof(double));

setidentity(ptr, N);

/* do all sorts of wonderful thngs with your matrix here */
/* free after you have finished with it */
free(ptr);
/*
function - sets a square matrix to identity (all zero except the main
diagonal)
*/
void setidentity(double *mtx, int size)
{
int i;

for(i=0;i<size * size;i++)
mtx[i] = 0.0;
for(i=0;i<size;i++)
mtx[i*size+i] = 1.0;
}
Nov 15 '05 #2
pa****@gmail.com a écrit :
Hi all
How can I define a dynamic matrix and pass it to a function?


Use a structure to gather the relevent information.

struct mat2d
{
/* array of y pointers to arrays of x T */
T **p;
size_t x;
size_t y;
};

or the linear way :

struct mat2d
{
/* array of (y * x) T */
T *p;
size_t x;
size_t y;
};

and provide a function to access the data.

--
A+

Emmanuel Delahaye
Nov 15 '05 #3
On 13 Nov 2005 11:17:55 -0800, pa****@gmail.com wrote:
Hi all
How can I define a dynamic matrix and pass it to a function?


There are two common approaches. For N rows with M columns each:

One is to simulate the matrix with a one-dimensional array. You
allocate space for N*M objects, as with
T *ptr = malloc(N * M * sizeof *ptr);
and you reference the (i,j)th element by calculating the appropriate
subscript yourself, as in
ptr[i*M+j] = 0;

The other is to build an array of pointers, each pointing to one
row of the matrix, as in
T **ptr = malloc(N * sizeof *ptr);
for (k = 0; k < N; k++)
ptr[k] = malloc(M * sizeof *ptr[k]);
and you reference the (i,j)th element using the natural syntax, as in
ptr[i][j] = 0;

In either case, you pass the array to a function by using the ptr
variable. Note, since N and M are not known until run time:

Using the first approach always requires M to be available to the
function.

In either approach, the function will need both N and M if it is
concerned in any way with the top or right boundary elements of the
matrix.

The second approach is more flexible in that it can handle jagged
arrays where M is not constant for each row.
<<Remove the del for email>>
Nov 15 '05 #4

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

Similar topics

2
by: E G | last post by:
Hi, I have a class similar to this: class Matrix { private: float **_A; unsigned _rows,_cols; public:
6
by: fivelitermustang | last post by:
I have two matrices allocated dynamically in both directions: matrix x and matrix v. I want to pass these matrices into a function by reference. What I have written down isn't working... can...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
16
by: laclac01 | last post by:
I have developed my own copy function for coping my own dynamic memory structure. It works, but I feel its not too efficient. There must be a quicker way to copy the data. In some of the...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
3
by: repairman2003 | last post by:
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) { ... }
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
1
by: crimsonalucard | last post by:
Below is a function I wrote used to evaluate the determinant of a nxn matrix. arguments are an array of the matrix values in order going from left to right jumping to the next line then going from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.