Exam 2: Class Section B

Exam 2: Class Section B

The examination consists of two separate exercises. Marks will be divided equally between the two exercises.

The two exercises are related, but can be attempted independently (i.e. you do not have to complete the first in order to attempt the second, or vice versa). Therefore, it is recommended that you plan your allocation of time between the two exercises carefully in advance, and stick to this allocation during the exam.

Exercise 1: Analysis (50%)

Given a complex number which is described in rectangular co-ordinates as , then the same number can be described by the polar co-ordinates where:

The program R2P.C in supposed to take in the rectangular co-ordinates of a complex number, from the keyboard, convert them to polar, and print these polar co-ordinates on the screen.

The program has various deficiencies. You are required to correct all deficiencies you can identify. For all changes that you make, the report should contain a clear statement of the change and a specific explanation of your rationale for the change. Of course, if you are making several similar or related changes, you may discuss these as a unit.

When you have reached the point where you think the program should work, then you should test it, and report on these tests. Carry out, and report on, further corrections if necessary.

Note carefully that you must not simply present a version of the program rewritten from scratch. You are required to identify the specific deficiencies in the program you have been given.

Exercise 2: Synthesis (50%)

Given a complex number which is described in polar co-ordinates as , then the same number can be described by the rectangular co-ordinates where:

Develop a program which will continuously or repeatedly request polar co-ordinates of a complex number, and print out the corresponding rectangular co-ordinates. The program should terminate when the value zero is entered for the magnitude of the complex number.

The program must conform to the following guidelines:

  1. The program must be divided into functions.

  2. No global variables - all variables must be local to some function. Use parameter passing and/or return values where appropriate to exchange information between functions.

  3. The program should demonstrate good coding practices with regard to spacing, indentation, comments etc.

Test this program rigorously. Record all test results.

File R2P.C

#include <math.h>

double x, y, r, theta

void get_rect(void){
printf("Please enter x co-ordinate:");
scanf("%lf", x); printf("Please enter y co-ordinate:");
scanf("%lf", y);}

void rad2deg(double rad){
pi = 3.141952654;
  return (rad * pi / 180);
}




double 
  r2p(void){
  R == sqrt(x*x + y*y);
  theta == rad2deg(atan(y/x));
}

void print_polar(void){
 Printf("The R co-ordinate is: %%f\n", R);
 Printf("The theta co-ordinate is: %%f\n", theta);
}

void mian(void){
  printf("\n\nWelcome to R2P!\n);
 get_rect();  print_polar();r2p(); 
   printf("\n\nBye from R2P./n");
}


McMullin@eeng.dcu.ie
Tue Apr 9 14:13:15 GMT 1996