CS 2073, Fall 2005
 Program 11
 Complex Numbers
 Due (on time): 2005-11-30  23:59:59
 Due (late):        2005-12-02  23:59:59

Program 11 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2005-11-30  23:59:59 (that's Wednesday, 30 November 2005, 11:59:59 pm) for full credit.
  • 2005-12-02  23:59:59 (that's Friday, 2 December 2005, 11:59:59 pm) for 75% credit.

Note: If you are working in the CS lab, Science Building 1.02.04, the old Visual C++ 6.0 is now gone. Instead you must use Visual C++ 2005 Express Edition. Directions for using this are at: running.


Introduction: For this program you are to work with complex numbers in C. The code needed for this assignment is very similar to the code in the example about fractions: structs and Fractions, and your program can modeled after it. In outline, you are to do:

  1. Use structs to represent complex numbers.
  2. Implement a number of basic complex operators: add, multiply, multiply by a real, modulus.
  3. Test the operators defined under item 2.
  4. Write code to implement the exponential function for complex numbers.
  5. Test the code under item 4 by finding exp(0 + pi i).

1. Complex numbers: These consist of two doubles, giving the real part and the imaginary part of the complex number. If these two numbers are a and b, then we will write the complex number either as (a, b) or as a + b i, where i = sqrt(-1). (Sometimes j is used in place of i.)

C does not support complex numbers in any way. Thus it is illegal to write anything such as sqrt(-1) in C. Some languages, such as Fortran, have built-in facilities for complex numbers. In other languages such as C++, it is reasonable to write library code that makes it look as if complex numbers are a built-in type. C is in the middle between these two extremes. So we are going to have to do everything "by hand", just as with fractions.

As with fractions, you must use the followings struct to represent a complex number ("must" because I want you to practice with structs):

For operations with these complex numbers, we have to fall back to the definitions of each operation. At least we don't have to use the gcd or reduce functions, as we did with fractions. (Things are a bit simpler with complex numbers.)


2. Operators on complex numbers: Here are definitions of several of the operators on complex numbers (notice that these follow the ordinary rules of algebra with the addition that i2 = -1):

You need to translate each of these into C functions that make use of the struct complex, in a way similar to the C functions that implement fractions.


3. Test of operators: Here is some simple test data:


4. exp defined for complex numbers:One can use the series for exp(x) given in Program 10 to define exp(z), where z is a complex number.


5. Test of code for exp(z): You are to find an approximate value for exp(0 + pi i). The table below gives terms 0 through 3 of the series, along with the cumulative sums. You program should add up 30 terms of the series, giving partial results in roughly the form below. In particular, answer the question: what does exp(0 + pi i) converge to?

First 4 terms of the series for exp(0 + pi i)
Calulating exp(0 + 3.141592653589792i)

#  0, term: ( 1.00000000000000, 0.00000000000000), abs: 1.00000000000000
       sum: ( 1.00000000000000, 0.00000000000000), abs: 1.00000000000000

#  1, term: ( 0.00000000000000, 3.14159265358979), abs: 3.14159265358979
       sum: ( 1.00000000000000, 3.14159265358979), abs: 3.29690830947561

#  2, term: (-4.93480220054467, 0.00000000000000), abs: 4.93480220054467
       sum: (-3.93480220054467, 3.14159265358979), abs: 5.03510404644239

#  3, term: (-0.00000000000000,-5.16771278004996), abs: 5.16771278004996
       sum: (-3.93480220054467,-2.02612012646017), abs: 4.42581417642655


What you should email: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Program Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Program 11:

Last Name, First Name; Course Number; Program Number.

  1. Source code for the program that works with complex numbers. This includes code to do the various operators, and code to use a series to do the exp function for complex numbers.
  2. A run giving the simple results in part 3 above.
  3. A run giving the individual terms and the cumulative sums for the series to calculate exp(0 + pi i).


Revision date: 2005-11-20. (Please use ISO 8601, the International Standard.)