Question: In C++: #include typedef struct { double re, im; } complex; #define compmag(a) sqrt((a.re*a.re)+(a.im*a.im)) The first line instructs the compiler to use some additional math

In C++:

#include

typedef struct { double re, im; } complex;

#define compmag(a) sqrt((a.re*a.re)+(a.im*a.im))

The first line instructs the compiler to use some additional math functions; we are interested in square root. The second line creates a custom datatype which is a structure containing 2 real numbers (this is from the lectures); this is also a global definition since it is not within any functions. The last line is a quick macro which determines the magnitude of a complex number; we will need this in our modified sorting routine. Copy your existing sorting function which operates on an array of doubles into a new function called mycomplexsort. Change the datatypes of your list argument to complex as well as your temporary variable for swapping. You need to also change your comparison line so that it properly compares complex numbers. Use the macro above to perform the magnitude calculation. Write ONLY that line below:

This is my sorting code below(C++), but how would you do those modification theyre asking??

int mycomplexsort(int entries, double *list2) {

int n = 0;

int j, maxindex, i;

double temp;

/* add your local variables here */

for (j = 0; j < entries - 1; j++) {

temp = list2[j];

maxindex = j;

for (i = j + 1; i < entries; i++, n++) {

if (list2[i] <= temp) {

temp = list2[i];

maxindex = i;

}

}

if (maxindex != j) {

temp = list2[maxindex];

list2[maxindex] = list2[j];

list2[j] = temp;

}

}

/* add your sorting code here */

return n;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!