doubles

R

robix

Hi all. i'm developing a simple program for matrix add/multiplication but i
don't know if i'm going the right path.
I'm supposed to receive doubles in the output of my program but instead i
receive: 6, 8, etc... Is this right or not? Shouldn't i receive 6.00000,
8.0000?
This is my first c program... i think it crashes at the end, though i'm not
quite sure(i'm using Dev c++/MingW). i know i need to release the memory but
i haven't learned that (yet). Maybe it's because of that? I guess it is...
Anyway, fell free to add more input about the program... what should i
change, etc.
Thanks

Here is the source code:


#include <stdio.h>
#include <stdlib.h>

/* matrix structure */
typedef struct {
int col;
int lin;
double *element;
} matrix;

/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));
M.lin = lin;
M.col = col;
return M;
}

/* add two matrix */
matrix add_matrix(matrix mat1, matrix mat2)
{
matrix M;
int a, b;
M = init_matrix(mat1.lin, mat1.col);
printf("Insert the matrix number 1\n");
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
scanf("%e", &mat1.element[a]);
}
printf("Insert the matrix number 2\n");
for (b = 0; b< (mat1.lin * mat1.col); b++)
{
scanf("%e", &mat2.element);
}
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
M.element[a] = mat1.element[a] + mat2.element[a];
}
M.lin = mat1.lin;
M.col = mat1.col;
return M;
}

/*mul two matrix */
matrix mul_matrix(matrix mat1, matrix mat2)
{
/* in development*/
}

/* prints the resulting matrix in screen*/
void show_matrix(matrix mat1)
{
int a, b;
printf("Matrix = \n");
for (a = 0; a< mat1.lin; a++)
{
for (b = 0; b< mat1.col; b++)
{
if (b == mat1.col - 1)
printf("%e\n", mat1.element[a * mat1.col + b]);
else
printf("%e, ", mat1.element[a * mat1.col + b]);
}
}
}

int main()
{
matrix m1, m2, result;
int esc = 0, tam1 = 0, tam2 = 0, tam3 = 0, tam4 = 0;
system("CLS");
do {
printf("1 - Add Matrix\n");
printf("2 - Mul matrix\n");
printf("3 - Exit\n");
scanf("%i", &esc);
fflush (stdin) ;
if ((esc == 1) || (esc == 2)){
printf("insert the number of lines for the first matrix?\n");
scanf("%i", &tam1);
printf("insert the number of colowns for the first matrix?\n");
scanf("%d", &tam2);
printf("insert the number of lines for the second matrix?\n");
scanf("%d", &tam3);
printf("insert the number of colowns for the second matrix??\n");
scanf("%d", &tam4);
if (esc == 1) {
/* codigo adição */
if ((tam1 ==tam3) && (tam2 == tam4)) {
m1 = init_matrix(tam1, tam2);
m2 = init_matrix(tam3, tam4);
result = init_matrix(tam1, tam2);
result = add_matrix(m1, m2);
show_matrix(result);
} else {
printf("can't add matrix!\n");
}
} else {
/* codigo multiplicação */
if (tam1 == tam4) {

} else {
printf("can't multiply matrix!\n");
}
};
};
} while (esc != 3);
system("PAUSE");
return 0;
}
 
E

E. Robert Tisdale

robix said:
Hi all. i'm developing a simple program for matrix add/multiplication but i
don't know if i'm going the right path.
I'm supposed to receive doubles in the output of my program but instead i
receive: 6, 8, etc... Is this right or not? Shouldn't i receive 6.00000,
8.0000?
This is my first c program... i think it crashes at the end, though i'm not
quite sure(i'm using Dev c++/MingW). i know i need to release the memory but
i haven't learned that (yet). Maybe it's because of that? I guess it is...
Anyway, fell free to add more input about the program... what should i
change, etc.

Take a look at The ANSI C Numerical Class Library at

http://www.netwood.net/~edwin/svmtl/

Also, take a look at GSL - The GNU Scientific Library

http://sources.redhat.com/gsl/

and The Vector, Signal and Image Processing Library

http://www.vsipl.org/
 
S

Sheldon Simms

/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));

This line has two problems, and one is a major problem. First,
you don't need to cast the return value of calloc in C. Second,
and more importantly, the amount of space you are requesting from
calloc is wrong. You are asking for lin * col elements, where
each element has the same size as M.element. But M.element is
a POINTER to double, not a double. Since you want the size of
a double, and M.element is a pointer to double, then *M.element
is a double, and you can use this expression in your call to
calloc:

