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' '+' '''
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.
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.
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.
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 `{'.
A top down analysis is presented below:
The identified operations can be incorporated into a single procedure:
NAME | DESCRIPTION | TYPE | VALUE/ RANGE |
---|---|---|---|
LOW_CASE_LETTER | Input variable | LOWER_CASE | 'a'..'z' |
ASCII_CODE | Global variable | INTEGER | Default |
An appropriate Nassi-Shneiderman design is presented to the right.
-- 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
TEST CASE | EXPECTED RESULT |
---|---|
LOW_CASE_LETTER | Output |
'`' | 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.
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.
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.
A top down analysis is presented below:
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:
NAME | DESCRIPTION | TYPE | VALUE/RANGE |
---|---|---|---|
DEG_2_RAD | Global constant | FLOAT | PI/180.0 |
INPUT_ANGLE | Input variable | ANGLE | 0.0..90.0 |
An appropriate Nassi-Shneiderman design is presented to the right.
-- 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;
TEST CASE | EXPECTED RESULT | ||
---|---|---|---|
INPUT_ANGLE | Sine | Cosine | Tangent |
-0.1 | CONSTRAINT ERROR | ||
0.0 | 0.00000 | 1.00000 | 0.00000 |
0.1 | 0.00175 | 1.00000 | 0.00175 |
45.0 | 0.70711 | 0.70711 | 1.00000 |
89.9 | 1.00000 | 0.00175 | 572.95721 |
90.0 | 1.00000 | 0.00000 | CONSTRAINT ERROR |
90.1 | CONSTRAINT 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