By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

Matrix as an argument

P: n/a
I need to pass a two dimensional matrix to a function. Function gets
int ** but i cannot find appropriate input to function in main.

in main
mdelc(&ar1, 5, 5, 3);

function prototype:
void mdelc(int **ar1, int a, int b, int d)

Sep 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Dinçay Akçören" <di****@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
>I need to pass a two dimensional matrix to a function. Function gets
int ** but i cannot find appropriate input to function in main.

in main
mdelc(&ar1, 5, 5, 3);

function prototype:
void mdelc(int **ar1, int a, int b, int d)
int **ar;

ar = malloc(height * sizeof(int *));
for(i=0;i<height;i++)
ar[i] = malloc(width * sizeof(int));

now you can use

ar[y][x] = a; to set up the matrix values

call
mcdecl(ar, 5, 5, 3)
no &. ar is already an int **.

That's probably what the function mdecl is expecting. However it is not
possible to be certain without more information. It might have different
requirements for setting up the matrix.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 1 '07 #2

P: n/a
On 2 Eylül, 00:38, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Dinçay Akçören" <din...@gmail.comwrote in message

news:11**********************@y42g2000hsy.googlegr oups.com...
I need to pass a two dimensional matrix to a function. Function gets
int ** but i cannot find appropriate input to function in main.
in main
mdelc(&ar1, 5, 5, 3);
function prototype:
void mdelc(int **ar1, int a, int b, int d)

int **ar;

ar = malloc(height * sizeof(int *));
for(i=0;i<height;i++)
ar[i] = malloc(width * sizeof(int));

now you can use

ar[y][x] = a; to set up the matrix values

call
mcdecl(ar, 5, 5, 3)
no &. ar is already an int **.

That's probably what the function mdecl is expecting. However it is not
possible to be certain without more information. It might have different
requirements for setting up the matrix.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
It doesn't work :(
Here is the function

void mdelr(int **arr, int a, int b, int d )

{
int i;
int j;

for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if (i >= d)
arr[i][j] = arr[i+1][j];
if (i == b-1)
arr[i][j] = 0;
}
}
}

arr is a*b matris, row d is deleted, then remaining part is shifted to
left.

Sep 1 '07 #3

P: n/a

"Dinçay Akçören" <di****@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On 2 Eylül, 00:38, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Dinçay Akçören" <din...@gmail.comwrote in message

news:11**********************@y42g2000hsy.googlegr oups.com...
I need to pass a two dimensional matrix to a function. Function gets
int ** but i cannot find appropriate input to function in main.
in main
mdelc(&ar1, 5, 5, 3);
function prototype:
void mdelc(int **ar1, int a, int b, int d)

int **ar;

ar = malloc(height * sizeof(int *));
for(i=0;i<height;i++)
ar[i] = malloc(width * sizeof(int));

now you can use

ar[y][x] = a; to set up the matrix values

call
mcdecl(ar, 5, 5, 3)
no &. ar is already an int **.

That's probably what the function mdecl is expecting. However it is not
possible to be certain without more information. It might have different
requirements for setting up the matrix.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
It doesn't work :(
Here is the function

void mdelr(int **arr, int a, int b, int d )

{
int i;
int j;

for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if (i >= d)
arr[i][j] = arr[i+1][j];
if (i == b-1)
arr[i][j] = 0;
}
}
}

arr is a*b matris, row d is deleted, then remaining part is shifted to
left.
>>>>>>>>>
It should work as I described.
arr is a pointer to a list of pointers. arr[i][j] accesses the i th row and
j th column of the matrix.
a and b have to be matrix height and width.
Sep 2 '07 #4

P: n/a
On Sat, 01 Sep 2007 15:38:29 -0700, "Dinçay Akçören"
<di****@gmail.comwrote:
>On 2 Eylül, 00:38, "Malcolm McLean" <regniz...@btinternet.comwrote:
>"Dinçay Akçören" <din...@gmail.comwrote in message

news:11**********************@y42g2000hsy.googleg roups.com...
>I need to pass a two dimensional matrix to a function. Function gets
int ** but i cannot find appropriate input to function in main.
in main
mdelc(&ar1, 5, 5, 3);
function prototype:
void mdelc(int **ar1, int a, int b, int d)

int **ar;

ar = malloc(height * sizeof(int *));
for(i=0;i<height;i++)
ar[i] = malloc(width * sizeof(int));

now you can use

ar[y][x] = a; to set up the matrix values

call
mcdecl(ar, 5, 5, 3)
no &. ar is already an int **.

That's probably what the function mdecl is expecting. However it is not
possible to be certain without more information. It might have different
requirements for setting up the matrix.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

It doesn't work :(
Describe doesn't work. Are you getting compiler or linker errors? Are
the results different than what you expect? How.
>Here is the function
Show the code that calls the function. Preferably a compilable unit.
>
void mdelr(int **arr, int a, int b, int d )

{
int i;
int j;

for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if (i >= d)
arr[i][j] = arr[i+1][j];
When i = b-1, this invokes undefined behavior by access arr[b][j],
non-existent elements of the array.

You could reverse these two if blocks and make the test on d an else
if.
if (i == b-1)
arr[i][j] = 0;
}
}
}

arr is a*b matris, row d is deleted, then remaining part is shifted to
left.

Remove del for email
Sep 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.