Thread: about complex functions in C

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    Smile about complex functions in C

    i was wandering which one is better?
    one with a defined
    Code:
    typedef struct
    {
    double re;
    double im;
    } complex;
    or just a simple scanf printf function without any structures?
    may i have ur opinion?
    because i want to know which is best and why please.newbie here

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    struct is a datatype, not a function.

    You could use the struct to store data from scanf and output it with printf. Explain yourself further.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Standard C does have an complex number data type, by the way.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well basically i want to do a coding about complex numbers calculator..
    i think my struct that i defined there is good enough for the coding right..
    now i would like to know at the end.the typedef name of complex;
    what's use is it?is it compulsory for me to use it or not?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'm not sure what your question is.

    You don't have to use the "typedef" keyword when declaring a struct; but if you don't, you have to use "struct" whenever you create an instance of that structure. For example:
    Code:
    struct something {
        int x, y;
    };
    
    struct something one, two;
    
    typedef struct {
        int a, b;
    } else;
    
    else this, that;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by master5001 View Post
    Standard C does have an complex number data type, by the way.
    That was true with the 1989 C standard, but no longer. The 1999 C standard specifies a <complex.h> which does support complex types.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    @dwk no no, else is a keyword.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well assume i follow dwk's method..
    changing my coding from typedef struct to struct complex

    now i got errors when i want to convert my codes here...
    can anyone help me if i want to change the first to the second in my following codes
    Code:
    #include<stdio.h>
    #include<math.h>
    typedef struct{double r;
    double i;} COMPLEX;
    COMPLEX c_add(COMPLEX za, COMPLEX zb);
    
    main()
    {
    	COMPLEX z1, z2, z3, *pz;
    /******************************************************************
    
    * Get two complex numbers *
    ******************************************************************/
    
    printf("enter the values of z1 and z2:\n z1?:\n");
    scanf("%lf%lf",&z1.r, &z1.i);
    printf("z2:\n");
    scanf("%lf%lf",&z2.r,&z2.i);
    
    pz = &z3;
    *pz = c_add(z1,z2);
    printf("%lf + %lfi\n",pz->r,pz->i);
    }
    /****************************************************************** 
    
    * Returns sum of complex numbers za and zb 
    ******************************************************************/
    COMPLEX c_add(COMPLEX za, COMPLEX zb)
    { 
    
    COMPLEX sum; 
    
    sum.r = za.r + zb.r;
    sum.i = za.i + zb.i;
    return (sum);
    
    }

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    @dwk no no, else is a keyword.
    Why, so it is. Oops.

    Code:
    printf("&#37;lf + %lfi\n",pz->r,pz->i);
    printf() uses %f for both floats and doubles (unlike scanf()). printf("%lf") doesn't necessarily do anything.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I corrected some code that had 'default' as a parameter name the other day. Annoyingly the code had been compiling anyway as it was an unused template function. YAY VISUAL STUDIO
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just do search-and-destroy turning "COMPLEX" into "struct complex" or whatever you called it. There's no actual work involved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. 2 am complex double conversion woes
    By Roule in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2004, 02:53 PM
  5. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM