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 takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
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'
Create a Java method findLongestPalindrome that takes a Scanner scn as its parameter and returns a...
Create a Java method findLongestPalindrome that takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: Use the built in isPalindrome method. This method calls for an optimization loop.) Where the isPalindrome Method takes a String s as a parameter and returns a boolean. It returns true if s reads the same forwards and backwards, i.e., is a...
Implement a method that does the following: (a) Create a Map data structure to hold the...
Implement a method that does the following: (a) Create a Map data structure to hold the associations between player name (key) and team affiliation (value) (b) Add at least 5 mappings to the map (c) Print out the current roster of mappings (d) Assuming some time has passed, change some of the mappings to simulate the players changing their affiliations (e) Again, print out the current roster of mappings 3. Implement a main method that calls the above methods, demonstrating...
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(); } } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT