Velocity Reviews > Matrix/ fill with zeros.

# Matrix/ fill with zeros.

David
Guest
Posts: n/a

 07-06-2003
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.

Robert W Hand
Guest
Posts: n/a

 07-06-2003
On 5 Jul 2003 21:40:48 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (David) wrote:

>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

Kevin Easton
Guest
Posts: n/a

 07-06-2003
David <(E-Mail Removed)> wrote:
> 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.

Barry Schwarz
Guest
Posts: n/a

 07-06-2003
On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
<(E-Mail Removed)> wrote:

>David <(E-Mail Removed)> wrote:
>> 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);

>
> - Kevin.

<<Remove the del for email>>

Kevin Easton
Guest
Posts: n/a

 07-06-2003
Barry Schwarz <(E-Mail Removed)> wrote:
> On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
> <(E-Mail Removed)> wrote:

[...]
>>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.