Monday, August 2, 2021

Programming – Pascal: Essential skills 8

Programming – Pascal: Essential skills 8

Other commands

 

Random()

 

To generate different random numbers when the same program is run, use the command randomize.

Random(100) generates a number with a possible range of 0 – 99.

var

                a : integer;

begin

randomize;                         (* when the same program is run again, a different set of random number is generated *)

a := Random(100) + 1      (* possible values of a = 1-100 *)

end;

 

Trunc()

 

The function trunc() changes a real number into an integer by simply removing the decimal part.

e.g. trunc(2.99) becomes 2

 

Multiple select: case … of

 

Instead of using multiple if… statements, we can better organize the program by using case … of.

var

                a : integer;

being

                case a of

                                1..3 : writeln (‘less than 4’);

                                4..6 : writeln (‘between 4 and 6’);

                                7..9 : writeln (‘between 7 and 9’)

                else

                                Writeln (‘out of range’)

                end

end;


 

Relevant past paper:

DSE ICT Elect B(SP-2017):  2013 3di.

CE CIT Elect A(2005-2011): 2007 3ab. 2009 1ad, 4bi. 2010 3bcd.

No comments:

Post a Comment

Syllabus comparison

 Syllabus comparison   DSE ICT 2025 New syllabus DSE ICT 2012-2024 CE CIT 2005-2011 CE CS 1994-2004 ...