Introduction




Document: Software Engineering 1: Lab Exercises

next Exercise 1: Correction (50%)
up Session 5: Week 10/11: Ever Decreasing Circles...
gif Session 5: Week 10/11: Ever Decreasing Circles...

Introduction

This session introduces the use of two new safe-c data types - integer_type and floating_point_type.

A data type is a format for storing and manipulating some particular "kind" of information in a C program. So far we have used just two data types: string_type and boolean_type.

string_type allows the input, output, and comparison of arbitrary strings of ASCII characters. The length of the string is variable, up to a certain maximum length. The only manipulations allowed (so far) on objects of string_type rely on using functions (or "subprograms") provided by the safe-c module:

boolean_type has only two possible values (TRUE and FALSE) and is useful in controlling if and while statements. Values of boolean_type can be manipulated with the following operators:

        &&       Boolean AND. 
        ||       Boolean OR.
        !        Boolean NOT.
        ==       Test for equality.
        =        Assignment.

In more detail, the meaning of these operators is as follows:

The two new data types introduced in this session are both used to represent, store, and manipulate numbers. integer_type can represent only whole numbers (positive and negative); floating_point_type can represent fractional, or rational, numbers (positive or negative), using a form of scientific notation.

The most basic operation or manipulation that can be performed with these data types is to assign a value to a variable of that type. This uses the same assignment operator as for boolean_type. For example:

  integer_type the_answer;
  floating_point_type pi;

  the_answer = 42;
  pi = 3.141;

Note, incidentally, that the assignment operator cannot be used with objects of string_type. Thus, it would quite wrong to say something like:

  string_type s;

  s = "Hello world!";

If you need to do something like that, use the function assign_string instead:

  string_type s;

  assign_string(s, "Hello world!");

Both the numerical data types allow the four normal arithmetic operations:

        +       Addition. 
        -       Subtraction.
        *       Multiplication.
        /       Division.

These operators all accept two operands, and evaluate with a result which is of the same data type as the operands. Note carefully that when you divide two values of integer_type the result is just the quotient and will still be of integer_type. Any remainder will be discarded. Thus, for example 5/3 would evaluate as 1. You can use the special remainder operator, denoted by %, to separately calculate the remainder part of an integer_type division if you like. So 5%3 evaluates as the remainder after dividing 5 by 3, which is to say 2. The remainder operator is not applicable to floating_point_type (for obvious reasons?).

These numerical data types also allow various comparison operations:

        ==        Test for (exact) equality.
        >         Strictly greater than.
        >=        Greater than or equal to.
        <         Strictly less than.
        <=        Less than or equal to.

All of these comparison operators yield a result of boolean_type.

The detailed way that integer_type and floating_point_type are represented internally will not be discussed here, except to say that they both rely on a binary notation, because this matches best the actual physical nature of the computer's memory system. Functions are provided to convert between values of these binary numerical data types and string_type - which indirectly allows input and output of these numbers (using get_string() and put_string()).

All the data types and functions introduced here are specific to the safe-c module and you will not find them discussed in standard C texts. However, all the operators (arithmetic, boolean, assignment, comparison) are part of the C language definition and will be covered in the textbooks.

More detailed information on these new data types, and the facilities available for using them, is provided in the safe-c User Guide.

You are again required to write a formal lab report in this session, and submit it by email, in exactly the same format and manner as in sessions 3 and 4. This session also again involves creating, editing and storing files on your PC (in suitable directories), and compiling C source files to produce executable files. If you are unsure of any aspect of doing these things, please review the session 3 notes again. The relevant instructions will not be repeated here.

The overall format of this session is very similar to session 4. It has two exercises. In the first you are given a program and told what it ought to do. You are then asked to test whether the program behaves as specified; and, if not, to correct any defects you can identify. In the second exercise you are asked to develop a more ambitious program, based on the given program.




Document: Software Engineering 1: Lab Exercises

next Exercise 1: Correction (50%)
up Session 5: Week 10/11: Ever Decreasing Circles...
gif Session 5: Week 10/11: Ever Decreasing Circles...



McMullin@eeng.dcu.ie
Tue Apr 30 14:15:37 GMT 1996