THE CHARACTER TYPE


1. THE CHARACTER TYPE

The type character is used for handling single character such as letters, digits and special symbols (e.g. question mark, full stop, colon etc.), or non-printable control character (e.g. tab, newline etc.). In Ada characters are written by enclosing them in single quotes. Examples:

'a'     'A'     '2'     '+'     '''

2. ASCII CODE

Characters are usually stored, in a computer, using a group of 8 bits, i.e. a byte. Originally, only seven of these bits were used. The eighth most significant bit, referred to as the parity bit, was used for checking. Using only seven bits there are 128 different character codes available. There is a generally accepted standard, called the ASCII standard, which determines which characters can be encoded using the seven available bits, and which character code represent which character. ASCII (pronounced "ass-key") is an acronym for American Standard Code for Information Interchange.


3. LATIN-1 CODE

The ASCII standard was developed on the assumption that all computer usage would be in English. The English alphabet has 26 letters derived from the Latin alphabet. This set of letters is sufficient for only a small group of languages, e.g. English, Swahili and Hawaiian. All other living languages use either the Latin alphabet plus other characters, or other non-Latin alphabets, or syllabaries. Use of the ASCII standard therefore presents a problem in many countries. The obvious solution to addressing this problem is to drop the use of the parity bit so that 256 character codes are available. There are a number of "8 bit" character standards available. ADA supports what is commonly referred to as LATIN-1 (ISO-8859). In this standard the first 128 codes (0 to 127) adhere to the ASCII standard, while the remaining codes provide for additional characters.


4. CHARACTER ATTRIBUTES

Ada supports two attributes for the character type:

CHARACTER'POS(C)

Gives the character code of C (where C has type CHARACTER).

CHARACTER'VAL(N)

Gives the character that has character code N.


5. EXAMPLE PROBLEM LOWER TO UPPER CASE CONVERSION


5.1 Requirements

To produce a program that converts lower case alphabetic characters to upper case alphabetic characters. Note that lower case letters a..z have ASCII codes 97..122, and upper case letters A..Z have ASCII codes 65..90. Therefore to convert from lower case to upper case we must subtract -32 from the ASCII code of the input character. Note also that the ASCII character code of 96 is equivalent to the symbol '`', and the code 123 to the symbol `{'.


5.2 Design

A top down analysis is presented below:

TOP DOWN ANALYSIS

NASSI_SHNEIDERMAN CHART

The identified operations can be incorporated into a single procedure:

  1. LOWER_2_UPPER (top level procedure) - input lower case character, convert, output uppercase character.
    NAMEDESCRIPTIONTYPEVALUE/ RANGE
    LOW_CASE_LETTERInput variableLOWER_CASE'a'..'z'
    ASCII_CODEGlobal variableINTEGERDefault
    where LOWER_CASE is a subtype of the type CHARACTER.

An appropriate Nassi-Shneiderman design is presented to the right.


5.3. Implementation

-- LOWER TO UPPER CASE CONVERSION
-- 6 August 1997
-- Frans Coenen
-- Dept Computer Science, University of Liverpool

with TEXT_IO;
use TEXT_IO;

procedure LOWER_2_UPPER is
        subtype LOWER_CASE is CHARACTER range 'a'..'z';
        LOW_CASE_LETTER : LOWER_CASE;
        ASCII_CODE      : INTEGER;
begin
-- Input lower case character
        PUT_LINE("Input lower case character: ");
        GET(LOW_CASE_LETTER);
-- Conversion
        ASCII_CODE := CHARACTER'POS(LOW_CASE_LETTER)-32;
-- Output upper case value
        PUT("The equivalent in upper case is: ");
        PUT(CHARACTER'VAL(ASCII_CODE));
        NEW_LINE;
end LOWER_2_UPPER;

Notes

  1. We have declared a subtype, LOWER_CASE, which is a subtype of CHARACTER.
  2. We have used the character type attributes POS and VAL to convert to and from ASCII character codes.
  3. Character I/O can be achieved directly using TEXT_IO without the need to create special packages.

5.4. Testing

TEST CASEEXPECTED RESULT
LOW_CASE_LETTEROutput
'`'CONSTRAINT_ERROR
'a''A'
'b''B'
'm''M'
'y''Y'
'z''Z'
'{'CONSTRAINT_ERROR

A set of BVA and limit test cases is given in the table to the right. These test cases will also serve to test the arithmetic operation of the code with the inclusion of a sample input value near the middle of the prescribed range (e.g. 'm'). We should also carry out some random data validation testing.


Example Problem Lower to Upper Case Conversion Report.


6. TRIGONOMETRIC RATIOS

The trigonometric functions that return the trigonometric ratio (sine, cosine, tangent, cosecant, secant and cotangent) given an appropriate angle are available in the standard Ada package GENERIC_ELEMENTARY_FUNCTIONS (along with other functions including the SQRT function introduced earlier). As with SQRT, to use these trigonometric functions we must first create a new package using the GENERIC_ELEMENTARY_FUNCTIONS template. Thus:

package MATH_FUNC is new GENERIC_ELEMENTARY_FUNCTIONS(FLOAT);
use MATH_FUNC;

The trigonometric functions are now available for use. However, it should be noted that the required angle must be presented in radians and not degrees. To convert from degrees to radians we multiply by:

PI/180.0

To convert from radians to degree we multiply by:

180.0/PI

In previous examples where we have used PI (CIRCLE_CALC and CYLINDER_CALC) we have assumed that PI is equivalent to 3.14. A much more accurate value for PI is available in the Ada standard package MATH_CONSTANTS (this package also includes EXP1, i.e. Eulers number usually referred to as the constant e). Should we need to access the MATH_CONSTANTS package we simply incorporate it into our programs in the normal manner using with and use.


7. EXAMPLE PROBLEM SINE, COSINE AND TANGENT


7.1. Requirements

Produce an Ada program that will return the sine, cosine and tangent of a given angle between 0.0 and 90.0 degrees. Assume the output is to be given to five significant figures.


7.2. Design

A top down analysis is presented below:

TOP DOWN ANALYSIS

Note that the implementation will require a constant DEG_2_RAD to convert input in degrees to radians. The above identified tasks can be incorporated into a single procedure:

  1. SIN_COS_TAN (top level procedure) - input angle; convert to radians; output sine, cosine and tangent-++.
    NAMEDESCRIPTIONTYPEVALUE/RANGE
    DEG_2_RADGlobal constantFLOATPI/180.0
    INPUT_ANGLEInput variableANGLE0.0..90.0
    where ANGLE is a subtype if the type FLOAT.
NASSI_SHNEIDERMAN CHART

An appropriate Nassi-Shneiderman design is presented to the right.


7.3. Implementation

-- SINE COSINE TANGENT
-- 5 August 1997
-- Frans Coenen
-- Dept Computer Science, University of Liverpool

with TEXT_IO, MATH_CONSTANTS, GENERIC_ELEMENTARY_FUNCTIONS;
use TEXT_IO, MATH_CONSTANTS;

procedure SIN_COS_TAN is
        subtype ANGLE is FLOAT range 0.0..90.0;
        package FLOAT_INOUT is new FLOAT_IO(FLOAT);
        use FLOAT_INOUT;
        package MATH_FUNC is new GENERIC_ELEMENTARY_FUNCTIONS(FLOAT);
        use MATH_FUNC;
        DEG_2_RAD   : CONSTANT FLOAT:= PI/180.0;
        INPUT_ANGLE : ANGLE;
begin
-- Input angle
        PUT_LINE("Input angle: ");
        GET(INPUT_ANGLE);
        INPUT_ANGLE:= INPUT_ANGLE*DEG_2_RAD;
-- Sin A
        PUT("sin is: ");
        PUT(sin(INPUT_ANGLE),FORE=>1,AFT=>5,EXP=>0);
        NEW_LINE;
-- Cos A
        PUT("cos is: ");
        PUT(cos(INPUT_ANGLE),FORE=>1,AFT=>5,EXP=>0);
        NEW_LINE;
-- Tan A
        PUT("tan is: ");
        PUT(tan(INPUT_ANGLE),FORE=>1,AFT=>5,EXP=>0);
        NEW_LINE;
end SIN_COS_TAN;

7.4. TESTING

TEST CASE EXPECTED RESULT
INPUT_ANGLESineCosineTangent
-0.1CONSTRAINT ERROR
0.00.000001.000000.00000
0.10.001751.000000.00175
45.00.707110.707111.00000
89.91.000000.00175572.95721
90.01.000000.00000CONSTRAINT ERROR
90.1CONSTRAINT ERROR

Use BVA and limit testing to verify input. This will also serve to check the operation of the trigonometric statements (with the inclusion of a mid range value, i.e. 45.0 in this case). A suitable set of test cases are presented in the table right. For completeness we should also carry out some data validation testing.


Example Problem Sine, Cosine and Tangent Report.




Created and maintained by Frans Coenen. Last updated 11 October 1999