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)
Thursday, July 09, 2009 | Labels: Pascal | 0 Comments
Pascal - 2 (string)
Question:
Whether the output from this program?
var s : string[12];
begin
s := 'setia';
writeln('#', s, '#');
s := s + ' sampai akhir ';
writeln('#', s, '#');
end.
a. #setia# (and the runtime error occurred)
b. #setia #
#setia sampai#
c. #setia#
#setia sampai akhir#
d. #setia#
#setia sampai#
e. #setia#
#sampai akhir#
Discussion:
If our analysis on the program, s bertipe data is variable length character string with a maximum of 12. S initial value is the string ’setia’ ((length of line 5). Means that there is still free space for the rest of the string s as much as 7 units. Remember ... an empty space in here not contain spaces, but the null character. So when printing writeln ('#', s,'#'); still appear ‘#setia#’ and not ‘#setia #’.
Then on the next line profile with a long string ‘ sampai akhir ‘ (14 characters including spaces). Can the string plus the string? Runtime error? he ... 3x here the meaning of plus signs (+) does not mean the addition, but the string unification. Operator + can be given in the string. But do not try to use fraudulent operators -, * or / in the string operation. Computer can error.
So that the string s is the new, ’setia’ + ‘ sampai akhir ‘. Continue ... what is the result? whether the ’setia sampai akhir’? Eit .. whoa, how much capacity the number of characters that can be loaded on the s? Oops ... there are only 12 characters only. Thus the result is ’setia sampai’ (12 characters).
Next printing writeln ('#', s,'#'); and the result is ‘#setia sampai#‘. Thus the result is a D.
Source : Rosihan Ari’s Blog at blog.rosihanari.net (edited)
Wednesday, July 08, 2009 | Labels: Pascal | 0 Comments
Pascal - 1
Question:
If M (x, y) is the statement "x is greater than y", and there is a row in the following pseudo Pascal:
while M (x, y) do
begin
x: = x - 10;
y: = y + 2;
end;
with prices in the first and x = 70 y = 5, y was the price after the row out of the while loop?
A. 11
B. 15
C. 17
D. 21
E. 25
Discussion:
OK ... we will study the above questions.
Note that the while loop has a condition "x is greater than y".
So during these requirements met (Condition TRUE value) then the looping will continue to run.
And the looping process will stop when the condition is not met (value FALSE) Note the way the following:
x = 70, y = 5
Check (x> y) -> (70> 5) -> TRUE
x = x - 10 = 70 - 10 = 60
y = y + 2 = 5 + 2 = 7
Check (x> y) -> (60> 7) -> TRUE
x = x - 10 = 60 - 10 = 50
y = y + 2 = 7 + 2 = 9
Check (x> y) -> (50> 9) -> TRUE
x = x - 10 = 50 - 10 = 40
y = y + 2 = 9 + 2 = 11
Check (x> y) -> (40> 11) -> TRUE
x = x - 10 = 40 - 10 = 30
y = y + 2 = 11 + 2 = 13
Check (x> y) -> (30> 13) -> TRUE
x = x - 10 = 30 - 10 = 20
y = y + 2 = 13 + 2 = 15
Check (x> y) -> (20> 15) -> TRUE
x = x - 10 = 20 - 10 = 10
y = y + 2 = 15 + 2 = 17
Check (x> y) -> (10> 17) -> FALSE
STOP
After a while looping process is complete, the final value of y is 17 (Best in the right C).
Source : Rosihan Ari’s Blog at blog.rosihanari.net (edited)
Wednesday, July 08, 2009 | Labels: Pascal | 0 Comments