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

In C++: In order to print a complex value, you must print the real part first, t

ID: 3591468 • Letter: I

Question

In C++:

In order to print a complex value, you must print the real part first, then the imaginary part (with the sign showing) and finish it off by adding a “j” to the end to indicate which part is imaginary. Look back at lab #1 to see how to make cout display the signs of positive numbers. It should look something like: “1+2j”. Write ONLY the “cout” portion of your new printing function below:

How would I modify the function below to print out complex lists???

void myComplexprinting(int entries, complex *list1, complex *list2) {

int i;

for (i = 0; i < entries; i++) {

cout << list1[i] << " " << list2[i] << endl;

}

return;

}

Explanation / Answer

Your requirement is to print the numbers in the format real + imag j

As given, entries represent the number of entries in the lists, and the first list i.e., list1 represents an array of complex variables, and list2 represent an array of complex variables.

But to make this print in the specifed format, we should be knowing the datatype for complex, as this is a user defined data type.

Let me assume that the structure complex is defined as:

typedef struct complex

{

int real;

int imag;

}complex;

Now, to print the specified format, the cout statement will be:

cout << list1[i].real<<"+"<<list1[i].imag<<"j"<<" "<<list2[i].real<<"+"<<list2[imag]<<"j"<<endl;