180 likes | 357 Views
Ordinal types. An ordinal data type is an ordered set in which every element, except the first element, has an immediate predecessor, and every element, except the last element, has an immediate successor. all types of integers characters boolean
E N D
Ordinal types • An ordinal data type is an ordered set in which every element, except the first element, has an immediate predecessor, and every element, except the last element, has an immediate successor. • all types of integers • characters • boolean • enumerated types (Enumerated types are user defined. We will not cover them in this course)
Ordinal types (con’t) • Reals and strings are not ordinal types because they do not have a unique predecessor or successor. • A FOR-DO loop’s control variable must be an ordinal type. • This is why reals cannot be control variables
ASCII Table • ASCII (American Standard Code for Information Exchange) table contains 128 entries relating whole numbers to characters • Turbo Pascal extends this table with graphical characters. The total number of characters in the table for Turbo Pascal is 256 (0 to 255 or 0000 0000 to 1111 1111). • A character’s position in this table is called it’s ordinal position.
Distinction between integer and its character representation • The number 6 and the character ‘6’ are not the same • The explicit representation of the character 6 in a program is ‘6’ and its ordinal position in the ASCII table is 54. • The explicit representation of the integer 6 in a program is 6 and its ordinal position in the integer sequence is also 6.
Ordinal Functions • ord(ordinal_type) This function returns the ordinal position of the value in the parenthesis. • ord (‘A’) returns 65 • ord (123) returns 123 • ord (TRUE) returns 1 • ord (FALSE) returns 0
Ordinal functions (con’t) • chr(byte) • give this function an integer between 0 and 255 and it returns the character whose ordinal value is that integer. • chr(64) returns ‘@’ • chr(52) returns ‘4’ {note: this is a character} • chr(87) returns ‘W’ • chr(129) returns ‘ü’
Ordinal functions (con’t) • succ() is the successor function. It returns the next value of the ordinal type. • succ(‘A’) returns ‘B’ • succ(3) returns 4 • succ(‘3’) returns ‘4’ • succ(FALSE) returns TRUE • succ(TRUE) does not have a value
Ordinal functions (con’t) • pred() is the predecessor function. It returns the previous value of the ordinal type. • pred(‘B’) returns ‘A’ • pred(3) returns 2 • pred(‘3’) returns ‘2’ • pred(FALSE) does not have a value • pred(TRUE) returns FALSE
Converting numbers read as characters to integers • Sometimes you may want to read numerical digits as characters and convert them to numerical values in your program. Number := ord(char) - ord(‘0’);
Standard Functions: arithmetic • Sqr(x): squares x • sqrt(x): positive square root of x • abs(x): absolute value of x • exp(x): raises e to x power • ln(x): log of x to base e • cos(x): Cosine of x • sin(x): Sine of x • arctan(x): Arctangent of x
More standard functions • Transfer functions (real to integer) • Round(x) rounds x to the closest integer • trunc (x) drops the part of x to the right of the decimal. • Boolean functions • odd (x) returns TRUE if x is odd or FALSE if x is not • we will learn others later
Turbo Pascal non-standard functions • Pi: the value of pi (note: no arguments) • upcase(L): returns uppercase for a lowercase letter L. For any other character it returns the same character. • Frac(x): returns part to the right of the decimal • int(x): returns part to the left of the decimal • random(N): returns random integer between 0 and N-1 • random: returns random real between 0 & 1
Midterm topics • Variables • types • integers • reals • boolean • character • strings • rules for naming • garbage values
Midterm topics (con’t) • Arithmetic operators • integers • reals • precedence • formatting output - all types • reading data • buffer • all types
Midterm topics (con’t) • Units • compiler • loops • FOR TO • FOR DOWNTO • nested • ordinal types - all topics
Midterm topics (con’t) • Programming • any topic covered in • class • homework • chapters 1-5 • functions