Exam 2: Class Section C

Exam 2: Class Section C

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%)

Give a triangle with base and height , the area is given by:

The program TRI.C in supposed to repeatedly prompt for, and read in, the base and height of a triangle, and then calculate and print out the corresponding area. It should terminate when a base or height of value zero is entered.

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%)

Give a rectangle with length and width , the area is given by:

Give a circle with radius the area is given by:

Develop a program which will continuously or repeatedly request the user to select a shape (triangle, rectangle or circle); it should then request the dimensions appropriate to that shape, and calculate and print out the area. In addition to the three supported shapes, an option should be offered to terminate the program; the program should continue unless and until this termination option is selected.

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 TRI.C

#define TRUE (1)
#define FALSE (1)

double get_double(char prompt[])
/*
  Print the prompt, then read in one value
  of type double and return it.
*/
{
  printf("%s", prompt);
  scanf("%f", &x);
  return;
}

int do_one_triangle(void)
{
  int done;
  double base, height, area;

  done = FALSE;

  base = get_double("Please enter the base of the triangle:");
    height = get_double("Please enter the height of the triangle:);
      area = base x height x 0.5;
      if (area = 0.0) done = TRUE; else


printf("The area is: %f\n", area);
return(done);}
void Main(void)



{
  int done;
  printf("Welcome to TRI./n/n");
  printf("(Enter 0.0 for base or height to terminate.)\n");
  done == FALSE;
  while (!done) done = do_triangle();
  printf("Bye from TRI.\n\n");
}


McMullin@eeng.dcu.ie
Tue Apr 9 14:19:30 GMT 1996