CS 1001 Introduction to Computers -- D Term, 2000
Assignment 4   Due: Tuesday, April 18, 2000

Purpose:  To write a program that utilizes subprograms.

Problem

A complex number is a number of the form a + bi where a and b are real numbers and i2 = -1 . Fortran supports the data type COMPLEX, but in this program you are to implement operations on complex numbers without using Fortran's built-in data type. Using a top-down approach (see page 29 section 1.4 of text), develop a menu-driven program that reads two complex numbers (where a + bi is entered simply as the pair of real numbers a and b) and allows the user to select one of the operations of addition, subtraction, multiplication, or division. The program should then call an appropriate subroutine to perform the specified arithmetic operation and display the result in the form a + bi. The four operations are defined as follows:

(a + bi) + (c + di) = (a + c) + (b + d)i

(a + bi) - (c + di) = (a - c) + (b - d)i

(a + bi) * (c + di) = (ac - bd) + (ad + bc)i

(a + bi) / (c + di) = ( (ac + bd) / (c2 + d2) ) + ((bc - ad) / (c2 + d2)) i

provided that c and d are not both 0 in the case of division. The program should allow the user to continue entering complex numbers until she/he chooses to quit.

Assumptions and Restrictions

You may not use the data type COMPLEX in this program.
Your program must include four subroutines (one for each of the arithmetic operations). Each subroutine is given four input parameters, representing the real and imaginary parts of the two complex numbers. Each subroutine prints the result of the arithmetic operation (there are no output parameters).

Use the following subroutine when printing the results:

SUBROUTINE Print_Complex (Real_A, Im_A, Real_B,  Im_B, Real_X, Im_X, Operation)
 !   This subroutine will print out the result of the complex calculation
 !   Pre-condition :  variables Real_A, Im_A, Real_B,  Im_B, Real_X, Im_X, Operation are defined
 ! Post-condition:  A line giving the two input complex numbers, the operation, and the result are printed.

     REAL INTENT(IN) ::  Real_A, Im_A, Real_B, Im_B, Real_X, Im_X
     CHARACTER(1) :: Operation

     PRINT 20, Real_A, Im_A, Operation, Real_B, Im_B, Real_X, Im_X
     20    FORMAT ( 1x, " ( ", F7.2,  " + ", F7.2, "i ) ", A1, " ( ", F7.2,  " + ", F7.2, "i ) =    ( ", F7.2,  " + ", F7.2, "i ) " )

END SUBROUTINE Print_Complex

So that your output looks similar to that shown in the sample execution, i.e.,  (   2.10 +   4.70i) * (   6.00 +  -8.40i) =   52.08 +  10.56i    The input to this subroutine are six  real numbers that are the real and imaginary parts of the two given complex numbers and the calculated results.  The last argument is a character that is  "+", "-", "*", or "/" depending on the operation that the user has selected.

Your program must also include one function of type LOGICAL. The function should return the value .TRUE. if both c and d (as given in the equations above) equal zero, .FALSE. otherwise.

In the main program, your selection structure should be set up like this:

     IF the user chooses addition THEN
        add the two complex numbers
     ELSE IF the user chooses subtraction THEN
        subtract the two complex numbers
     ELSE IF the user chooses multiplication THEN
        multiply the two complex numbers
     ELSE IF the user chooses division THEN
        IF denominators are O.K. THEN
           divide the two complex numbers
        ELSE
           print ``zero denominator'' message
        ENDIF
     ENDIF

Sample Execution

                COMPLEX NUMBER CALCULATOR

 You will be asked to enter two complex numbers of the
 form a + bi.  For each complex number, your input
 will be the pair of real numbers a and b.

 After entering the complex numbers, you will be asked
 to choose from a menu of arithmetic operations.

 Enter first complex number as a pair of real numbers
 (separate your two values with a space)
2.1 4.7
 Enter second complex number as a pair of real numbers
 (separate your two values with a space)
6 -8.4

 Which operation would you like to perform?

 Type:  A to add
        S to subtract
        M to multiply
        D to divide

 What is your choice?
q

 Sorry, you have entered an invalid choice
 What is your choice? (A, S, M, or D)
m

 You have chosen to multiply the complex numbers
(   2.10 +   4.70i) * (   6.00 +  -8.40i) =   52.08 +  10.56i


 Would you like to enter another pair?
n

 program terminating...
%exit
exit

Grading

This assignment will be graded on 100 points. Point values will be allocated as follows:
Documentation
(30 points) Introductory comment, meaningful identifiers, proper use of indentation and blank lines, comments for variables, algorithm comments. Each subprogram must be adequately commented, including pre- and post-conditions! (Pre-conditions describe the condition that must be true before the subprogram is called.  Post-conditions are the conditions that must be true after the subprogram execution.)
Functionality
(40 points) The program must work correctly on our test data.
Modularity and Design
(20 points) The main program should be simple and easy to follow. The program must contain at least four subroutines and one function (as mentioned above.) Parameters must be used to pass information to the subprograms.
Output
(10 points) Introductory message to the user, useful prompts, helpful error messages, output properly labeled and formatted

Deliverables

Using the turnin program, turn in your Fortran source file and a typescript file showing successful compilation and program execution. Your files must be turned in prior to 12:00 p.m. on April 18, 2000 to receive full credit. The command you should use to turn in your assignment is:   /cs/bin/turnin submit cs1001 hw4 hw4.f90 hw4.script


Adapt from Professor Glynis Hamel's material -- 1998