Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    #include <stdio.h>
    #include <math.h> /* needed for the sqrt and atan2 functions */
    #include <conio.h> /* (may be needed in future versions) */

    typedef struct{ /* create a new data type, called Cmplx */
    float re;
    float im;
    } Cmplx;

    #define PI 3.1415926 /* create a symbolic constant */

    /******** Prototypes for auxiliary functions ********/
    void cx_print(Cmplx v1); /* display the value of v1 */
    void cx_getnum(char *msg, Cmplx *gp); /* get a Cmplx number from keyboard */
    float cx_modu(Cmplx d1); /* return the modulus of d1 */
    float cx_argd(Cmplx d2); /* return the angle of d2 in degrees */
    void cx_mult(Cmplx m1, Cmplx m2, Cmplx *m3); /* multiply m1 by m2 */

    /********* Main program ********/

    int main(void)
    {
    Cmplx c1,c2,c3;
    float mod1, arg1;

    printf("Demonstration of complex multiplication \n");
    cx_getnum("Complex number 1: ", &c1); /* get c1 from user */
    mod1 = cx_modu(c1); /* find modulus of c1 */
    arg1 = cx_argd(c1); /* find angle of c1 */
    printf("Modulus = %f Angle = %f \n",mod1, arg1);
    cx_getnum("Complex number 2: ", &c2); /* get c2 from user */
    printf("Modulus = %f Angle = %f \n",cx_modu(c2),cx_argd(c2));

    cx_mult(c1, c2, &c3); /* form the product, put into c3 */
    printf("C1 * C2 = ");
    cx_print(c3);

    printf("Modulus = %f Angle = %f \n",cx_modu(c3),cx_argd(c3));
    return 0;
    }

    /******** Auxiliary (helper) functions *********/
    void cx_print(Cmplx v1) /* displays the value of v1 */
    {
    printf("%5.1f + j %5.1f \n", v1.re, v1.im);
    }
    void cx_getnum(char *msg, Cmplx *gp) /* gets a complex number from user */
    {
    printf("%s",msg);
    printf("Enter real part : ");
    scanf("%f",&(gp->re)); /* modify the external Cmplx num *gp */
    printf("Enter imaginary part : ");
    scanf("%f",&(gp->im));
    }

    float cx_modu(Cmplx d1) /* returns the modulus of d1 */
    {
    return sqrt((d1.re * d1.re)+ (d1.im * d1.im));
    }


    float cx_argd(Cmplx d2) /* returns the angle of d2, in degrees */
    {
    return (180.0 / PI) * atan2(d2.im, d2.re);
    }

    /* Forms the product of two complex numbers: */
    void cx_mult(Cmplx m1, Cmplx m2, Cmplx *m3) /* m3 = (m1 * m2) */
    {
    /* If m1 = a + jb, and m2 = c + jd, then: */
    /* product = (ac - bd) + j (ad + bc) */

    m3->re = (m1.re * m2.re) - (m1.im * m2.im); /* (ac - bd) */
    m3->im = (m1.re * m2.im) + (m1.im * m2.re); /* (ad + bc) */
    }


    That is the program regarding complex numbers but I dont understand the functions in main other than declaration.Could anyone please explain?
    Offline

    2
    ReputationRep:
    If you still need help, be more specific about where and I'll help explain it to you.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Will you be richer or poorer than your parents?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.