473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Matrix/ fill with zeros.

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.
Nov 13 '05 #1
4 18827
On 5 Jul 2003 21:40:48 -0700, df******@aol.com (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
Nov 13 '05 #2
David <df******@aol.com> 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.

Nov 13 '05 #3
On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
David <df******@aol.com> 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>>
Nov 13 '05 #4
Barry Schwarz <sc******@deloz.net> wrote:
On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> 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.

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code...
8
by: nestini | last post by:
Hello everybody, I am student who just begin learning about programming I want to know what is Sprase Matrix. And would you please show me how to add the 2 Sprase Matrix in C soursecode.
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
0
by: Anthony Liu | last post by:
I am talking about the 'take' method of numarray. See here for more info: http://stsdas.stsci.edu/numarray/Doc/node33.html If I initialize a matrix 'mymatrix' by zeros((3,3),...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
3
by: jif20 | last post by:
Hello, basically i have a 2D matrix 'a'. It contains both data i want to use and data i want to ignore. e.g. 'a' matrix: 1.937145 -0.500000 -1.333333 0.000000 -0.103812 ...
1
by: Erik Lind | last post by:
I see a more complicated thread on a similar sounding question, but my question is simpler, I hope. I have a large numpy matrix, initially created as: Mat = zeros((a,b), int) and a smaller...
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.