473,396 Members | 1,773 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,396 software developers and data experts.

doubles

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[b]);
}
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;
}
Nov 13 '05 #1
7 1711
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,
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[b]);
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.
return 0;
}


Nov 13 '05 #2
In article <pa****************************@yahoo.com>, Sheldon Simms wrote:
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
[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.

--
Andreas Kähäri
Nov 13 '05 #3
robix wrote:
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/

Nov 13 '05 #4
On Wed, 12 Nov 2003 17:25:34 +0000, Andreas Kahari wrote:
In article <pa****************************@yahoo.com>, Sheldon Simms wrote:
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.

Nov 13 '05 #5

"Andreas Kahari" <ak*******@freeshell.org> wrote in message
news:sl**********************@mx.freeshell.org...
In article <pa****************************@yahoo.com>, Sheldon Simms

wrote:
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.


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

-- glen
Nov 13 '05 #6
Sheldon Simms <sh**********@yahoo.com> wrote in message news:<pa****************************@yahoo.com>...
On Wed, 12 Nov 2003 17:25:34 +0000, Andreas Kahari wrote:
In article <pa****************************@yahoo.com>, Sheldon Simms wrote:
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.

--
Peter
Nov 13 '05 #7
[snips]

On Wed, 12 Nov 2003 01:28:32 +0000, robix wrote:
#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.
Nov 13 '05 #8

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

Similar topics

7
by: Daniel Lidström | last post by:
Hi, I'm currently using this method to extract doubles from a string: System::String* sp = S" "; System::String* tokens = s->Trim()->Split(sp->ToCharArray()); m_Northing =...
43
by: J.K. Becker | last post by:
Hi there, I am trying to multiply doubles with floats (actually I tried every possible combination by now) and it never works (well, it does something but it is always wrong). I have no idea...
14
by: my.correo.basura | last post by:
Yesterday I found in a piece of code a float being incremented with ++. For some reason (wrong, I guess...) I thought that only integer types could be incremented that way, and I had never seen...
25
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able...
5
by: Tales Normando | last post by:
The title says it all. Anyone?
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
27
by: brad | last post by:
Does anyone else feel that unittesting is too much work? Not in general, just the official unittest module for small to medium sized projects? It seems easier to write some quick methods that are...
5
by: =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post by:
Hi, I developed a custom Matrix class derived from custom VC++ doubles Array class derived from a RealArray which is defined as: typedef CArray<double,doubleRealArray; Now I need to convert...
2
by: utab | last post by:
Dear all, On a formatted printing operation that I am trying to accomplish for doubles, I have a question. I would like to print a double, d, and its square, d*d, but I would like to make the...
48
by: Bill Cunningham | last post by:
Is this valid C syntax ? double=double/int; I seem to be having trouble here. Bill
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.