Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Complete the functions for the attached program complex. c. The program adds,

ID: 3667649 • Letter: 2

Question

2. Complete the functions for the attached program complex. c. The program adds, subtracts, and multiplies complex numbers. The program will prompt the user for the choice of operations, read in the complex numbers, perform the operations, and print the results. The main program repeatedly prints the menu, reads in the user's selection, and performs the operation We use pointers only for those arguments that the function intends to change. In all the functions r represents the real component and i represents the imaginary component. Study the program and complete the following functions. Do not modify the function prototypes. 1) Complete the function that reads in two complex numbers. void read nums (double *rl, double *il, double r2, double *12); The function should prompt for the user to enter the real component and imaginary component of the first number and the second number and store the values in the variables pointed by r i 1, r2, i2, respectively 2) Complete the following functions that calculate the addition, subtraction, and multiplication of two complex numbers. The functions take real component and imaginary component of the first number and the second number and store the values in the variables pointed by r3 and 13, respectively void add (double rl, double il, double r2, double i2, double *r3, double *13); void subtract (double rl, double il, double r2, double i2, double *r3, double 13) void multiply (double rl double il, double r2 double i2, double *r3, double *13);

Explanation / Answer


/*****************************************************************/
/* This program adds, subtracts, or multiplies complex numbers. */
/* Project 5, Program Design                                     */
/*****************************************************************/
#include <stdio.h>
void read_numbers(double *r1, double *i1, double *r2, double *i2);
void add(double r1, double i1, double r2, double i2, double *r3, double *i3);
void subtract(double r1, double i1, double r2, double i2, double *r3, double *i3);
void multiply(double r1, double i1, double r2, double i2, double *r3, double *i3);
void print_complex(double r3, double i3);

int main(void)
{
        double r1, r2, r3, i1, i2, i3;
        int option;
      
        printf("Complex Number Arithmetic Program: ");
        for(;;)
        {
                printf("1. Add two complex numbers ");
                printf("2. Subtract two complex numbers ");
                printf("3. Multiply two complex numbers ");
                printf("4. Quit ");

                printf("Choose an option (1 - 4): ");
                scanf("%d", &option);
                switch(option){
                        case 1:
                                read_numbers(&r1, &i1, &r2, &i2);
                                add(r1, i1, r2, i2, &r3, &i3);
                                print_complex(r3, i3);
                                break;
                        case 2:
                                read_numbers(&r1, &i1, &r2, &i2);
                                subtract(r1, i1, r2, i2, &r3, &i3);
                                print_complex(r3, i3);
                                break;
                        case 3:
                                read_numbers(&r1, &i1, &r2, &i2);
                                multiply(r1, i1, r2, i2, &r3, &i3);
                                print_complex(r3, i3);
                                break;
                        case 4:
                                return 0;
                        default:
                                printf("Invalid option. Choose an option (1-4): ");

                }
        }

        return 0;
}

void read_numbers(double *r1, double *i1, double *r2, double *i2)
{

        //add your code here
   printf(" Enter First Complex Number ");
   printf(" Enter Real Part ");
   scanf("%lf", r1);     /* no need to specify & operator due to r1 already contain an address */
   printf(" Enter Imaginary Part ");
   scanf("%lf", i1);    /* no need to specify & operator due to i1 already contain an address */
   printf(" Enter Second Complex Number");
   printf(" Enter Real Part ");
   scanf("%lf", r2);   /* no need to specify & operator due to r2 already contain an address */
   printf(" Enter Imaginary Part ");
   scanf("%lf", i2);   /* no need to specify & operator due to i2 already contain an address */
}
void add(double r1, double i1, double r2, double i2, double *r3, double *i3)
{
        //add your code here
   *r3=r1+r2;  
   *i3=i1+i2;
}
void subtract(double r1, double i1, double r2, double i2, double *r3, double *i3)
{

        //add your code here
   *r3=r1-r2;
   *i3=i1-i2;

  
}
void multiply(double r1, double i1, double r2, double i2, double *r3, double *i3)
{
        //add your code here

   *r3 = r1*r2 - i1*i2;
         *i3 = i1*r2 + r1*i2;

}
void print_complex(double r3, double i3)
{
        if(i3 >= 0)
                printf(" The operation yields %.3f + %.3fi ", r3, i3);
        else
              
                printf(" The operation yields %.3f %.3fi ", r3, i3);
}