473,653 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic matrix

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

Nov 15 '05 #1
3 2918

<pa****@gmail.c om> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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(dou ble *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.co m 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.co m 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
1297
by: E G | last post by:
Hi, I have a class similar to this: class Matrix { private: float **_A; unsigned _rows,_cols; public:
6
2192
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 somebody enlighten me on how I would solve this? Here is what my prototype and my function look like: =====================================================
11
5209
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 = new double; }
4
4421
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 reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this possible? If yes, can anyone show me simple examples in C++
16
2128
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 routines I have developed the copy function gets called several hundreds of times, with very large data structures. This can take some time.... If you can't tell by the code the data structure is a matrix for storing complex numbers. Attached is...
60
10140
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: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is readily >implemented in template classes that can create dynamically allocated >multi-dimensional...
3
2457
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
7960
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
1
2154
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 left to right again. An error happens after the function recursively calls itself once and attempts to allocate dynamic memory for a sub-matrix. I'm not sure why this happens? Can anyone explain why an error occurs and how to correct it?...
0
8370
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8283
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.