doubles

Discussion in 'C Programming' started by robix, Nov 12, 2003.

  1. robix

    robix Guest

    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;
    }
    robix, Nov 12, 2003
    #1
    1. Advertising

  2. 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/
    E. Robert Tisdale, Nov 12, 2003
    #2
    1. Advertising

  3. 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);


    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;
    > }
    Sheldon Simms, Nov 12, 2003
    #3
  4. In article <>, 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
    Andreas Kahari, Nov 12, 2003
    #4
  5. On Wed, 12 Nov 2003 17:25:34 +0000, Andreas Kahari wrote:

    > In article <>, 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.
    Sheldon Simms, Nov 12, 2003
    #5
  6. "Andreas Kahari" <> wrote in message
    news:...
    > In article <>, 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
    Glen Herrmannsfeldt, Nov 12, 2003
    #6
  7. Sheldon Simms <> wrote in message news:<>...
    > On Wed, 12 Nov 2003 17:25:34 +0000, Andreas Kahari wrote:
    >
    > > In article <>, 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
    Peter Nilsson, Nov 13, 2003
    #7
  8. [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.
    Kelsey Bjarnason, Nov 14, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kloofy
    Replies:
    0
    Views:
    317
    Kloofy
    Jul 8, 2005
  2. Rick
    Replies:
    3
    Views:
    1,055
    Chris Smith
    Oct 8, 2003
  3. Loek Raemakers

    Apache-Jserv: Servlet doubles info

    Loek Raemakers, Oct 21, 2004, in forum: Java
    Replies:
    0
    Views:
    310
    Loek Raemakers
    Oct 21, 2004
  4. Paul Morrison

    doubles and integers

    Paul Morrison, Jan 4, 2005, in forum: Java
    Replies:
    2
    Views:
    349
    Ryan Stewart
    Jan 4, 2005
  5. dan
    Replies:
    1
    Views:
    2,304
    Jack Klein
    Nov 26, 2003
Loading...

Share This Page