Question

1. [20 pts] Please write the functions addComplex, multiplyComplex, compareComplex, and printComplex, whose declarations are
question should be solved in C language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

The main challenge in the given problem is returning 2 values from a single function (the real part and the imaginary part).

This can be done in several ways.

1.You can use an array as an argument in the function and update the array with the returning values in the function itself.

2.You can pass pointers of the variables (as arguments)which are made to store the returning values.

3. You can create a structure and return a structure object (which indeed can have several data type values which can be updated by a function)

Here is the code for the array method approach-

#include<stdio.h>
#include<math.h>

void addComplex(double real1,double img1, double real2, double img2,double sum[])
{
   sum[0]=real1+real2;
   sum[1]=img1+img2;
  
}

void multiplyComplex(double real1,double img1, double real2, double img2,double prod[])
{
   prod[0]=real1*real2-img1*img2;
   prod[1]=real1*img2+img1*real2;
  
}

int compareComplex(double real1,double img1, double real2, double img2)
{
   double a=sqrt(pow(real1,2)+pow(img1,2));
   double b=sqrt(pow(real2,2)+pow(img2,2));
   if(a>b)
   {
       return 1;
   }
   else if(a==b)
   {
       return 0;
   }
   else{
       return -1;
   }  
}

void printComplex(double real1,double img1)
{
   printf("The complex number is : %lf + i*%lf \n",real1,img1);
}

int main()
{
   double real1,real2,img1,img2;
   double sum[2];
   double prod[2];
   int comp;
   printf("Enter the real part of 1st number\n");
   scanf("%lf",&real1);
   printf("Enter the complex part of 1st number\n");
   scanf("%lf",&img1);
   printf("Enter the real part of 2nd number\n");
   scanf("%lf",&real2);
   printf("Enter the complex part of 2nd number\n");
   scanf("%lf",&img2);
   addComplex(real1,img1,real2,img2,sum);
   printf("The result after adding is:\n");
   printComplex(sum[0],sum[1]);
   multiplyComplex(real1,img1,real2,img2,prod);
   printComplex(prod[0],prod[1]);
   comp=compareComplex(real1,img1,real2,img2);
   if(comp==1)
   {
       printf("complex1 > complex2\n");
   }
   else if(comp==0)
   {
       printf("complex1 = complex2\n");
   }
   else{
       printf("complex1 < complex2");
   }
  
}

A sample run of the code looks as follows-

Enter the real part of 1st number Enter the complex part of 1st number 4 Enter the real part of 2nd number Enter the complex

In the structure procedure, we basically create a structure called complex which has 2 double data types named real and img which represent the real and imaginary part of a real number.

Just like the array approach , we pass the real and img. part values into the function but this time we first create 2 instances of these structures (which represent our 2 complex numbers ) and pass their real and img. data fields as arguments.

In the function itself we create another structure which represents our result value. THis structure is then returned by the function.

The return value is collected by another structure defined in Main.

The code looks like this-

#include<stdio.h>
#include<math.h>

struct complex{
   double real;
   double img;
};
struct complex addComplex(double real1,double img1, double real2, double img2)
{
   struct complex sum;
   sum.real=real1+real2;
   sum.img=img1+img2;
   return sum;
  
}
struct complex multiplyComplex(double real1,double img1, double real2, double img2)
{
   struct complex prod;
   prod.real=real1*real2-img1*img2;
   prod.img=real1*img2+img1*real2;
   return prod;
  
}
int compareComplex(double real1,double img1, double real2, double img2)
{
   double a=sqrt(pow(real1,2)+pow(img1,2));
   double b=sqrt(pow(real2,2)+pow(img2,2));
   if(a>b)
   {
       return 1;
   }
   else if(a==b)
   {
       return 0;
   }
   else{
       return -1;
   }  
}
void printComplex(double real1,double img1)
{
   printf("The complex number is : %lf + i*%lf \n",real1,img1);
}
int main()
{
   struct complex c1,c2;
   struct complex r_sum;
   struct complex r_prod;
   int comp;
  
   printf("Enter the real part of 1st number\n");
   scanf("%lf",&c1.real);
   printf("Enter the complex part of 1st number\n");
   scanf("%lf",&c1.img);
   printf("Enter the real part of 2nd number\n");
   scanf("%lf",&c2.real);
   printf("Enter the complex part of 2nd number\n");
   scanf("%lf",&c2.img);
  
   r_sum=addComplex(c1.real,c1.img,c2.real,c2.img);
   r_prod=multiplyComplex(c1.real,c1.img,c2.real,c2.img);
   printf("The result after adding is:\n");
   printComplex(r_sum.real,r_sum.img);
   printf("The result after multiplication is:\n");
   printComplex(r_prod.real,r_prod.img);
  
   comp=compareComplex(c1.real,c1.img,c2.real,c2.img);
   if(comp==1)
   {
       printf("complex1 > complex2\n");
   }
   else if(comp==0)
   {
       printf("complex1 = complex2\n");
   }
   else{
       printf("complex1 < complex2");
   }
  
  
}

A sample output of the code looks like-

Enter the real part of 1st number 1 Enter the complex part of 1st number 2 Enter the real part of 2nd number Enter the comple

Add a comment
Know the answer?
Add Answer to:
1. [20 pts] Please write the functions addComplex, multiplyComplex, compareComplex, and printComp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re,...

    need the code in .c format #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> struct _cplx double re, im; // the real and imaginary parts of a complex number }; typedef struct _cplx Complex; // Initializes a complex number from two real numbers Complex CmplxInit(double re, double im) Complex z = { re, im }; return z; // Prints a complex number to the screen void CmplxPrint(Complex z) // Not printing a newline allows this to be printed in the middle of...

  • Complete the functions for the program complex.c (Down) . The program adds, subtracts,and multiplies complex numbers....

    Complete the functions for the program complex.c (Down) . The program adds, subtracts,and multiplies complex numbers. The C program will prompt the user for the choice of operations,read in the complex numbers, perform the operations, and print the results. The main programrepeatedly 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 thefunctions, r represents the real component and i represents the imaginary...

  • Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an...

    Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an operator overloading function     ProblemSolution operator + (ProblemSolution const &P) which adds two ProblemSolution objects and returns a new ProblemSolution object. Input     12 -10      -34 38     where, Each row is a complex number. First element is real part and the second element is imaginary part of a complex number. Output     -22 28 Two complex numbers are 12-10i and -34+38i. Sum of complex numbers are =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT