Astro 503 Homework #2, due Jan 24, 2006 1. Write a complex.cpp file that implements all the subroutines declared in the following complex.h file. Then write a textComplex.cpp program that has a main() that tests each of these routines. //// begin complex.h typedef struct { double real; double imag; } Complex; Complex add(Complex z1, Complex z2); Complex subtract(Complex z1, Complex z2); Complex multiply(Complex z1, Complex z2); Complex divide(Complex z1, Complex z2); void conjugate(Complex& z); double norm(Complex z); //// end complex.h 2. a) Write subroutines and a header file stopwatch.cpp/stopwatch.h to make a "stopwatch" out of the following two subroutines: void stopwatchReset(); double stopwatchRead(); These should work in the following way: first you call stopwatchReset(). Any successive call to stopwatchRead() returns the number of seconds since you last called stopwatchReset(). To get started on this, look up the C standard library routines "difftime" and "time." On UNIX, just type "man difftime" and "man 3 time" at your command line. b) Write a program called "speed.cpp" that does the following: * Creates three arrays a,b, and c of 20,000 double precision numbers each. * Fills the arrays a and b with positive numbers of your choosing. * Uses the stopwatch functions to determine how long it takes to place the (element-wise) product of a and b into c, then prints out the number of seconds. * Repeats previous step, but using a pointer-index loop instead of the natural (for i=0; i<20000; i++) loop. * Reports how long it takes to calculate the sqrt of every element of a. * Reports how long it takes to calculate the arctangent of every element of b. c) Now recompile speed.cpp by telling your compiler to optimize (on GNU and Intel compilers, you say "g++ -O -c" or "icc -O -c" instead of just "g++ -c" etc. Report the speeds of your programs with and without optimization on your machine. 3. Write a subroutine NthPrime.cpp that will determine and return the value of the Nth prime number, where N<=10,000. Write the code so that the determination of the Mth prime is done only once, no matter how many times the routine is called or what the sequence of N's is for the call. Extra credit for the one whose program runs most quickly on Reiko's machine! 4. Write a subroutine that reverses the characters in a string. Then create a driver program called reverseit.cpp that will print back to the screen all of the words on its command line, but with each word's characters reversed. You may do this with either C-style null-terminated strings, or with the C++ string class, as you please. Sample: garyb% reverseit steelers over colts sreleets revo stloc