Question

In: Computer Science

Modify the Complex structure in the notes and create a method that returns the complex conjugate....

Modify the Complex structure in the notes and create a method that returns the complex conjugate.

// Structure Method Notes C++

#include
using namespace std;

// Object Definition
struct Complex {

   // Data members that define a complex number a + bi
   double a, b;

   // function within a data structure = METHOD
   // functions specific to this type of object.
   void printComplex(void);

};

int main(void) {

   int size = 3;

   // Declare three Complex objects and intitialize the data members.
   // In other words, instantiate 3 complex objects.
   Complex z1 = { 3,4 };
   Complex z2 = { 5,0 };
   Complex z3;
  

   z3.a = 6;
   z3.b = -8;

   // Use the given method to print the three objects.
   // Need three function calls.

   z1.printComplex();
   z2.printComplex();
   z3.printComplex();


   // In class we will create a function that calculates and returns
   // the distance from the origin.

   // If time... change to an array of 3 Complex objects.


   return(0);
}

//Function Implementations
// use the scope-resolution operator ( :: ) to tie the function to the object definition
void Complex::printComplex(void) {

   if (b >= 0)
       cout << a << " + " << b << "i" << endl;
   else
       cout << a << " - " << -1 * b << "i" << endl;

}

Solutions

Expert Solution

Code:

// Structure Method Notes C++
#include <iostream>
using namespace std;

// Object Definition
struct Complex {

        // Data members that define a complex number a + bi
        double a, b;

        // function within a data structure = METHOD
        // functions specific to this type of object.
        void printComplex(void);
        // function to return complex conjugate
        Complex complexConjugate(void);

};

int main(void) {

        int size = 3;

        // Declare three Complex objects and intitialize the data members.
        // In other words, instantiate 3 complex objects.
        Complex z1 = { 3,4 };
        Complex z2 = { 5,0 };
        Complex z3;


        z3.a = 6;
        z3.b = -8;

        // Use the given method to print the three objects.
        // Need three function calls.

        z1.printComplex();
        z2.printComplex();
        z3.printComplex();

        // check out complex conjugate
        cout<<"Printing Complex Conjugate:"<<endl;
        Complex z_1 = z1.complexConjugate();
        Complex z_2 = z2.complexConjugate();
        Complex z_3 = z3.complexConjugate();
        // display results
        z_1.printComplex();
        z_2.printComplex();
        z_3.printComplex();

        return(0);
}

//Function Implementations
// use the scope-resolution operator ( :: ) to tie the function to the object definition
void Complex::printComplex(void) {

        if (b >= 0)
                cout << a << " + " << b << "i" << endl;
        else
                cout << a << " - " << -1 * b << "i" << endl;

}
// function to return complex Conjugate
Complex Complex::complexConjugate(void){
        Complex x; 
        x.a = a;
        x.b = b;
        if(x.b != 0){ // this is done because, if b is 0, then output would be like a + -0i
                x.b = -1*x.b;   
        }
        return x;
}

Code Screenshot:

Code output:

=================

Please refer to the code and screenshot added above.

Please upvote.


Related Solutions

CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap. 3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap. For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Create a method that returns the third character from the String word. Be sure to use...
Create a method that returns the third character from the String word. Be sure to use a try catch block and use the appropriate exception to deal with indexes out of a String’s bound: StringIndexOutOfBoundsException. Return the character 'x' (lowercase) when this exception is caught. Do not use if statements and do not use the general exception handler Exception. Examples: thirdLetter("test") -> 's' public char thirdLetter(String word) { } ​should return the char 'r'
Modify the processFile method so that it will compile and it will not produce a runtime...
Modify the processFile method so that it will compile and it will not produce a runtime error: public static void processFile(File file) { File input = "contents.txt"; String line = null; try { input = new File(file); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (file != null) { file.close(); } } }
Write a MATLAB code for the conjugate gradient method and apply it to solve the system...
Write a MATLAB code for the conjugate gradient method and apply it to solve the system Hx = b, where H is the n×n Hilbert matrix, and b is A times the vector of all ones, for (a) n = 4; (b) n = 8. Compare your numerical solutions with the exact solution (which is the vector of all ones), and report your numerical errors.
Draw the Lewis structure of the conjugate acid of CH2NH. Then, draw the valence bond (bonding...
Draw the Lewis structure of the conjugate acid of CH2NH. Then, draw the valence bond (bonding orbital) diagram for the conjugate acid of CH2NH. How many sigma bonds are in this molecule? How many π bonds are in this molecule? How would you describe the bonding orbital overlap between the carbon atom and the nitrogen atom?
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT