Matrix/ fill with zeros.

D

David

Anyone,
Does anyonee now how to use the following outline to fill a matrix
with zeros and call on the first function in the following program?


void fill_with_zeros(int *mat){
mat=mat1=mat2=mat3;
return 0;
}

int main(void){
int mat;
mat= {0};
fill_with_zeros(*mat);
......
}

The first function is called on in main while mat is called into
fill_with_zeros. The syntax is killing me in my program. Also, the
matrix is a 3x3. If this isn't enough, I will just copy the entire
program on here next time I need to ask.

All I need the output to do is to print:
"About to fill Matrix1, Matrix2, and Matrix3 with zeros...
Matrix1:
0 0 0
0 0 0
0 0 0
Matrix2:
0 0 0
0 0 0
0 0 0
Matrix3:
0 0 0
0 0 0
0 0 0
"


I hope this is good enough.
 
R

Robert W Hand

void fill_with_zeros(int *mat){
mat=mat1=mat2=mat3;

Hard to know what to do with the above three undeclared identifiers.
mat is a pointer to int in this scope.
return 0;

The function returns void, i.e. nothing.
}

int main(void){
int mat;

mat is an int not a matrix. Do you want to declare a two-dimensional
array?
mat= {0};
fill_with_zeros(*mat);

These two expressions are approaching hopelessness. You are treating
mat as a pointer when it is an int. It is likely that you are
invoking undefined behavior.

Why not just write:

int mat[3][3] = {0};

mat will be an array of three arrays of three ints. It has
"matrix-like" form, although it certainly lacks the algebra of
matrices. All the elements of mat will be zeros.

If you need to refill the array with zeros, you will need to either
always use the same dimension (perhaps with a two defines for the
column and row number) or pass a size to the function for the number
of rows and columns.

HTH.

Best wishes,

Bob
 
K

Kevin Easton

David said:
Anyone,
Does anyonee now how to use the following outline to fill a matrix
with zeros and call on the first function in the following program?
[...]

The first function is called on in main while mat is called into
fill_with_zeros. The syntax is killing me in my program. Also, the
matrix is a 3x3. If this isn't enough, I will just copy the entire
program on here next time I need to ask.

If you want to create a 3x3 int matrix, you'd declare it as:

int m[3][3];

If you want to initialise it with all zeroes, this is enough:

int m[3][3] = { 0 };

If you want to write a function to fill a three by three matrix with
zeroes, you could do:

#include <string.h>

void fill_with_zeros(int (*m)[3][3])
{
static const int z[3][3] = { 0 };

memcpy(m, &z, sizeof z);
}

And call it on a matrix declared as above in the following way:

fill_with_zeros(&m);

- Kevin.
 
B

Barry Schwarz

David said:
Anyone,
Does anyonee now how to use the following outline to fill a matrix
with zeros and call on the first function in the following program?
[...]

The first function is called on in main while mat is called into
fill_with_zeros. The syntax is killing me in my program. Also, the
matrix is a 3x3. If this isn't enough, I will just copy the entire
program on here next time I need to ask.

If you want to create a 3x3 int matrix, you'd declare it as:

int m[3][3];

If you want to initialise it with all zeroes, this is enough:

int m[3][3] = { 0 };

If you want to write a function to fill a three by three matrix with
zeroes, you could do:

#include <string.h>

void fill_with_zeros(int (*m)[3][3])

There is no need to clutter the function and its calling statement
with an unnecessary level of indirection.

void fill_with_zeros(int m[3][3])
{
static const int z[3][3] = { 0 };

memcpy(m, &z, sizeof z);

memcpy(m, z, sizeof z);
}

And call it on a matrix declared as above in the following way:

fill_with_zeros(&m);

fill_with_zeros(m);



<<Remove the del for email>>
 
K

Kevin Easton

Barry Schwarz said:
On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
If you want to write a function to fill a three by three matrix with
zeroes, you could do:

#include <string.h>

void fill_with_zeros(int (*m)[3][3])

There is no need to clutter the function and its calling statement
with an unnecessary level of indirection.

void fill_with_zeros(int m[3][3])

Sure there is - my version allows the the compiler to check both the
number of rows and columns is correct, in yours only the columns.

And as you've shown, the only thing we pay for this is 4 characters in
the function definition and one in the function caller.

- Kevin.
 

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
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top