PHYCS 3730/6720 Lab Exercise
Reading and references:
For these exercises we play with code from lab11 and
lab02. Answers should go in a file lab12.
Exercise 1. Experimenting with the standard complex class
Convert the file ~p6720/exercises/intro_to_classes/checkcmplx.cc so it
uses the standard C++ complex class instead of our own complex class.
Make the following changes. Compile and run your code to verify your
changes.
- Change the #include statement so it calls for
<complex> instead of "mycomplex.h"..
- Using the typedef statement, as explained in the notes,
define a dcmplx to be an abbreviation for
complex<double> and use it in place of the type
complex throughout. Note that the name of the constructor
function also changes.
- Change all but one of the cout statements so you
write your dcmplx object without picking apart the real
and imaginary parts. Example: cout << b .
- Change the line that computes and prints the result of norm(a).
- Add three lines at the bottom of your code that prompt for and
read the complex number c using cin >> c and write
out the result.
Copy your code to your answer file. Then answer these questions:
- What method (accessor) gives the real part of a dcmplx number
a?
- In what format does cout display the real and imaginary
parts of a number?
- We usually define the norm of a complex number to be
the square root of the sum of the squares of the real and imaginary
parts. What did you get with the C++ standard norm function?
- How do you have to enter the real and imaginary parts of
a complex number so cin reads the correct value?
Exercise 2. Experimenting with the standard string class
In exercise 3 of lab02, you modified the code whoyou.cc.
Change your modified code so the character string is defined as a
string object instead of a character array. Note that it is
unnecessary to specify a string length. In the cout statement,
use the overloaded + operator to concatenate strings, as in the
example in the notes.
Copy your code to the answer file.