M.element = calloc(lin * col, sizeof *M.element);
M.lin = lin;
M.col = col;
return M;
}

/* add two matrix */
matrix add_matrix(matrix mat1, matrix mat2)
{
matrix M;
int a, b;
M = init_matrix(mat1.lin, mat1.col);
printf("Insert the matrix number 1\n");
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
scanf("%e", &mat1.element[a]);

Here you are trying to store a value in a double, but you have
told scanf that it's a float. Change to:

scanf("%le", &mat1.element[a]);
}
printf("Insert the matrix number 2\n");
for (b = 0; b< (mat1.lin * mat1.col); b++)
{
scanf("%e", &mat2.element);


Same problem here.
}
for (a = 0; a< (mat1.lin * mat1.col); a++)
{
M.element[a] = mat1.element[a] + mat2.element[a];
}
M.lin = mat1.lin;
M.col = mat1.col;
return M;
}
[snip]

int main()
{
matrix m1, m2, result;
int esc = 0, tam1 = 0, tam2 = 0, tam3 = 0, tam4 = 0;
system("CLS");

This line isn't particularly helpful on my system. I just get the
message:

sh: line 1: CLS: command not found
do { [snip]
} while (esc != 3);
system("PAUSE");

This line also does nothing on my system.
 
A

Andreas Kahari

/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));

This line has two problems, and one is a major problem. First, [cut]

M.element = calloc(lin * col, sizeof *M.element);

Also, an all bits zero double may not be 0.0.

See this FAQ:

http://www.eskimo.com/~scs/C-faq/q7.31.html


[cut]
/* add two matrix */
matrix add_matrix(matrix mat1, matrix mat2) [cut]
scanf("%e", &mat1.element[a]);

Here you are trying to store a value in a double, but you have
told scanf that it's a float. Change to:

scanf("%le", &mat1.element[a]);

I'm wondering why a function that adds two matrices also does
the I/O. This should be moved into its own function.


[cut]
int main()
{ [cut]
return 0;
}

There's a lot of allocated memory not freed at the end of the
program.
 
S

Sheldon Simms

/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));

This line has two problems, and one is a major problem. First, [cut]

M.element = calloc(lin * col, sizeof *M.element);

Also, an all bits zero double may not be 0.0.
See this FAQ:

http://www.eskimo.com/~scs/C-faq/q7.31.html

Good point.

As far as I can tell however, the original program did not assume
that the array was filled with 0.0 values after allocation. Maybe
the original poster assumed that. I don't know.
 
G

Glen Herrmannsfeldt

Andreas Kahari said:
/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));

This line has two problems, and one is a major problem. First, [cut]

M.element = calloc(lin * col, sizeof *M.element);

Also, an all bits zero double may not be 0.0.

It is not required, but it is commonly done by hardware designers, anyway.

-- glen
 
P

Peter Nilsson

Sheldon Simms said:
On Wed, 12 Nov 2003 01:28:32 +0000, robix wrote:
/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));

This line has two problems, and one is a major problem. First, [cut]

M.element = calloc(lin * col, sizeof *M.element);

Also, an all bits zero double may not be 0.0.
See this FAQ:

http://www.eskimo.com/~scs/C-faq/q7.31.html

Good point.

As far as I can tell however, the original program did not assume
that the array was filled with 0.0 values after allocation. Maybe
the original poster assumed that. I don't know.

Well, if they *did* make that assumption, then the FAQ is applicable;
but if they *didn't*, then the calloc call is likely to suffer a
performance hit over a simple malloc call anyway. Either way, calloc
is not the best tool for the job.
 
K

Kelsey Bjarnason

[snips]

#include <stdio.h>
#include <stdlib.h>

/* matrix structure */
typedef struct {
int col;
int lin;
double *element;
} matrix;

/* allocates matrix space in memory*/
matrix init_matrix(int lin, int col)
{
matrix M;
M.element = (double *) calloc(lin * col, sizeof(M.element));

Casting *alloc. Bad. :)

Also, presumably you want to allocate lin * col _doubles_; however,
M.element is a _pointer_; taking its size is almost certainly the wrong
thing to do here.
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top