Exam 1: Class Section A

Exam 1: Class Section A

Thursday, 18th January 1996

The exercise consists of two parts. Marks will be divided equally between the two parts.

The two parts are independent (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 parts carefully in advance, and stick to this allocation during the exam.

Part 1: Analysis (50%)

Consider the file CYLVOL0.C.

This is supposed to be a C program to calculate the volume () of a cylinder, of radius and length , based on the following equation:

It should request measurements of the relevant dimensions of the cylinder, to be input from the keyboard, calculate the volume, and display the result 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.

Part 2: Synthesis (50%)

You are required to develop (code, test, debug) a C program to carry out the following calculation.

Pythagoras' Theorem states that, for a right angled triangle, the square on the hypotenuse will be equal to the sum of the squares on the other two sides:

where a and b are the two sides enclosing the right angle, and c is the hypotenuse.

Develop a program which will read in the length of one side (a) and the length of the hypotenuse (c), and then calculate and print out the length of the remaining side (b). Note that certain combinations of input values would be invalid in that it would be impossible to construct a right angled triangle with the given dimensions. This will be the case if a is specified to be greater than c. If this situation arises your program should issue a warning message and terminate.

In more detail, your program must do the following:

You will find it necessary to use the standard library function sqrt(). This accepts one argument of floating_point_type. The value of this argument must be non-negative. The return value is also of floating_point_type and is the square root of the value given as the argument.

File CYLVOL0.C

#input (safe-c.h)


Void Main(Void)
{
  floating_point radius, length, Volume;
string string_number;

    put_string("\n\nWelcome to CYLVOL.\n\n");
    put_string("This program calculates the volume of a cylinder,\n");
    put_string("given the radius and length.\n\n");

    put_string("Please enter the radius: "); get_string
    (string_number); radius
    = string_to_floating_point(string_number);
  put_string("Please enter the length: ");
   get_string(string_number);
    length = string_to_floating_point(string_number);
     volume = 3.141 * radius * radius * length;
      floating_point_to_string(volume, string_number);

  put_string("\n\nThe volume is: );
  put_string(string_number);

  put_string("/n/nGoodbye from CYLVOL./n/n");
}


McMullin@eeng.dcu.ie
Fri Mar 29 08:52:22 GMT 1996