Matrix as an argument

  • Thread starter =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?=
  • Start date
?

=?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?=

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)
 
M

Malcolm McLean

Dinçay Akçören said:
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 = 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.
 
?

=?iso-8859-1?B?RGlu52F5IEFr5/ZyZW4=?=

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 = 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.


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[j] = arr[i+1][j];
if (i == b-1)
arr[j] = 0;
}
}
}

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

Malcolm McLean

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 = 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.


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[j] = arr[i+1][j];
if (i == b-1)
arr[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[j] accesses the i th row and
j th column of the matrix.
a and b have to be matrix height and width.
 
B

Barry Schwarz

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 = 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.


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[j] = arr[i+1][j];


When i = b-1, this invokes undefined behavior by access arr[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[j] = 0;
}
}
}

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



Remove del for email
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top