Pascal - 3 (string)

Question :

Note the following Pascal program

var s1, s2, s : string; i, ins : integer;
begin
s1 := 'TOKI';
s2 := '2005';
for i:=1 to length(s1) do if (ord(s1) < 10) then ins := 1;
insert(s2, s1, ins);
s := s1 + s2;
writeln(s);
end.

The output is:
a. runtime error
b. TOK2005I2005
c. no output
d. 2005TOKI2005
e. 200TOKI5TOKI

Discussion:

Oops ... just see the program has been a headache. But .. eh, do not faint first. "If there is a option Runtime Error, then check all the rules and syntax, may have errors. So that we do not need to do all the process in the program. " OK let's check it out!

Line 1 ... there is no problem, line 2 ... there is no problem, lines 3, 4 also does not have a problem. Line 5 ... if there is a problem? where there is a function Ord (). Function Ord () will return the integer that indicates ordinality from a number of diversified data Ordinal. View the Ord (s1) in the program. There seen that 's1' is a string data manifold.

Please note that the Ordinal type data is ascending type data. Type Ordinal data that include, among others: integer (and its derivative: shortint, longint, word, byte), boolean and char.

Eh ... why not real data types including Ordinal? because the real numbers can not be expressed in the form of a sequence. What is the real number after 1? if 2? whether the 1.5? 1.05? 1.000001? can not isn't it? because in between two real numbers there is another real number. Thus the real number can not be presented in Ascending. String is also not including Ordinal data type so that the program raises an error.

Source : Rosihan Ari’s Blog at blog.rosihanari.net (edited)

0 comments:

Post a Comment

Visitors


counter

Translate it

Followers

User


counter