Anda di halaman 1dari 172

Roby's Pascal Tutorial Part 1 Contents

The Basic of Pascal


What you will get after this lesson : 1. The understandings of inner program structure of Pascal. 2. Know Pascal data structure well. 3. Write a simple text-based program. 4. Know how to store and retrieve data from the disk. Well, the first lesson is divided into 14 chapters.

CHAPTER 1 -- "Hello, World !" A classic approach to begin the course QUIZ Contains the basic understanding of structured programming in Pascal, simple data and some basic input output commands just to declare "Hello, World !" CHAPTER 2 -- Let's Extend ! QUIZ Extending the first "Hello, World !" with some colors. Know what crt unit is. Doing some assignments and calculations. CHAPTER 3 -- If I... QUIZ Contains the basic understanding of conditional branching and its variants, the case of. This is also a lesson for getting keyboard command, readkey. CHAPTER 4 -- "Constants !" QUIZ Yet, all basic thing about constants in Pascal. CHAPTER 5 -- Loop A Loop QUIZ Contains three form of looping in Pascal: For, Repeat ... Until, and While ... do. CHAPTER 6 -- What the hell are Procedures and Functions QUIZ Contains basic understandings about Procedures and Functions. How to break down the problems and then write the required procedures and/or functions. CHAPTER 7 -- Mastering Array QUIZ Mentions basic understandings about array and how to use it. CHAPTER 8 -- Dancing With Strings QUIZ Contains all basic things needed to modify and process strings. CHAPTER 9 -- Best Records QUIZ Get record basic understandings and how to use it. CHAPTER 10 -- Complex ? Not so ... QUIZ Continues records and extending it into complex data structure. CHAPTER 11 -- It's All Set ! All basic understandings about sets. CHAPTER 12 -- Unit 1 Ready ! QUIZ Writing your own unit, extending the available standard units. CHAPTER 13 -- Read the Text File QUIZ Writing and Reading text files and other applications.
file:///F|/Documents/Pascal_Doc/pas/pasles01.html (1 of 2)8/3/2005 7:42:19

Roby's Pascal Tutorial Part 1 Contents

CHAPTER 14 -- How About Binary One ? Writing and Reading binary files and other applications.

QUIZ

Every lesson has a quiz so that you could evaluate yourself.

file:///F|/Documents/Pascal_Doc/pas/pasles01.html (2 of 2)8/3/2005 7:42:19

Pascal Tutorial - Chapter 1

"Hello, World !" A Classic Approach to Begin the Lesson

Welcome !
Pascal is a structured programming language. It means that everything you program, must be structured and in orderly. No more free gotos and jumps. It has a format. Yeah... right. You don't have to obey it anyway since you COULD extend it to complex ones. A format of very simple Pascal programs :

uses .... var ... begin .... (Your program is here) end.
The word end here should end with stop ('.'). Every Pascal program blocks begins with the word begin and ends with the word end. Some of the end word ends with semicolon ';' or period '.' or just nothing. That will be described later (not in this lesson). For example : Type in these words in your BP, and run it by pressing Ctrl+F9

file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (1 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 1

begin Writeln('Hello, World !'); end.


In this case we don't need the word uses and var. Try to run it. It should display 'Hello, World !' on screen. But perhaps it's too fast. But, how can I view it anyway ? Press Alt+F5. What is the word uses for ? It's a kind of terms to declare that we use specific library. Some are standard ones. At first, we use the standard ones that come with every Pascal compiler. The one we will use most is crt unit, since it contains various commands that is suitable for us for this moment. What is Writeln for ? Writeln is a command to write something into the screen. If you want to write a sentence, just wrap it with quote ('). And don't forget the pair of brackets and the semicolon. Example :

begin Writeln('I learn Pascal'); Writeln('Hi, there !'); end.


You will see another messages on screen. View it with Alt+F5 after running it by pressing Ctrl+F9. So, you want to clear the screen when the program starts. You need to add the word Clrscr; just after the word begin. But it needs uses crt since Clrscr is one of the commands included in crt library. (Pascal terms for library is unit) So, the above example would become :

uses crt; begin Clrscr;


file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (2 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 1

Writeln('I learn Pascal'); Writeln('Hi, there !'); end.


Try and run it ! Pascal has a command Write instead of Writeln. Why don't you just try that ? Just replace all the Writeln into Write (of the program above). Run it and see what happens. See the difference ? It'll output I learn PascalHi, there !. Stick ! Sometime, we need to leave a line blank. Suppose we want a blank line between I learn Pascal and Hi, there !. Just insert Writeln; between, and bingo ! Try inserting the Writeln in correct order so it yields :

I learn Pascal Hi, there !


If you do have a good grip about how to write something on screen, please proceed. Otherwise, you should repeat it once again.

Now the input parts. When we ask computer to receive input from users, we need something to store the input before it is being processed. That was variable. We need it to corporate our program. There are various types of variable and with various range too. Here are some frequently used variable types :

Pascal Variable Name, Range, and Type Type name Range

Type

file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (3 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 1

Shortint Byte Integer Word Longint Real String Char Boolean

-128 to +127 0 to 255 -32768 to +32767 0 to 65535 -2146473648 to +2146473647 -??????? to +??????? up to 255 letters 1 letter only false / true only

integer integer integer integer integer fractional non-numeric non-numeric logical

There are many more, but that's all we need for now. How to declare our variables ? As we seen in our format previously that we have var section there. That's where we suppose to declare our variables. Here is some example :

var MyAge : Byte; Comments : String;


To assign variables with values, you need to write this syntax :

var_name := value;
This should be written anywhere within begin ... end block, in appropriate location.

Example :

var MyAge : Byte; Comments : String;


file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (4 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 1

begin MyAge:=19; Comments:='Hi, there ! I'm learning Pascal'; Writeln('I am ',MyAge,' years old'); Writeln(Comments); end.
Write it down, run it an view it on screen ! How to get the user's input ? Yeah, that's easy actually. Similar to Write or Writeln, Pascal uses Read and Readln. The difference is that in Read or Readln, we cannot write anything on screen but to get user's input. Example :

var MyAge : Byte; Comments : String; begin Write('How old are you ? '); Readln(MyAge); Write('Give us comments: '); Readln(Comments); Writeln; Writeln('I am ',MyAge,' years old'); Writeln(Comments); end.
Write it down, run it an view it on screen ! Now, you know how to get user's input. But wait ! If I enter 23.5 in 'How old are you ?' it spouts error ! Yeah, you right ! The value on that question is actually stored in MyAge. MyAge is a Byte variable. So it only accept integer values between 0 to 255. That's the range of it ! It cannot go further. If you want computer to accept fractional values in MyAge, try using Real instead of Byte.

file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (5 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 1

But why when I change it to Real, it outputs erratical numbers ? No, it actually doesn't throw garbage on the screen. It just exponential numbers. For ordinary people -- non technical -- it would be more convenient to view fractional number at 3 or 4 place after the decimal point. Try changing :

Writeln('I am ',MyAge,' years old');


to :

Writeln('I am ',MyAge:2:4,' years old');


What does it mean ? Why some :2:4 attached to MyAge ? For real numbers, it means : Show 2 place before decimal point and 4 place after decimal point So, when we enter 23.5 for MyAge, it will output :

I am 23.5000 years old


Try changing :2:4 with other values. Be creative ! Can we apply it to the string ? Nope ! But it only accepts just :x. It means that 'I only grant this variable x place on the screen to express its value'. Suppose you change :

Writeln(Comments);
to

Writeln(Comments:5);
Try writing long comments when the program is running. See what happens. If you enter Programming as the Comment, you will see output :

Progr
Ok ! I'm tired of pressing Alt+F5 to view this tiresome tutorial result ! No problem. Just add Readln; just before end. Try it yourself ! That's all for this time. Refer to the quiz to evaluate your understanding.

file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (6 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 1

Now, select your way


Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz To Chapter 2, about exploring

crt units

file:///F|/Documents/Pascal_Doc/pas/pasl1000.html (7 of 7)8/3/2005 7:42:29

Pascal Tutorial - Chapter 2

Let's Extend !
Hi !
We meet again ! Glad to see you ! Now, let's extend our first program with a little variation. Crt unit does not consist of Clrscr only. It has a lot of interesting functions and procedures. The ones we will discuss this time are : 1. 2. 3. 4. 5.

TextColor and TextBackGround TextAttr GotoXY TextMode Sound, Delay, and NoSound

This chapter is intended to add your interests in programming so the lesson wouldn't be so tiresome. If you want to skip this chapter it's OK since it has no important thing to spot. But beware that every jargon and commands I use in this lesson will be brought to the next ones. OK ! Let's give color to our text on screen ! We use Crt unit's procedures: TextColor and TextBackGround. Both accepts value ranging 0 to 15. Example :
TextColor(14); TextBackground(1);

It will set the foreground color to yellow and blue background for the next Write or Writeln. Yep ! Easy ! Let's look at full example :

uses crt; begin TextColor(14); TextBackGround(1); Writeln('Hello there !'); end.


file:///F|/Documents/Pascal_Doc/pas/pasl1001.html (1 of 6)8/3/2005 7:42:39

Pascal Tutorial - Chapter 2

Run it and see what happens. O ho ! That's great ! Yellow over Blue. Try to figure all color values ! A lot of variation could be applied and it's all yours to choose. There's even more convenient than above : Use TextAttr instead. It's a kind of a 'combo' variable. You just assign a value that is :

(background * 16)+foreground
Suppose you want a cyan background (or light blue), the number is 3, and black foreground, that is 0. So you would write this:

TextAttr:=(3*16)+0;
Then, subsequent writes on screen will be in black over cyan. Easy, eh ? If you clear screen AFTER you assign colors, you will found that the entire screen will be wiped to the last assigned color. Suppose you have set color black over cyan, if you clear the screen, the entire screen will be black over cyan ! It's useful if you want alternative background instead of black -- It's boring. Yeah ! Easy ! Want to have big characters on screen ? Try adding TextMode(CO40);. Well, TextMode is used for changing text mode. Valid values to pass is CO80, that is for return back to normal mode, or LastMode -- that is back to last mode visited, and refer help for more information! Example :

uses crt; begin TextMode(CO40); Writeln('A Real BIG Characters on screen !'); Readln; TextMode(CO80); Writeln('Back to normal'); Readln; end.
file:///F|/Documents/Pascal_Doc/pas/pasl1001.html (2 of 6)8/3/2005 7:42:39

Pascal Tutorial - Chapter 2

Before we continue, I would like to introduce the screen behavior of PCs in text mode. Screen has a resolution. In text mode, screen has either 80 or 40 horizontal resolution (generally) and 25 vertical resolution. It means, the screen could be 80 cols by 25 lines or 40 cols by 25 lines. The setting of TextMode reflects to this : CO40 -- sets the screen to 40 characters wide and 25 lines -- and CO80 -- sets the screen to 80 characters wide and 25 lines too. 40 characters wide causes the screen to "stretch" horizontally, so in result, WIDE characters appear on screen. EGA and VGA has a special feature that is also supported by crt unit : an enhanced text mode consists of 43 (when EGA) or 50 lines, instead of 25. We put the value Font8x8 inside TextMode parameter. We can also directs our text output at specified location by using GotoXY. It accepts parameters X and Y (both are bytes). Note that X reflects to column position, and Y reflects to line position. Both X and Y must not exceed the range of the current text mode. Run and view this program GOTOXY.PAS to see what's going.

uses crt; Begin TextMode(CO40); GotoXY(8,12); Writeln('At 8,12'); Readln; GotoXY(35,8); Writeln('At 35,8'); Readln; GotoXY(40,20); Writeln('At 40,20'); Readln; GotoXY(30,30); Writeln('Guess'); Readln; TextMode(CO80); Writeln('Back to normal');
file:///F|/Documents/Pascal_Doc/pas/pasl1001.html (3 of 6)8/3/2005 7:42:39

Pascal Tutorial - Chapter 2

End.
Now, let' s play with sounds ! It's easy ! Start playing the sound with Sound, delay for several times, then NoSound. Let's practice ! Cut and paste this program. Run it in BP.

uses crt; begin Sound(440); Delay(100); NoSound; Sound(550); Delay(100); NoSound; end Sound accepts frequency number and it's a word. It is the value of frequency you want to play. Delay accepts value of words. It reflects how many milliseconds (1/100th of a second) you want to delay. NoSound tells the PC to stop playing the note. If you omit Delay, you may not hear any voices. Try it and figure it out !
Pascal is able to calculate things and it made programming much easier. OK guys, here is the convention:
G

Addition (+), Subtraction(-), and Multiplication(*) : If both numbers are integers, it yields an integer result. Otherwise, even there's only one is real, the result becomes real. integer with integer = integer H integer with real = real H real with real = real Division(/) : Always yields real result. Special Division (Div) : It's quite the same as (/), but it always yields integer result.
H

G G

Fine, fine. But how can I apply this ? Real life equation Becomes

y=5x3

y:=5*3; (y is either integer or real) z:=5+4*3; (z is either integer or z=5+4x3 real) a = 3.14 x 7 x 7 a:=3.14*7*7; (a is always real)

file:///F|/Documents/Pascal_Doc/pas/pasl1001.html (4 of 6)8/3/2005 7:42:39

Pascal Tutorial - Chapter 2

b:=14*(5+a); (b depends on a, if a real, b is b = 14 x (5 + a) always real. If a integer, b may be real or integer). Yeah, that's quite an example. But you should know it though. How about the special division : a:=22/7; when a is real, it yields result 3.142856 .... so on otherwise, error.

b is always integer, and it hold 3. Div always round to the floor, I mean b:=22 div 7; not to the top nor not to the nearest integer.Even 98 div 3 will result 32 though normal people consider 32.66666666..... to 33. Pascal could convert the real numbers to integers using Trunc and Round. Trunc behaves similarly like Div, it rounds to the floor always. Round is more moderate though, it rounds to the nearest integer. Evaluate the expressions below and see the result.

uses crt; begin Clrscr; Writeln(Trunc(12.31)); Writeln(Round(12.31)); Writeln(Trunc(31.49)); Writeln(Round(31.49)); Writeln(Trunc(44.59)); Writeln(Round(44.59)); Readln; end;
OK ! That's all for now ! Shall we have the quiz ?

Where to go ?
Back to main page

file:///F|/Documents/Pascal_Doc/pas/pasl1001.html (5 of 6)8/3/2005 7:42:39

Pascal Tutorial - Chapter 2

Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 1, 'Hello, World !' To Chapter 3 about branching (IF)

file:///F|/Documents/Pascal_Doc/pas/pasl1001.html (6 of 6)8/3/2005 7:42:39

Pascal Tutorial - Chapter 3

If I...
Hi ! Nice to meet you again. And now, we're getting closer, right ? We will begin our lesson with conditional branching. What is conditional branching anyway ? Well, it's a if not this, do this, else do that ... ummm similar things like that, you know. We have the conditions, and if the conditions are satisfied, do some statements, otherwise do something else. Pascal provides two ways to implement conditional branching. One uses if, the other uses case ... of. Well, let's discuss the first part : if The structure of if in Pascal is like this :

if condition then begin : : end;


OR, if you add the else part :

if condition then begin : : end <-- notice that the semicolon disappeared now ! else begin : : end;
Now, you ask : How could Pascal determine the process ? If the condition(s) beside the word if is met, then the block begin...end -- just below the if -- will be executed, otherwise, then the block begin..end after the else will be executed. If we have the if structure as the first one -- that is, no else block -- the program execution will
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (1 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

continue after the word end. Let us examine the excerpt of the code below:

if counter<10 then begin counter:=counter+1; writeln(counter); end; writeln('Done');


If the variable counter has the value less than 10, the commands between begin and end will be executed. If it has the value 10 or more, it skips the process just after the end. It means that it does the writeln('Done');. But, don't be mistaken. Even the counter has the value less than 10, after it has done the commands between begin and end, it continues the process to the next statement -- that is writeln('Done');. How about adding else block ? It just as the same as above, but of course with some differences. See the examples, and run them ! Input some numbers and see how it works. To clarify how the if works, refer to these programs

program if1; var i : byte; begin writeln('Enter a number = '); readln(i); if i <= 10 then begin writeln('It is done if i <= 10 only'); writeln('Or you just can omit the begin..end block'); writeln('if there is only ONE command inside (See IF2. PAS)'); end; writeln('We always do this'); end. program if2;
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (2 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

var i : byte; begin writeln('Enter a number = '); readln(i); if i <= 10 then writeln('It is done if i <= 10 only'); writeln('We always do this'); end. program if3; var i : byte; begin writeln('Enter a number = '); readln(i); if i <= 10 then writeln('It is done if i <= 10 only') { Omit the semicolon ; } else writeln('It is done if i > 10 only'); { There is a semicolon } writeln('We always do this'); end. program if4; var i : byte; begin writeln('Enter a number = '); readln(i); if i <= 10 then begin writeln('It is done if i <= 10 only'); writeln('Yeah, you can add begin..end block to else part too !'); end { Omit the semicolon BEFORE the end keyword } else writeln('It is done if i > 10 only.'); writeln('We always do this'); end.
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (3 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

program if5; var i : byte; begin writeln('Enter a number = '); readln(i); if i <= 10 then begin writeln('It is done if i <= 10 only'); writeln('Yeah, you can add begin..end block to else part too !'); end { Omit the semicolon BEFORE the end keyword } else begin writeln('It is done if i > 10 only.'); writeln('Look !'); end; writeln('We always do this'); end.

Nested if
Now how to have some ifs inside an if ? Yeah, right ! That's perfectly legal provided if you don't forget to wrap the begin..end (if there are two or more statements inside). Here is one example:

if i<10 then begin writeln('i is more than 10.'); if j>3 then writeln('j is more than 3.'); end;
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (4 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

Yes, yes ! You may do if inside the begin..end block after the else too. Why don't you do some experiments ?

Combining the conditions


Pascal lets us combine the conditions. Say that if a salesman is qualified to get the project if he has sold 7000 pieces of products and he has at least 3 years of experience. This is the example :

if (productsold>=7000) and (experienceyear>=3) then qualified;


Yes, this cannot be run of course. In this example I would like to introduce the and, or, and xor keywords. The keyword and means that it will return true if both conditions are met. It means that the first begin..end block will be executed only if both conditions are true. The keyword or means that if there is at least one of the conditions is met (or both), then the first begin..end block will be executed. The keyword xor means that if there is ONLY one condition is met (not both), the first begin..end block will be executed. The keyword not means negating all conditions. Then if the condition is true, it will be considered false and vice versa. Now, take a look at these codes :

program if6; var i, j : byte; begin write('Enter a value for i = '); readln(i); write('Enter a value for j = '); readln(j); if (i>3) and (j>4) then begin writeln('This will be done if i>3 and j>4'); writeln('Now change the "and" with "or" and "xor"');
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (5 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

end; end. program if7; var i : byte; begin write('Enter a value for i = '); readln(i); if not (i>3) then begin writeln('This will be done if i is NOT more than 3'); writeln('So, do you understand ?'); end; end.
How will it be if the conditions are more than 2 ? Simply wraps them with brackets, two by two. Note : The conditions inside the brackets will be done first. Example, look at this :

program if8; var i, j, k : byte; begin write('Enter a value for i = '); readln(i); write('Enter a value for j = '); readln(j); write('Enter a value for k = '); readln(k); if ((i>3) and (j>4)) or (k>5) then begin writeln('Yeah !!'); writeln('Now change the bracket orders and run it again !'); end; end. Case..of
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (6 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

Sometimes, you really need to select conditions according some criteria. You're clever ! Use some ifs and let it be done ! But how if the criterium was quite long and complex ? I'm sure that it would be a daunting task to have an if for each. How could we categorize the criteria with some similarities and simplify the problem. Suppose we have to do the grading system for our campus. Say, if the student has 80 or more deserves an A. 70 to 79 is for B, 60 to 69 is C, 50 to 59 is D, and 49 or below is E. The if example would be like this : (Note that mark is a byte)

if mark>=80 then grade:='A' else { 79 or below goes here } if mark>=70 then grade:='B' else { 69 or below goes here } if mark>=60 then grade:='C' else { 59 or below goes here } if mark>=50 then grade:='D' else { 49 or below goes here } grade:='E';
Wow, that's pretty long. Now see this :

case mark of 80..100: grade:='A'; 70..79: grade:='B'; 60..69: grade:='C'; 50..59: grade:='D'; else grade:='E'; end;

file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (7 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

Simple and elegant ! Now, let's learn about readkey statement. It is one of the commands included in crt unit, like clrscr. What it does ? It receives the ASCII code of pressed keyboard button. Well, what is ASCII code anyway? It is some sort of codes that is defined for computers. If we pressed the keyboard, it produces some sort of code, and then the computer translates it into the ASCII code, the code we commonly (programmers) know.
Note for advanced programmers : Don't laugh at this, I have to explain what ASCII code exactly to the non-programmer folks in a practical and easy way without going into the complicated talks ! Yes, we know the "origin" keyboard codes. I do too.)

Readkey is a function, returning a char value. OK, let's see how it works ! program test_readkey; uses crt; var c : char; begin c:=readkey; case c of #8 : writeln('You #9 : writeln('You #13: writeln('You #27: writeln('You #32: writeln('You else writeln('You end; end.

presses presses presses presses presses presses

backspace'); tab'); enter'); escape'); space'); other key');

The program simply waits for a keypress then detects it and quits. Now you asked : Why do we use the # sign ? Because it denotes a character with a code. Suppose #8 means a character with a code of 8. Why don't we use the .. like the previous example ? Because in this case we don't specify ranges of values while the first example did. Like 80..100 means from 80 to 100. Let us detect the arrow keys. Arrow keys, just like function keys, are extended. It means
file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (8 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

it generates a special codes. Now, here is the code to trap them :

program trap_arrow; uses crt; var c : char; begin c:=readkey; if c=#0 then { If extended codes, } begin c:=readkey; { read the code once more } case c of #72: writeln('Up arrow'); #75: writeln('Left arrow'); #77: writeln('Right arrow'); #80: writeln('Down arrow'); end; end; end.
How to detect the ASCII number ? How do you know that ? Easy, with ord. Suppose you want to know the ASCII codes for each keyboard keys. Do this :

program trap_key; uses crt; var c : char; begin c:=#0; while c<>#27 do begin c:=readkey; if c=#0 then begin

{ If extended codes, }

file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (9 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 3

c:=readkey; { read the code once more } writeln('Extended : ',ord(c)); end else writeln(ord(c)); end; end.
Now, press keyboard keys, and see the codes. Combine it with Ctrl, Alt, and Shift and see what happens. To understand the program above, you need to know what while means. The statements inside the while will be repeated on and on as long as the condition (c<>#27) is met. It means the c:= readkey; if c=#0 ... will be repeated as long as c is not equal to character 27. Since character 27 is escape, then the program simply runs until user presses Esc. More about while and other repetitional commands will be explained thoroughly in chapter 5.

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 2 about extending first program To Chapter 4 about constants in Pascal

file:///F|/Documents/Pascal_Doc/pas/pasl1002.html (10 of 10)8/3/2005 7:42:49

Pascal Tutorial - Chapter 4

Constants !
Hello ! We meet again ! Get ready for this short one ! In real world, we meet many constants, such as pi = 3.1415926513... or e = 2.7182818284529... Many of these are impractical number so that it is quite hard for us to remember, at least for some of us. Pascal has a good facility to use the constants. Even it has predefined ones, like pi. How to define our own constant ? Easy, just like a variable definition, we just give const above our constants AND start tayloring our needs.

const : myconst = 1234; : : (Like variable definition)


We just omit the constant type, Pascal will understand it. We can use it just like a normal variable. Example :

const myconst = 1234; var i : word; begin i:=40; writeln(i*myconst); end.


Easy, no ? But heed my word, constants is unchangeable. If you try to change it, Pascal will say error. Wait a minute ! I heard that Pascal's constants can be changed somewhere in the program. You are absolutely right ! Now, the declaration becomes :
file:///F|/Documents/Pascal_Doc/pas/pasl1003.html (1 of 3)8/3/2005 7:43:31

Pascal Tutorial - Chapter 4

const myconst : mytype = thevalue;


example:

const abc : word = 1234;


You may not declare constants with values exceeding the type, like these :

const aaa : word = 100000; ranging 0-65535 } bbb : byte = 123.44; fractional part } ccc : string = 123; type }

{ Illegal because word is integer { Illegal because of the { Illegal because incompatible

Clear ? How can we modify them inside our program ?

const myconst : word = 1234; var i : word; begin i:=40; myconst:=1000; writeln(i*myconst); end.
Easy, just like a normal variable, doesn't it ? That's all for this chapter !

file:///F|/Documents/Pascal_Doc/pas/pasl1003.html (2 of 3)8/3/2005 7:43:31

Pascal Tutorial - Chapter 4

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 3 about branching(if, case..of) To Chapter 5 about looping in Pascal(for, while..do, repeat..until)

file:///F|/Documents/Pascal_Doc/pas/pasl1003.html (3 of 3)8/3/2005 7:43:31

Pascal Tutorial - Chapter 5

Loop A Loop
Hi ! Nice to meet you ! We meet again in Loop a loop. This course suppose to be long and you feel quite bored, don't you ? Well, let's make it quite straight forward. In many cases in programming, we need to repeat processes. In that case, we need some loops. Pascal understands it, therefore it provides three types of loops : 1. For ... to ... do 2. While ... do 3. Repeat ... until Well, let's look inside them :

1. For ... to ... do


Syntax :

For variable := fromwhat to what do begin : : statements / commands : : end;


Example : Suppose i is an integer

For i:=1 to 5 do begin writeln(i);


file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (1 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

if i=5 then writeln('Finished !'); end;


It yields 1, 2, 3, 4, 5, then the message 'Finished !'. You see that inside the begin-end block, you could add any commands you want. If there is only one command inside it, you may omit the begin..end, just like if we have studied before. Suppose you omit the if i=5 ..., so inside the begin..end we have only writeln(i);. We can omit the begin..end, then it becomes :

For i:=1 to 5 do writeln(i);


If we want to count backwards, i.e. from 5 to 1, we just change to into downto, so check this out :

For i:=5 downto 1 do writeln(i);


How about if we want to step more than one, i.e. 1, 3, 5, 7, 9, ...? BASIC provides the STEP how about Pascal ? Well, in this case, Pascal DOES NOT provides such, but if you are creative, you are able to do it. This applies to fractional step as well. BUT, Pascal does include special feature. Check this :

{ Suppose c is a char variable } For c:='A' to 'Z' do write(c,' ');


You may also write For c:='Z' downto 'A' do write(c,' '); as well. You may even write this : (Suppose b is a boolean)

For b:=false to true do writeln(b);


And many even stranger cases than this, if you have studied type statement.
file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (2 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

2. While...do
Well, if you have learned BASIC, you will find that While...do is quite the same as WHILE ... WEND. The syntax is :

While conditions do begin : (statements / commands) : end;


Before we enter the begin..end block, FIRSTLY it checks whether the condition(s) are satisfied. If not, it just skips it out, or else it does any commands inside begin.. end. When it reaches the end, it checks the condition again. If it is satisfied, then loop again. If not, go out. It goes on and on. The logic is like this :

For example :

i:=1; While i<6 do begin writeln(i); i:=i+1;


file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (3 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

end;
You may also combine conditions with AND, OR, or XOR. If Pascal does not provide STEP like BASIC, you can change the for into while that has a lot more flexibilities.

3. Repeat ... Until.


The syntax is :

Repeat : statements / commands : Until condition;


Unlike While...do, Repeat...Until does not check for conditions at first. It just enters the block and do anything inside. Beware that Repeat...Until does not require begin...end. AT THE END, it then checks whether the conditions are satisfied or not. If SO, it QUITs. That is the third difference from the While...do. If it does not, it simply loops back. So, the logic is like this :

For example :

(uses crt first) Repeat Until Keypressed;


file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (4 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

OR :

i:=1; Repeat writeln(i); i:=i+1; Until i>5;


In While...do and Repeat...until there is a chance to get an infinite loop. Simply give an impossible conditions. But remember, that this also a drawback if we don't use it carefully. Example :

While 2<4 do begin (bla bla bla ...) end;

OR

Repeat (bla bla bla ...) Until 3>5;

(2 is always less than 4, so able to exceed 5, so While...do get an infinite loop) goes forever).
Or you might just:

(3 is never be Repeat...until

While true do begin (bla bla bla ...) end;

OR

Repeat (bla bla bla ...) Until false;

(They yield the same effect as previous examples : infinite loops). You may also nest the loop, i.e. a for inside another for, a While in another While, etc. And even, you may nest one form of loop inside another form, like a for
file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (5 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

inside Repeat...Until. Example :

Repeat : For ... do begin : end; : Until ...;


It is perfectly legal. How about if I want to bail out in a middle of loops ? Easy, use Break. Example :

begin for i:=1 to 10 do begin writeln(i); if i=5 then break; end; writeln('Finished !'); end.
It yields :

1 2 3 4 5 Finished

file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (6 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

Got it ? Break means go out from current loop. It can also be applied on while... do and repeat...until. How about nested loop ? Break only applies on its begin...end block. Check out the program below :

begin for i:=1 to 4 do begin for j:=1 to 5 do begin writeln(i,' ',j); if j=3 then break; end; writeln('Next i loop'); end; writeln('Finished !'); end.
It yields :

1 1 1 2 1 3 Next i loop 2 1 2 2 2 3 Next i loop 3 1 3 2 3 3 Next i loop 4 1


file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (7 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

4 2 4 3 Next i loop Finished !

Another example :

begin for i:=1 to 5 do begin for j:=1 to 3 do begin writeln(i,' ',j); end; if i=2 then break; end; writeln('Finished !'); end.
It yields :

1 1 1 2 1 3 2 1 2 2 2 3 Finished ! Break only does its function in its current scope (begin...end block) How about if I want to skip instead of quitting current loop ? Well, use Continue instead of Break. Replace all Break in previous examples with Continue and see what happens.
file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (8 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

Extra !!!
Here we are, have additional lessons : 1. Random and randomize 2. Halt 3. ASCII characters Random and randomize All of them are used to generate random numbers. Random numbers are always used to simulate the reality, where certainty is hardly guaranteed. We are here not to discuss how to make it, but how to use these two commands.

Randomize is used to start the random generator, so that it is only used at the first initialization, placed after the begin in the main block. It is enough to call it once, even though it may be called several times. Random is used to get the random number. Its parameter is the highest possible number that can be generated. Suppose : num: =random(50); It means that num will contain any integer number between 0 to 49.
Look at next example :

var i : byte; begin randomize; for i:=1 to 100 do write(random(30),' '); end.
Let's remove the randomize. Run it for several times and note that when it runs, if you don't use randomize, the computer will result the same number. So, always use the randomize so that the computer generate quite a good random numbers. Halt
file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (9 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 5

Just stop and return to system. Usage : Halt; You may place it anywhere you want. If computer encounters the halt, it will stop the program execution and back to the operating system. ASCII characters There are 128 basic ASCII characters and 128 extended ASCII characters. Writing ASCII characters is as simple as using writeln; Example :

writeln(#30); { Writing ASCII code 30 }


Try changing 30 with other numbers between 0 to 255. That's all. We'll have a quiz. Well, it's not hard, actually, but it is harder than before. Bye for now !

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 4 about constants To Chapter 6 about procedures and functions

file:///F|/Documents/Pascal_Doc/pas/pasl1004.html (10 of 10)8/3/2005 7:43:36

Pascal Tutorial - Chapter 6

What are Procedures and Functions

Hello ! Nice to meet you again ! How is the lessons ? It was a great quiz, wasn't it ? Well, time's getting more difficult to pass through. I've got a very busy semester, so that I may not even keep my promise in the Web........ Sorry ! But I keep moving on, don't worry ! Contents : 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Simple syntaxes Scope Local variables and global variables Parameters (pass by value and pass by reference) Ignored return values Inter-procedure and/or functions call Nested syntax Recursive calls Inter-referenced procedures/functions Break down problems

OK ! Let's start ! We know that Pascal is a structured language. It means that the problems is divided into steps and subproblems and then is coded in a structure. This division is made through procedures and functions. Well, what the hell are procedures and functions ? It is like a subprogram that has its own begin..end. It is useful when a part of program must be performed more than once in scattered parts of the program. May be it is a bit murky, but let's take a look on its syntax :

Procedure procname; begin : { commands } :


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (1 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

: end; begin end.


This is the simplest form of a procedure. (Functions are discussed later) As we can see that procedures are always placed ABOVE the main begin...end. Example :

uses crt; procedure pause; begin writeln('Press any key when ready ...'); readkey; end; begin clrscr; writeln('ABC'); pause; { Make a call to procedure pause } writeln('Hey, let''s pause !'); pause; { Make a call to procedure pause } writeln('Pause again !'); pause; { Make a call to procedure pause } end.
Run the previous example. Look at this example :

uses crt; begin


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (2 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

clrscr; writeln('ABC'); writeln('Press any key when ready ...'); readkey; writeln('Hey, let''s pause !'); writeln('Press any key when ready ...'); readkey; writeln('Pause again !'); writeln('Press any key when ready ...'); readkey; end.
It gives the same effect as the first example, isn't it ? Which is nicer ? Writing silly same phrases or efficiently using procedures. If you are normal guy, you would love the first example. It is clearer and cleaner. Easy to read. Especially the lines to be replaced with procedures are very long. Procedures, and functions too, make the codes healthier and easier to read. It makes life easier in debugging the program, too. Procedures could have its own variable, called local variable. The variable is only known inside the procedure. For example :

procedure foo; var a : byte; begin a:=10; writeln(a); end; begin { main begin...end block } foo; a:=15; { This will be an error, since a is only known inside foo }
file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (3 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

end.
Global variable is a variable that is known throughout the program and its procedures or functions. Example :

var b : byte; procedure foo; var a : byte; begin a:=10; b:=15; { This is legal } end; begin { main begin...end block } foo; a:=15; { This is illegal } b:=5; { This is perfectly legal } end.
If a local variable is declared inside a procedure with the same name as the global variable, the global variable is overrided. For example :

uses crt; var a : byte;

{ This is the global variable }

procedure foo; var a : byte; begin


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (4 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

a:=15; writeln('foo is ',a); end; begin a:=10; writeln(a); foo; writeln(a); end.
Functions are pretty much the same, except it has a return value. It just like mathematic functions. Syntax :

function funcname : returntype; begin : { commands } : : end; begin end.


Functions are also placed before the main begin...end. Example :

uses crt; function yesno : boolean; var c : char; begin


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (5 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

write('Are you sure (Y/N) ? '); repeat c:=upcase(readkey); { Make it upcase letters } until (c='Y') or (c='N'); writeln(c); if c='Y' then yesno:=true else yesno:=false; end; var x : boolean; { Don't be tricked, this is a LOCAL variable of main block } begin clrscr; writeln('Discard all changes made'); x:=yesno; if x=true then writeln('Changes discarded !'); else writeln('Changes saved !'); writeln('Quitting'); x:=yesno; if x then begin writeln('Process terminated !'); halt; end; writeln('Quit cancelled !'); writeln('Continuing process ...'); end.
Can you see a big difference between procedures and functions ? Use function if we want to get a return value. Suppose the yesno function. It returns true if user presses y and returns false if user presses n. Yes, yes, it is pretty complex. But, try to understand.
file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (6 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

Or... try to understand this simple excerpt : (This is only the begin...end part. All above is the same as the previous one.)

begin x:=yesno; if x then writeln('You answered "Yes"') else writeln('You answered "No"'); end.
In a well-structured program, we use global variables as minimal as possible and use the optimum amount of local variables. How can we ? Well, practice a lot ! Sometimes, we need some value to be known inside a procedure. In that case, we need parameters. We can put parameters just beside the procedure's or function's name, suppose :

procedure myproc (a:integer; b:real); begin : : end; function abc (a,b : integer; c:byte) : longint; begin : : end;
Let's see it in 'real life' !

file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (7 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

uses crt; procedure makewin(x1,y1,x2,y2 : byte); var i,j : byte; begin { top } gotoxy(x1,y1); write(#201); for i:=x1+1 to x2-1 do write(#205); write(#187); { middle } for i:=y1+1 to y2-1 do begin gotoxy(x1,i); write(#186); for j:=x1+1 to x2-1 do write(' '); write(#186); end; { bottom } gotoxy(x1,y2); write(#200); for i:=x1+1 to x2-1 do write(#205); write(#188); end; begin makewin(1,1,30,8); makewin(10,10,60,18); end.
Simple example above tell us about making a window in a coordinate (x1, y1) to (x2, y2). With this engine, we could make many windows with just entering coordinates and the SAME code. Parameters makes us easier to customize the procedures and functions. Let's see this example :

file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (8 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

function factorial(n : byte):longint; var i : byte; result : longint; begin result:=1; for i:=1 to n do result:=result*i; factorial:=result; end; var x : byte; begin writeln('Enter a value : '); readln(x); writeln(x,'! is ',factorial(x)); end.
Those previous examples are widely used in programming life. Let's look at this :

procedure foo(a : byte); begin writeln(a); {15} a:=10; writeln(a); {10} end; var x : byte; begin x:=15; writeln(x);

{15}

file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (9 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

foo(x); writeln(x); end.


It outputs :

{Still 15}

15 15 10 15
At first, x is 15, then passed to procedure foo as passing parameter a. Eventhough it is legal to modify a inside foo, the value of x is unaffected anyway. This type of passing method is called PASS BY VALUE. How about this :

procedure foo(var a : byte); { See the difference?} begin writeln(a); {15} a:=10; writeln(a); {10} end; var x : byte; begin x:=15; writeln(x); foo(x); writeln(x); end.

{15} {10}

file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (10 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

It outputs :

15 15 10 10
If we add var before a as passing parameter, the value of x is changed whenever a is changed. This type of passing method is called PASS BY REFERENCE. We must pass by value if the parameters are not necessarily be changed in the procedure. If the parameters need changing and the change is important to be known by the caller, use pass by reference. In Borland Pascal 7.0, we could omit the return values of a function. In our examples so far, we use readkey, right ? Readkey is actually a function but a built-in one. Before version 7.0, we must take a return value of it (readkey), like this : c:=readkey;. But, from version 7.0, we could omit it by just calling readkey like this : readkey; You CAN call another procedure inside a procedure or function as long as the caller procedure/function lies below it. Example :

procedure a; begin : : end; procedure b; begin : a; { legal } end;


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (11 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

begin : end.
It is legal for procedure b to call procedure a. But procedure a can NEVER call procedure b. It is ILLEGAL. Function can call procedures, procedures can call functions, functions can also call other functions as long as the caller is below the target (the one which is invoked). So, you'd better arrange the procedures and functions you might have. If the if-structure and loop structures could nest each other, can procedures and functions nest each other ? YES, IT COULD ! Example :

procedure e; { e cannot access a, b, c, and d } begin : end; procedure a; procedure b; begin c; {illegal} e; {legal} end; procedure c; begin b; {legal} e; {legal} end; begin : b; {legal} c; {legal} e; {legal} end; procedure d;
file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (12 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

begin : b; {illegal} c; {illegal} a; {legal} e; {legal} end; begin : b; {illegal} c; {illegal} a; {legal} d; {legal} e; {legal} end.
Procedure c can call procedure b since c is below b, but procedure b cannot call procedure c. Procedure a, whose begin..end block below procedure b and procedure c, can call procedure b and c. Procedure b and c cannot be access ed by procedure d and main block. So, nested procedure can only be accessed by their brothers/sisters (among the same level of nested procedure), and their parent (the procedure who endow them). Accesses between brothers follow the rule of normal procedure calling. Practice a lot ! How about calling ourselves ? Well, IT IS PERFECTLY LEGAL too !! Example :

procedure a; begin a; end; begin


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (13 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

a; end.
But it will say error since it call itself ENDLESSLY. The method of calling oneselves is called RECURSIVE CALLS. In recursive calls, we must : 1. Provide terminating conditions. 2. Be extremely careful in coding. 3. Not use too many recursive calls, say 50,000 times.
If not, you could find these symptoms :

1. 2. 3. 4. 5. 6.

Hang / stopped responding. Computer "auto-murmur". Beeping eratically. Auto-reboot. Auto-format (no...no ! Just kidding !) Other unexpected result

Let's see a very good example of recursive calls : It's factorial. See the non-recursive method of factorial function above, then see this recursive version :

function factorial (n:byte):factorial; begin if n<2 then { This is the terminating condition } factorial:=1; else factorial:=n*factorial(n-1); { This is the recursive part } end; var x : byte; begin writeln('Enter a value : '); readln(x); writeln(x,'! is ',factorial(x)); end.
file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (14 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

If x = 5, At first call, n second call Second call, n third call Third call, n fourth call Fourth call, n fifth call Fifth call, n back to above so 4th call becomes back to above so 3rd call becomes back to above so 2nd call becomes back to above so 1st call becomes

= 5. Factorial:=5*factorial(4); => need = 4. Factorial:=4*factorial(3); => need = 3. Factorial:=3*factorial(2); => need = 2. Factorial:=2*factorial(1); => need = 1. Factorial:=1; : Factorial:=2*1; (=2) : Factorial:=3*2; (=6) : Factorial:=4*6; (=24) : Factorial:=5*24; (=120) => inserted => inserted => inserted => inserted

As you may see that factorial in recursive method is simpler than ever. Suppose you miswrite n-1 to n, the terminating condition would never be functional. So, it will loop forever ! Be careful ! Well, we come to a quite advanced matter. It is sometimes used though. Interreferenced calls. It means that 2 procedures or functions could call each other.

procedure a; begin b; { illegal } end; procedure b; begin a;


file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (15 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

end;
As you may see, calling a from b is legal, but calling b from a is illegal. It sometimes necessary for a to call b and for b to call a. The real-life problem is like context-sensitive help. Sometimes the description, when it is pointed or clicked, it call the index. But also, the index has to call its description after a topic is picked. The solution is : FORWARD CALL. Example :

procedure b; forward; procedure a; begin b; { now legal } end; procedure b; begin a; end;
Use the statement forward to make b visible to a, so a can call b. It is more dangerous than recursive call. If you are not careful, you may find the same symptoms as recursive call did. What you must do is also the same as in the recursive call. Just beware : Calling procedures uses a part of memory called stack. Reciprocal calls wastes stacked much faster than recursive calls. If you run out of stack, your program would stuck. How could I break down complex problems ? How good your ability in breaking problems is actually depends on your experience in programming. But, I can only tell you this : 1. Identify your problem correctly. Suppose you have to make a program to record employee data for a bank. What is it for ? Employee data for just an information or for salary statistics or what ? 2. Separate one process from another. Suppose you do employee data for information. The processes are record it to
file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (16 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

3.

4.

5.

6.

7.

8.

9. 10.

disk, edit data, delete data, sort data, or print and display data, or search data. List the solution steps of each process. Suppose you first do record data. You take input from keyboard and record it to disk. You ask if user wants to input again or not. List the data requirements. Employe data may require name, position, residence, office location, civil status, age, sex, years of service, and so on. Determine the output requirements. You may design the output like a list of employee data. It is useful for comparison reason. Or may be you would love to make it like an archive form for complete document. Or you ask the user and they can choose. Construct an algorithm to do the process. Suppose taking data from keyboard, may sound : input name, input age, input position, and so on. Then prepare file, record the data to disk, close the file. Ask user if they want to input some more. If they do, repeat the whole process, otherwise quit. Use your creativity to expand the input, process and output. Don't stick the model from input to output. You may add your creativity. Client may want the output format exactly the same as they wish. Perhaps you could give suggestions in arranging them, or perhaps if you think some data is important to display or print, go tell the client. Think of all other useful things to add in program. Suppose you want to add the feature of Auto-Correct, to minimize the humanerror. You probably record some usual names, like Jim, Dave, and so on. If user types Dabe, for example, your program can give suggestion to change it to Dave. Yeah, somethings like that. Implement it, step by step. Don't let all dreams be dreams. Let it comes true. Combine all the programs to tackle the problem. All the module of each process should be gathered. You may want to generalize your input procedure to fit in each module to save energy and time. Combine them using menus. And don't forget to make finishing touches, to make the software looks great.

Well, that's one of the complex problems. You may follow the guidelines. OK, that's all for today ! Have a good quiz ! =)

Where to go ?
file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (17 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 6

Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 5 about looping To Chapter 7 about arrays

file:///F|/Documents/Pascal_Doc/pas/pasl1005.html (18 of 18)8/3/2005 7:43:43

Pascal Tutorial - Chapter 7

Mastering Array
Hello ! Nice to meet you ! It seems you are cleverer day by day. You are even be able to write simple games ! I have a little problem on my health recently, so I have to get some good rest. Topics that will be discussed this time are : 1. 2. 3. 4. 5. 6. What is an array. Declarations. Using arrays. Two-dimensional array. Multi-dimensional array. Special case of array.

Let's begin ! What is an array exactly ? Well it is like a variable that is able to store a bunch of data. Think about drawers. Drawers have several latches, don't they ? Each latch could keep things safe. Eventhough they have so many latches and store things, they are called drawers. Confused ? Let's look at the declaration :

var arrayname : array [x..y] of vartype;


Example :

var ourdata : array[1..100] of byte; myarray : array[5..25] of char;


Yes, yes, name it whatever you want. Array declarations are located inside the var block. You can declare arrays as global variable or local variable, just whenever you want it. How to use it ? Suppose, I have an array :

file:///F|/Documents/Pascal_Doc/pas/pasl1006.html (1 of 6)8/3/2005 7:43:47

Pascal Tutorial - Chapter 7

var x : array[1..30] of byte;


It means that x has 30 elements ranging from 1 to 30. Each element can be accessed individually, not affecting others. Suppose I want to access the first element, it would be :

x[1]:=10; with 10 }

{ Suppose I want to fill the first element

Then, I would like to access the second element :

x[2]:=15;
If you check the first element, it is still 10. Even I have assigned the second element with 15, the first one still retain its originality. Check out this simple program to gain better understanding :

var a : array[1..10] of byte; begin a[1]:=10; a[2]:=15; a[3]:=a[1]+a[2]; writeln(a[1]); writeln(a[2]); writeln(a[3]); end.
As you can see that the variable a could store multiple values. Each element could be accessed without interfering others.

file:///F|/Documents/Pascal_Doc/pas/pasl1006.html (2 of 6)8/3/2005 7:43:47

Pascal Tutorial - Chapter 7

In what case the array contribute to the applications in real-life ? Suppose you have a list of names. You would probably prefer to have an array of names instead of declaring each variable names. Suppose : It would be nicer to store 10 names with an array like this :

var names : array[1..10] of string;


rather than having each variables like this :

var name1, name2, name3, name4, name5, name6, name7, name8, name9, name10 : string;
Array makes us easier in writing the contents. Writing all the names with an array names above, would be :

for i:=1 to 10 do writeln(names[i]);


You would love doing that rather than writing writeln statements with 10 variables intact. It is much easier and simpler. What about if I have a table of x and y, could I access it with array too ? Yes ! But, you will need a two-dimensional array. It is the same, but the declaration is like this :

var table : array[1..5, 1..3] of byte;


That table is 5x3 in size. How to access it ? Well, like this :

table[5,3]:=5;
file:///F|/Documents/Pascal_Doc/pas/pasl1006.html (3 of 6)8/3/2005 7:43:47

Pascal Tutorial - Chapter 7

table[1,2]:=4; table[4,1]:=table[1,2]*table[5,3];
Yes, it is pretty much the same. Well, array helps you make spreadsheets, just like Lotus 1-2-3 or Excel. How about the three-dimensional table or more ? It also the same. Look at the declaration :

var table3d : array [1..5, 1..4, 1..6] of byte;


Accessing it is all the same :

table3d[3,4,5]:=6;

And so on. You may extend it to 4-D array, like :

var table4d : array[1..2, 1..3, 1..4, 1..5] of byte;


Accessing it is analogue to the previous examples. 5-D array, or even 100-D array is just the same in concept. Here are the special case of array in Pascal : You can declare array like this :

x : array[3..40] of integer; idx : array['A'..'Z'] of string; a : array['a'..'z'] of byte; n : array[byte] of integer; { The same as array [0..255] of integer; }
Or even like this :

file:///F|/Documents/Pascal_Doc/pas/pasl1006.html (4 of 6)8/3/2005 7:43:47

Pascal Tutorial - Chapter 7

schedule : array[Monday..Saturday] of string; carprice : array[Honda..Suzuki] of longint;


These types of array will be discussed in depth in chapter 10. Accessing them is just the same :

x[3]:=4; x[1]:=5; {Illegal} idx['D']:='Dave'; idx[1]:='XX'; {Illegal} a['a']:=65; n[0]:=1; n[255]:=10; n[5]:=5; schedule[Monday]:='Go meet clients at 10am'; carprice[Honda]:=15000; {US$}
Well, well ! Got to practise a lot. Accessing array idx and a with loop is similar. Suppose c is a char variable :

for c:='A' to 'Z' do idx[c]:=''; for c:='a' to 'z' do a[c]:=random(100);


This kind of array, for me, is seldom used. The more usual ones are the array of schedule and carprice above. They use enumeration types. This will be discussed in chapter 10. OK, that's all for this time !

file:///F|/Documents/Pascal_Doc/pas/pasl1006.html (5 of 6)8/3/2005 7:43:47

Pascal Tutorial - Chapter 7

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 6 about procedures and functions To Chapter 8 about processing strings

file:///F|/Documents/Pascal_Doc/pas/pasl1006.html (6 of 6)8/3/2005 7:43:47

Pascal Tutorial - Chapter 8

Dancing with Strings


Hi ! We meet again ! Now, I would like to discuss about strings in depth. How are you doing ? Well, I'm having health problem this day and I need a rest. I am still able to write this to you. Cheer ! Topics discussed this time are : 1. String as array of char 2. Limited length string 3. String manipulation commands :

Length, Copy, Pos, Val, Str, Concat, Insert, Delete, Fillchar 4. String as a pointer of characters (PChar), introduction
Let's begin !

Actually, string is an array of characters. So, suppose s is a string. s[1] is equal to the first character of s. s[2] is the second character, and so on. Look at this :

var s : string; begin s:='Hello, dear'; writeln(s); s[1]:='J'; { Replace the s[5]:='y'; { Replace the writeln(s); { Jelly, dear writeln('The length of s is end.

first character with J } fifth character with y } } ',ord(s[0]));

Zeroth character is the character form of the length of that string. So, ord(s[0]) denotes the actual length of s. You may replace it with length(s) which gives the same effect.
file:///F|/Documents/Pascal_Doc/pas/pasl1007.html (1 of 6)8/3/2005 7:43:53

Pascal Tutorial - Chapter 8

Normally, strings would hold 80 characters in maximum. Suppose you would have a set of strings that is about 10 characters long. Declaring each as normal string would be a waste of space. Pascal provides facility to limit the string length. If you would like to have a string that has maximum limit of 10 characters, you would write :

var s : string[10];
Pascal also provides routines to manipulate string : 1. Length : return string length. Syntax : length(s) Example : n:=length(s); Suppose s:='Who are you ?'; n would be 13. 2. Copy : get an excerpt from a string. Syntax : copy(s,from,howmuch) Example : st:=copy(s,5,3); Get an excerpt of 3 characters from s, beginning from the 5th character. Suppose s:='Who are you ?'; st will be 'are'. Note that if index from is greater than the length of s, st would be empty, example :

st:=copy(s,15,4); { empty string } If howmuch exceed the end of the string s, it returns the remainder of the string,
example :

st:=copy(s,9,10); st would be equal to 'you ?'


3. Pos : get the position of a substring from a string. Syntax : Pos(substr,s) Example : n:=pos('are','Who are you ?');{ n:=5; } If the substring is not found, it returns 0. 4. Val : converts strings to numeric. Syntax : val(strvar,numvar,errorcode) strvar is a string variable to be converted, numvar is any numeric variable either integer or real, and errorcode is an integer variable that holds the error code. If errorcode is 0, conversion success. Otherwise, it points at the position of strvar that cause failure. Example :

file:///F|/Documents/Pascal_Doc/pas/pasl1007.html (2 of 6)8/3/2005 7:43:53

Pascal Tutorial - Chapter 8

var s : string; e : integer; r : real; begin write('Enter a number : '); readln(s); val(s,r,e); if e<>0 then writeln('Error at position : ',e); else writeln('That was : ',r:4:3); end.

5. Str : converts numeric into strings. Syntax : str(numvar,strvar) Example :

var s : string; i : integer; begin write('Input an integer : '); readln(i); str(i,s); writeln('That was : ',s); end.
Note that if you deal with real, you may format it before you convert it into strings. Suppose r is a real variable, s can be like this :

str(r:4:3,s);
s consists of 4 digits before the decimal point of r, and 3 digits after the decimal point. Example :

file:///F|/Documents/Pascal_Doc/pas/pasl1007.html (3 of 6)8/3/2005 7:43:53

Pascal Tutorial - Chapter 8

var s : string; r : real; begin write('Input a real : '); readln(r); str(r:4:3,s); writeln('That was : ',s); end.

6. Concat : Concatenates two or more strings Syntax : concat(s1,s2,...,sn) Example : st:=concat(s1,s2); If s1='ABC' and s2='DEF', st would be 'ABCDEF'

st:=concat('Borland ','Pascal ','ver. ','7.0'); Would be 'Borland Pascal ver. 7.0' You may put as many parameters to concat as possible. If the resulting string
length is more than 255, it will be truncated to 255. Concat is the same if we use plus sign (+). For example : st:=concat('ABC','DEF'); is the same as st:='ABC'+'DEF'; 7. Insert : Insert a string inside another string from indexth character Syntax : insert(source,target,index) Example :

var s1, s2 : string; begin s1:='not '; s2:='I do love you'; insert(s1,s2,6); writeln(s2); { I do not love you } end.

file:///F|/Documents/Pascal_Doc/pas/pasl1007.html (4 of 6)8/3/2005 7:43:53

Pascal Tutorial - Chapter 8

If the result string length is more than 255, it will be truncated into 255 as well. 8. Delete : Deletes n characters from string s starting from index i. Syntax : delete(s,i,n); If index i is greater than length of s, s is unchanged. If n specifies more characters than remain (starting from i), the remainder of the string is deleted. Example :

var s : string; begin s:='I am not responsible for that !'; delete(s,6,3); writeln(s); { I am responsible for that } end.

9. Fillchar : fill string s with character c until s is n-1 char long. Syntax : fillchar(s,n,c); Beware : s[0] is overrided, so don't forget to add s[0]:=chr(n-1); to normalize it.

var s : string; begin fillchar(s,51,'='); s[0]:=chr(50); end.

Actually, those procedures or functions can be read from Pascal's help. So, refer to Borland Pascal help if you want working examples. You can even make your own string functions. If you don't understand, e-mail me.

file:///F|/Documents/Pascal_Doc/pas/pasl1007.html (5 of 6)8/3/2005 7:43:53

Pascal Tutorial - Chapter 8

As Borland Pascal 7.0 arrives, we know that C style string is adopted in. In C, we view strings either as an array of characters or a pointer of characters. Pointer will be discussed in second lesson. The main reason is that Pascal can not maintain strings larger than 255. Then a new type of string is employed : PChar, a pointer of character. Pascal string consists of : one byte for its length then the contents. In PChar, just like C, we don't recognize the length. All we know is when the string stops -- that is when we encounter ASCII 0 (NULL). That null stop style makes the C style of string called NULL TERMINATED STRING. All string manipulation routines for PChar are adopted from C. If you would like to know each one of it, you should learn C, where it came from. Nevertheless, you could even learn it in Borland Pascal help too. The details of pointers and this type of string could be found in my second lesson of Pascal. You could compare two strings just like numbers, too ! Suppose s1 and s2 are strings, you could do like this :

if s1 < s2 then .... (and so on ...)


That's all for this time.

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 7 about arrays To Chapter 9 about records

file:///F|/Documents/Pascal_Doc/pas/pasl1007.html (6 of 6)8/3/2005 7:43:53

Pascal Tutorial - Chapter 9

Best Records
Hello ! We meet again ! How are you ? Good ? Thank God ! Ready to take this lesson ? Good ! Topics discussed this time are : 1. 2. 3. 4. 5. 6. What is records Declarations How to use it (with...do) Nested records Conditional records Extras : Sorting methods ! (Bubble, Shell, and Quick)

Let's begin !

Like an array, record is just grouping various data under one name. However array can only store ONE type of data, but record can store ALL kind of data, including arrays. How could we declare a record ? Actually, record is a customized kind of data type. So, I hereby want to introduce you the type part of Pascal program. The type part is placed BEFORE the var part. Let's look at the outline of somewhat-complex-Pascalprogram-structure :

program programname; uses units1, units2, ...; type name = variabletype; : : (type declarations) var (variable declarations) : :
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (1 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

(procedures and functions) : : begin (main begin...end block) : : end.


You can declare variable type anywhere, but normally, before variable declaration. Here is one example to declare a record :

type TEmployee = record name address age position commision end; var x : TEmployee;

: : : : :

string[25]; string[40]; byte; string[10]; real;

Record will be no use after its declaration if there are no variable using it. So, you can use the declaration if you have a variable of that kind of data. In this case, x. So, let's look at the main begin...end block :

begin x.name x.address x.age

:= 'Paul Doherty'; := '11th Kingston Avenue'; := 35;

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (2 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

x.position := 'Salesman'; x.commision := 0.10; end.


Through x, all accesses are available. Beware that : TEmployee.name is NOT the way to access name field. Each data stored inside a record is called FIELDS. So, the TEmployee has fields of name, address, age, position, and commision. Sometime, if we have so many fields inside a record, we find it is bothersome to write the variable, like the example above : so many x has to be written. We may want to simplify it. The example above (the main begin..end block) can be written as follows :

begin with x do begin name address age position commision end; end.

:= := := := :=

'Paul Doherty'; '11th Kingston Avenue'; 35; 'Salesman'; 0.10;

Suppose you have a variable called name, how can Pascal distinct it from the record fields of name ? Within with...do statement, the record field overrides. If you want to assign the name variable, not the one which is the field of x, do it outside of with... do block. You can use array of records. Suppose you have declared the TEmployee record tag. Now you want to declare an array of it. It's simple :

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (3 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

var MyEmployee : array[1..100] of TEmployee;


Just like declaring normal arrays. How to access it ? Suppose I access the first element :

begin MyEmployee[1].name MyEmployee[1].address MyEmployee[1].age MyEmployee[1].position MyEmployee[1].commision end.


With with...do it's all the same :

:= := := := :=

'Paul Doherty'; '11th Kingston Avenue'; 35; 'Salesman'; 0.10;

begin with MyEmployee[1] do begin name := 'Paul Doherty'; address := '11th Kingston Avenue'; age := 35; position := 'Salesman'; commision := 0.10; end; end.
Can we do nested record as looping and conditional do ? Good question ! It IS perfectly legal. But you may do it like this :

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (4 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

type TCommision = record sales production absence end; TEmployee = record name : address : age : position : commision : end;

: real; : real; : real;

string[25]; string[40]; byte; string[10]; TCommision;

In that example, the field commision of TEmployee record tag is a record of TCommision. How to access the sales field of commision ? Easy ! Suppose you have declared x as a variable of TEmployee : x.commision.sales:=0.5; Just add another period after commision then type sales, that's it ! Using arrays are pretty much the same ! MyEmployee[1].commision.sales:=0.3; You may nest records more than 2 steps. To accomplish such task you may use analogies to the previous example. To access them is quite the same, just type down the period and subfield name and so on. The deeper you nest, the longer you type. You may use with...do block inside another with...do block to access nested records. Go on, try it ! One special caution should be taken : Pascal give a special gift that may not even exist in other programming languages : CONDITIONAL RECORDS. What is conditional records exactly ? Conditional records are record with a variable length depending on the contents of a field. That field whose contents become so important is called pivot field. Let's look at this :

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (5 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

type TEmployee = record Name : string[20]; Age : byte; case department:byte of 0: (sales : longint); 1: (product : integer); end; TEmployee has the field of name, age, and department. Department is a byte field. If the department is 0, then the TEmployee contains sales field as the next field. Or else, in this case 1, it has product field as the next one. The usage is quite
rare though. Yet, this is very useful indeed. Need to know : You have already known division in Pascal. How can we get the remainder ? Well, use mod :

x := n mod 13; by 13 } EXTRA !

{ x is the remainder of n after divided

As the lesson in this chapter is quite short, I decided to give you some extras. That is sorting methods. Three of which I'm going to tell you is the extreme : 1. Bubble Sort (Well-known of its simplicity but very slow) 2. Quick Sort (Well-known of its speed but quite complex) 3. Shell Sort (Quite fast but not too complex) The simplest but quite powerful sorting methods among all is bubble sort. It is very easy to implement. The idea is bubbling up the desired data into sorted order. Suppose we want to sort the data ascendingly, earlier or smaller data will come up gradually. The main layout algorithm for this, is just like this :

for i:=1 to NumberOfData-1 do


file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (6 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

for j:=i+1 to NumberOfData do if Data[i]>Data[j] then swap Data[i] with Data[j];


That's all ! Very simple. If you want to sort descendingly, replace the '>' with '<' in If statement. Sorting a record using specific fields, like name or something just replace the if statement :

if Data[i].name<Data[j].name then ....


Unfortunately there are no first key and second key, as you may note. Pascal knows only the first key. How to accomplish the second key ? Errrm, you may sort the data twice. First with the second key, finally with the first key. Easy, no ? Swapping datas require a temporary variable. The swap Data[i] .... can be changed into :

begin t:=Data[i]; Data[i]:=Data[j]; Data[j]:=t; end;


Note that t is a temporary variable. Variable t and the Data must be in the same type. Suppose Data is an array of byte, t must also an array of byte. Otherwise, computer will spit an error. Some Pascal version may need special care in swapping records. Suppose t is a record and Data is an array of record of the same type. You may see the exchange is done through the fields. Example the record consists of name and address, the swapping is done like this :

begin t.name:=Data[i].name; t.address:=Data[i].address;


file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (7 of 18)8/3/2005 7:43:57

{ one by one }

Pascal Tutorial - Chapter 9

Data[i].name:=Data[j].name; Data[i].address:=Data[j].address; Data[j].name:=t.name; Data[j].address:=t.address; end;


Yup ! Simple, eh ? All sorting methods need swapping and the swapping is done in the same way. The algorithm : (If you are curious, you may read through. If you are as just practical as me, you may skip this part without losing lesson details.) The idea of bubble sort is just comparing all the data. The first data is compared to the second, to the third, and so on until the end of the data. Then the second data compared to the third, to the fourth, so on. Then the third data to the fourth and so on ... and so on. Why it is called as the Bubble sort. It is because that the comparison and the swapping make the desired data to gradually come up like a bubble. Example : Suppose we have data like this 5 3 8 4 1 7 6 2 (unsorted) Follow the algorithm -- i.e. comparing the first and the second data : 5 and 3. 5 is greater than 3, hence it must be swapped out 3 5 8 4 1 7 6 2 (1st step) Then the first and the third. Since 3 is smaller than 8, no swapping is necessary. Then the first and the fourth : No swapping either. The first and the fifth -- since 1 is smaller than 3, it must be swapped : 1 5 8 4 3 7 6 2 (4th step) And so on, till the end of data, the sequence remains the same. Then

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (8 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

the second with the third -- no changes this time. The second with the fourth. Change must be done : 14853762 then becomes : 13854762 And so on : 12854763 12584763 12485763 12385764 12358764 12348765 12347865 12346875 12345876 12345786 12345687 Finally, sorted : 1 2 3 4 5 6 7 8 You see that smaller data comes up like bubble and bigger data sank down gradually, as you may note the 8. That's why this method is called bubble sort.

Before we go to quick sort, let's discuss shell sort. Well, shell sort is a kind of exchange sort, actually, but it is optimized for speed. This algorithm is invented by Donald Shell, long long ago. His algorithm was named according to his name. The idea is comparing two datas in a quite distance in an array. Suppose, I have 8 data. First I compare the first with the fifth, and so on. Look at this excerpt :

for gap:=(NumberOfData div 2) downto 1 do for i:=1 to NumberOfData-gap do if Data[i]>Data[i+gap] then


file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (9 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

swap Data[i] with Data[i+gap];


It is quite similar to bubble sort, right ? Actually, it IS quite speedy and powerful. Let's track it down ! Suppose we have 8 data, then gap is initially 4 (8 div 2 is equal to 4, right ?). Variable i moves from 1 to 4. It start comparing the first with the fifth, the second with the sixth, the third with the seventh, and the fourth with the eighth. As usual, if there are any mismatches in order, it swaps around. Then the gap reduces from 4 to 3. Again, i moves from 1 to 5, comparing it. And so on. (You may skip this if you want to)

Let's look at our first scrambled data in our bubble sort example : At first, gap is 4. 5 3 8 4 1 7 6 2 1st comparison : ^ ^ (mismatch order, swap) 1 3 8 4 5 7 6 2 2nd comparison : ^ ^ (good order, no swap) 3rd comparison : ^ ^ (mismatch order, swap) 1 3 6 4 5 7 8 2 4th comparison : ^ ^ (mismatch order, swap) 1 3 6 2 5 7 8 4 ----> Then the gap is 3. 5th comparison : ^ ^ (good order, no swap) 6th comparison : ^ ^ (good order, no swap) 7th comparison : ^ ^ (good order, no swap) 8th comparison : ^ ^ (good order, no swap)
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (10 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

9th comparison : swap) is 2. 10th comparison : swap) 11th comparison : swap) 12th comparison : swap) 13th comparison : swap) 14th comparison : swap) 15th comparison : swap) is 1. 16th comparison : swap) 17th comparison : swap) 18th comparison : swap) 19th comparison : swap) 20th comparison : swap) 21th comparison : swap) 22th comparison : swap)

(mismatch order, ----> Then the gap (good order, no

1 3 6 2 4 7 8 5 ^ ^ ^ ^

(mismatch order,

1 2 6 3 4 7 8 5 ^ ^ 1 2 4 3 6 7 8 5 ^ ^ ^ ^ ^ ^

(mismatch order,

(good order, no (good order, no (mismatch order, ----> Then the gap (good order, no (good order, no (mismatch order,

1 2 4 3 6 5 8 7 ^ ^ ^ ^ ^ ^ 1 2 3 4 6 5 8 7 ^ ^ ^ ^ 1 2 3 4 5 6 8 7 ^ ^ ^ ^

(good order, no (mismatch order,

(good order, no (mismatch order,

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (11 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

1 2 3 4 5 6 7 8
Finished and completely sorted. Easy, no ? Much faster than bubble sort. Let's look at the fasted sort method in the world in general data : quick sort. It is invented by E. Hoare. It uses recursive method exhaustively. The idea is simple actually. Divide the data into two equal parts. Let the middle data be the pivot point. Gather all data below the pivot into the left side of the pivot, and the greater on the right. Let's see how is the partings. The equal parts is now unequal in size, since there are possibilities that one side is bigger than others. Each part is treated the same way, sorted with the same way. But now, the left side is choosing its new pivot and have two parts again. It is also done to the right side. Enough about lengthy theory. I'll bet you are yawning now. Let's look at the inside : Suppose Data is a global array of byte.

procedure qsort(lower, upper : byte) var left, right, pivot : byte; begin pivot:=Data[(lower+upper) div 2]; left:=lower; right:=upper; while left<=right do begin while Data[left] < pivot do left:=left+1; { Parting for left } while Data[right] > pivot do right:=right-1; { Parting for right} if left<=right then { Validate the change } begin swap Data[left] with Data[right]; left:=left+1; right:=right-1;
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (12 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

end; end; if right>lower then qsort(lower,right); { Sort the LEFT part } if upper>left then qsort(left ,upper); { Sort the RIGHT part } end;
Usage : qsort(1,NumberOfData); (This is quite technical, you may skip this without losing the details of the lesson. You may read this through to know how qsort works.)

At first we assume that the center point of data contains the correct order in the sequence. Thus, we make this as a pivot point. At the excerpt above, we have two pointers, named left and right, pointing the first data and the last one. Then we seek at the left hand side first for mismatch data. In order to swap the data around, we must seek its pair on the right hand side first. It is NOT the pivot we change, but the mismatches data so that they place the correct parting. You may wonder whether it is safe to do that assumption. Yes, IT IS. Why ? It is because the search for mismatch data is not limited from edge to the pivot, but it may overlap the center. Thus, the parting is not equal any longer. The parting is decided when the left marker and the right marker crosses. It means that the comparison is done throughout the data. When the marker crosses, the left marker is at the right hand side of the right marker so that RIGHT parting made of left marker and the upper bound of the data. The LEFT parting is made from lower bound to right marker. Then each parting is sorted again, using recursive method. The two ifs before end; makes sure that the lower limit of the parameter is always less than the upper limit. This also as a quitting conditions to avoid endless repetitive recursive call. Let's look at our first scrambled data :
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (13 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

5 3 8 4 1 7 6 2 Current position : | | | left : at first data (5)---+ | | right : at last data (2)---------+-------+ pivot : at fourth data(4)---------+ Remember that (1+8) div 2 = 4 Do the left parting : 5 > 4 --> mismatch the order, left marker stays Do the right parting : 2 < 4 --> mismatch the order, right marker stays Since left marker and right marker haven't crossed yet, it is legal to swap both data, so it became : 2 3 8 4 1 7 6 5 Left marker moves to the second data (3), right marker to the seventh (6). Since left marker and right marker haven't crossed yet, the main loop goes. Do the left parting : 3 < 4 --> legal, continue. 8 >= 4 --> mismatch, left marker stays. Do the 6 > 4 7 > 4 1 <= 4 right parting : --> legal, continue. --> legal, continue. --> mismatch, right marker stays.

Since left marker and right marker haven't crossed yet, it is legal to swap
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (14 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

both data, so it became : 2 3 1 4 8 7 6 5 Left marker moves to the fourth data (4), right marker moves to the fourth data, too. It hasn't crossed yet, so it continues. Left marker stops the left parting because 4 is not less than 4, so does the right marker. It swaps itself, so nothing is changed. Then left marker advances to the fifth data, right marker to third data. Left parting is made by lower bound and right marker (data no 1 to 3) and right one is made by left marker and upper bound (data no 5 to 8) This goes to recursive process. Let's look at the left parting first. 2 3 1 ... left marker = 2 right marker = 1 pivot = 3 [2]=3 } Do the left parting : 2 < 3 --> legal, continue 3 >= 3 --> mismatch, left marker stops Do the right parting : 1 <= 3 --> mismatch, right marker stops Because left and right marker haven't crossed, it is legal to swap data. So, it becomes : 2 1 3 ... (pivot is still=3)

{ (1+3) div 2 = 2, Data

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (15 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

Left marker advances to 3, and right marker to 1. It crossed. The left parting would be from data no 1 to 2 and the right parting would consist only 3. We look into the left parting first: 2 1 ... left marker = 2 right marker = 1 pivot = 2 { (1+2) div 2 = 1, Data [1] = 2 } Do the left parting : 2 >= 2 --> mismatch, left marker stays Do the right parting : 1 <= 2 --> mismatch, right marker stays. Swap the data so we have 1 2 ..., now we advance both left and right marker. It crossed. We have just 1 as the left parting and 2 as the right parting. So, we have nothing to sort. We then exit the recursive calls and revert to: 1 2 3 ..., sorted form of the original left parting. Then we look into the original right parting: ... 8 7 6 5 left marker = 8 (position 5) right marker = 5 (position 8) pivot = 7 { (5+8) div 2 = 6, Data
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (16 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

[6] = 7 } Do the left parting: 8 >= 7 --> mismatch, left marker stops Do the right parting: 5 <= 7 --> mismatch, right marker stops Data then swapped: ... 5 7 6 8 Then the left marker points at 7, the right marker points at 6 because we advance the pointer. The pivot is still 7. Do the left parting: 7 >= 7 --> mismatch, left marker stops Do the right parting: 6 <= 7 --> mismatch, right marker stops. Then we swap the data: ... 5 6 7 8 And advance the pointers so that left marker points to 7 (again) and right marker points at 6 (again) :-) Pivot is still 7. The pointers are crossed, so we have another left parting from 5 to 6 and the right parting from 7 to 8. However, the data is already sorted, so the parting is essentially does nothing but to return from recursive calls. All data sorted after quitting the recursive calls : 1 2 3 4 5 6 7 8
file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (17 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 9

Phew, quite a lengthy explanation, huh ? Looks like you need a break by now. OK, see you next time !

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 8 about processing strings To Chapter 10 about complex data structure

file:///F|/Documents/Pascal_Doc/pas/pasl1008.html (18 of 18)8/3/2005 7:43:57

Pascal Tutorial - Chapter 10

Complex ? Not so...


Hi ! We meet again ! Ready to step to this chapter ? This time, we're going to discuss complex data type. One of it has been discussed, record. As I promised in chapter 7, I would certainly explain thoroughly all the weirdos. It all begins with type. It is not necessarily before var section, but if you want to use it as a new variable type, you MUST place it before the var section. Let's analogue this to the record we have discussed. Record simply declared in type section and then we can freely use it as if it is a new variable type. You CAN define new variable types. Look at these examples :

type car = (Honda, Toyota, Mercedes, Volvo, Ford); chesspiece=(King, Queen, Rook, Bishop, Knight, Pawn); str20 byteptr = string[20]; = ^byte; { This kind of type will be

discussed on lesson 2 } var name mycar piece look

: : : :

str20; car; array [1..32] of chesspiece; byteptr;

Let's examine str20=string[20]; Variable name is a str20 type. It means that name is a string variable of 20 characters. We knew that string can be limited in length,

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (1 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

but this type can't. You can use name as if it is a normal string variable. Look at car and chesspiece types. Is it true ? Yes, it IS. Actually, they are treated as integers. These kind of variables are called enumerated types. You can not assign integers to mycar and to piece. You assign their rightful contents instead. Example :

begin mycar:=1; mycar:=Honda; if mycar=Volvo then begin clrscr; writeln('I end; piece[1]:=Pawn; end.

{ Illegal } { Legal } { Legal } have a volvo'); { Legal }

Is it true that compiler treat it as an integer ? Yes, it is. Therefore, Pascal provides these commands : Ord, Pred, Succ. Ord is used to know their integer contents. So :

Ord(Honda) = 0, Ord(Toyota) = 1, Ord(Mercedes) = 2, Ord (Volvo) = 3, Ord(Ford) = 4. Ord(King) = 0, Ord(Queen) = 1, Ord(Rook) = 2, Ord (Bishop) = 3, and so on Ord can also be used to get the ASCII number of a character, example :

n:=Ord('A');

{ n = 65 }

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (2 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

Pred is used to decrement the enumerated types. It is similar to Dec.

n:=Pred(Ford); x - 1 } n:=Pred(Bishop); x:=Toyota; n:=Pred(x);

{ n = Volvo } { n = Rook }

Dec(x);

{ x :=

{ n = Honda }

Succ is the complement : incrementing enumerated types. Similar to Inc.

n:=Succ(Mercedes); { n = Volvo } x + 1 } x:=Rook; n:=Succ(x); { n = Bishop }


Easy, right ?? Now, let's look at this special feature :

Inc(x);

{ x :=

type car = (Honda=1, Toyota, Fiat, Ford=5, Volvo=7, Mercedes);

And look at this :

Ord(Honda) Ord(Toyota) Ord(Fiat) Ord(Ford) Ord(Volvo)

= = = = =

1 2 3 5 7

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (3 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

Ord(Mercedes) = 8
Got what I mean ? You can do that instead of creating constants for similar behavior. This really makes codes more readable. Yup, this is a short chapter. So, I decided to give extras :

Random number generator !


It is a very theoritic, but I'll make it short and fun. Stripped down any unnecessary theory. Actually, computer is a deterministic machine. What does that mean ? It means that it could not do everything itself. Instead, it only does what the program wants. So, there is NO random generator. People simulate it. Therefore comes a jargon pseudorandom number. Wait a minute ... how could people simulate it ? Well, look at the following idea : 1. Von Neumann idea about squaring numbers and have some digits of them. It is obsolete and has many weakness. So, don't bother to discuss it. 2. Lehmer idea about linear function. This is brilliant, but it has weakness. If we don't handle it carefully, it may fall into disrandomness. OK, look at Lehmer's idea. His idea can be formulated as this : xn+1 = ( a xn + c ) mod m Where a, c, and m are predefined constants; n is the index. A pretty easy and straight-forward function to implement. It has several weakness. Three of them are : 1. Need a number to start with. 2. If we don't choose a, c, and m carefully, that function may fall into disrandomness. 3. The coming number may be negative, as the formula neglects negative numbers.

ad. 1.

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (4 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

The number to start with is called the seed (x0). If we choose the same seed, it generates the same sequence of numbers. Suppose a, c, and m is 9, 5, and 8 consequently. If we always take the seed (x0) 0, what we all get is the same sequence of : 5, 2, 7, 4, 7, ... The solution is that we must take different seed everytime. How ? Ask user to define the seed ? No way ! That's why we must start every random generator with randomize. Randomize simply select a seed. The seed is taken from timer ticks, which is always incremented each time. No one ever knows how much is it. The only thing for sure is that it always change. Now try this program : (Program 1)

uses crt; var i : byte; begin clrscr; randomize; for i:=1 to 20 do write(random(100):8); readkey; end.
Run it for several times and inspect the sequence of numbers displayed on screen carefully. It always change everytime you run it, right ? Now, remove the randomize. Run it for several times and inspect the sequence of numbers. You see that without randomize, every time you run the program, you will find the same sequence. This is the weakness. Got what I mean ?

ad. 2.
Well, random function in Borland Pascal, or many other programs seem to have chosen a, c, and m quite good so that the generated numbers seemed random. Well, the implementation in the program is : (Program 2)

var
file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (5 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

seed : longint; procedure myrandomize; begin seed:=meml[0040:006c]; end;

{ This how to get timer ticks }

function myrandom (howbig:word) : word; begin seed:=(seed*a+c) mod m; myrandom:=seed mod howbig; end;
Add the myrandomize and myrandom routines into program 1. Then, replace all random and randomize with myrandom and myrandomize. Think about nice number for a, c, and m. Then run it for several times. See the problem ? OK, the common problems are : 1. Non random sequence, like this : 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, ... 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, ... 2. We've got a repeating short sequence, like this : 3, 4, 7, 3, 4, 7, 3, 4, 7, ... 4, 5, 4, 5, 4, 5, ... 3, 9, 2, 6, 7, 3, 9, 2, 6, 7, ... No ! It's not a random generator ! It's a sequence generator ! 3. Good sequence, but ended with the same short sequence, or even the same number, like this : 10, 11, 11, 11, 11, 11 .... 5, 2, 7, 4, 7, 4, 7, 4, 7, 4, ... Well, needless to say, it's definitely not a random generator. 4. Incrementing sequence. It's known as Lattice problem, like this : 3, 4, 7, 6, 7, 10, 9, 10, 13, 12, 13, 16, ... See ? It may seem random, but let's look at this arrangement : 3, 4, 7, 6, 7, 10,

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (6 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

9, 10, 13, 12, 13, 16, ... Got what I mean ? It is the same sequence, but just adding all of them with three, then start over again. Again : It's not a random number ! The number of sequence before it started over again is called the period.
Sequence Period

3, 4, 7, 3, 4, 7, 3, 4, 7, ... 10, 11, 11, 11, 11, ... 3, 9, 2, 6, 7, 3, 9, 2, 6, 7, ...

3 1 5

To get a good random sequence is that it seems that the nevers reoccurs, it is simply generate new, erratic number. In order to make that 'illusion', we must generate a sequence with a very very long period. The period is closely related to m. The period never exceed m, so that we must give m a very big value, say 2,000,000,000. A good random number generator has a random sequence with period of m. Now, take a time, choose your a, c, and m. Here's a hint : let m be 2,147,483,647 let c be 1 let a be 16807 This was from S.K. Park and L.W. Miller research in 1988. It's quite good. Try it ! We've still get 2 problems more : 1. Multiplication overflow. 2. Signed bit set. 1. Multiplication overflow. We could handle it by using mathematical method. Most of the times you may ignore the overflow. It will not have much effect on your numbers. So, you may skip this if you want to. Yes, yes, I understand your reason why we should care about this. But it seldom happens. The idea is from multiplicative attitude :

2. 3.
file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (7 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

4.

x * (a+b) = x * a + x * b

Suppose n is a+b. You should divide n into 2 numbers right ? If you have a divisor (any number), called y, n could be easily divided into a and b which are :

a = (n div y) * y b = n mod y
Then we replace it with a and b :

x * n = x * y * (n div y) + x * (n mod y)
It's pretty tricky. You must choose y so that x * y * (n div y) will not overflow. So, what's the point ? We see that seed*a may cause overflow. Hence, you must replace it into :

seed * a = a * y * (seed div y) + a * (seed mod y)


Yes, but you shall never implement this directly. We know that it is still cause an overflow. How can it be done ? We know that a * (seed mod y) will never cause overflow if a * y is always ways less than maximum limit of the long integer. The maximum limit of long integer is 2,147,483,647, we call it MAX_LONGINT. To check whether the equation causes overflow, we do this :

temp = MAX_LONGINT - a * (seed mod y)


If temp is less than a * y * (seed div y) means that it WILL cause overflowing. So, you may wonder how it can be overcome. Suppose :

z = a * (seed div y)
Then, you may easily guess :
file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (8 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

(z - temp / y) * y
Will not cause overflow. Yup, wait a minute Sherlock ! temp / y is a rational number, the compiler will complain ! How can we get rid of it ? We change the equation above to :

(z - temp div y) * y - temp mod y


Phew ! Pretty lengthy, huh ? So, how is the final equation like ?

seed * a = (z - temp div y) * y - (temp mod y) + a * (seed mod y)


OK ! OK ! You are bored, so that how could we implement this into program ? Watch this :

function random (howbig : word) : word; const a = 16807; c = 1; m = 2147483647; y = m div a; { suppose to be 127773 } MAX_LONGINT = 2147483647; { same as m } var z, temp : longint; begin z := a * (seed div y); temp := MAX_LONGINT - a * (seed mod y); { seed := seed * a + c } seed := (z - temp div y) * y - (temp mod y) + a *
file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (9 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

(seed mod y) + c; { set loopback for seed } if seed < 0 then seed := seed + MAX_LONGINT; seed := seed mod m; myrandom := seed mod howbig; end;
That's it ! You may wonder what if for is. Well, it does tackle the second problem. Here is the explanation. You may just rip that code and never know what the inside. 5. Signed bit set. In our talk just now, we assume that a * y * (seed div y) is always causing overflow. If it doesn't, means that :

6. 7. 8.

(z - temp div y) * y - (temp mod y)

will always be negative. We don't want negative seed here since it will be catastrophic. Why is it called SIGNED BIT SET ? It is because to differ a number from negative to positive, computer only use sign bit. If it is a negative, the sign bit set (on), when it is positive, it is cleared (off). So, how to cure this ? Easy, just add it with MAX_LONGINT, and we should have got rid of it. This will certainly clear off the sign bit. This Lehmer's function is adequate for daily purposes. For scientific ones, it may not be enough. Therefore, there is another method called Uniform Random Deviates, invented by Paul L'Ecuyer. We would not discuss it now, since it is quite complicated. But you know that the Lehmer's above will have a period of 2,147,483,647. Uniform Random Deviates will create a period of about 108 !! Quite a slam bang ! Even this period is not enough for scientific uses ! However, combining both will result a period about 2,3 x 1018 ! It should be adequate for just any purposes. How can we implement random numbers to generate :

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (10 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

1. Fraction from 0 to 1 ? Suppose you have a function of srand, that generates such fraction :

function srand:real; { everything is just the same as myrandom function except myrandom := seed mod howbig; is changed into : } myrandom:= seed / MAX_LONGINT;

2. Integers from a to b ? Suppose you have a function of rand, that generates such integer :

function rand(lower, upper : word): word; { everything is just the same as myrandom function except myrandom := seed mod howbig; is changed into : } myrandom:= lower + (seed mod (upper - lower + 1));

Actually, there are a lot more to discuss : Random table, Random number with no duplication, scrambling arrays, etc. But I think you had enough for this time. So, adios amigos, my friend ! See ya on the quiz !

Where to go ?
Back to main page
file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (11 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 10

Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 9 about records To Chapter 11 about sets

file:///F|/Documents/Pascal_Doc/pas/pasl1009.html (12 of 12)8/3/2005 7:44:02

Pascal Tutorial - Chapter 11

It's All Set


Hi ! Nice to meet you again !This time, we'll discuss about : 1. Sets declaration 2. Using sets 3. Sets of constants Set's actually a special feature of Pascal that cannot be found in other programming languages. It merely took for granted most of the times, though it is very good to employ it in our program. Beside it helps much the readability, it ease our job in thinking about sets. It is intended to extend the array, but programmers seem to ignore it, prefer using array instead of sets. Well, personally I also seldom use sets. I have a little knowledge of this, but I thought it was sufficient enough for practical use. Many books ignore this special feature, but I'll try to cover it as best and as clearly as possible. What is sets exactly ? Sets in programming is quite the same as in mathematics. Sets in mathematics is like this :

A = { 1, 2, 3, 4, 5 }

So does sets. OK, how can we declare sets ?

type days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);

file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (1 of 7)8/3/2005 7:44:05

Pascal Tutorial - Chapter 11

var allday : set of days;


You cannot write its contents or read to add it. Well, how can we use it ? Run this program :

type days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); var allday : set of days; workday : set of Monday .. Friday; thisday : days; begin thisday:=Monday; if thisday in workday then writeln('This day I''m in work') else writeln('This day I''m on holiday'); end.
Now, change thisday:=Monday into thisday:=Saturday, and run it again. See the difference ? Look at the word in in if thisday in .... You know its function now ? Suppose I have the variable of myday, it is defined as set of days, like this :

var myday : set of days;

file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (2 of 7)8/3/2005 7:44:05

Pascal Tutorial - Chapter 11

Naturally, myday contents is Sunday, Monday, ... through Saturday. Suppose Wednesday is my bad day, so that I don't want it included inside myday. You do this :

exclude(myday,Wednesday);

Then again, excluding Friday :

exclude(myday,Friday);

After that, you change your mind, including Friday again :

include(myday,Friday);

Look at this program :

type days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); var allday myday workday thisday begin
file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (3 of 7)8/3/2005 7:44:05

: : : :

set of days; set of days; set of Monday .. Friday; days;

Pascal Tutorial - Chapter 11

exclude(myday,Wednesday); exclude(myday,Friday); thisday:=Friday; if thisday in myday then writeln('Friday is just my day !') else writeln('Friday is not my day !'); include(myday,Friday); { Changing your mind } if thisday in myday then writeln('I change my mind : Friday is just my day !') else writeln('Friday is still not my day !'); end.
Yes, you're brilliant ! You may guess the output be like this :

Friday is not my day ! I change my mind : Friday is just my day !

Got the idea ? Good ! The word in is not just comparing one variable to a set but comparing sets, too. Look at this :

if workday in allday then elements in workday } : :

{ This is true since all { exist in allday. }

file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (4 of 7)8/3/2005 7:44:05

Pascal Tutorial - Chapter 11

if allday in workday then there are elements } : exist in workday, } : Saturday. }

{ This is false, because { in allday which are not { they are Sunday and

Look at workday definition :

var workday : set of Monday..Friday; begin include(workday,Saturday); { This is illegal, since it is out of range } end.
You cannot include elements that is out of range. But you may include elements more than once even you have already got it inside the set, like this :

include(allday,Monday); include(allday,Monday);

{ Legal } { Legal too }

You might not need the sets since it can be formed inside our program. Look at these legal statements :

thisday:=Tuesday; if thisday in [Monday..Friday] then


file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (5 of 7)8/3/2005 7:44:05

{ True }

Pascal Tutorial - Chapter 11

: :

or like this :

thisday:=Tuesday; if thisday in [Monday,Thursday..Saturday] : :

{ False }

This is the practical use :

uses crt; var c : char; begin write('Yes or no (Y/N) ?'); repeat c:=upcase(readkey); until c in ['Y','N']; end.
Run that program then figure it out ! Then, this program :

uses crt; type allletter = set of 'A'..'Z'; var


file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (6 of 7)8/3/2005 7:44:05

Pascal Tutorial - Chapter 11

c : char; vowel : set of 'A','E','I','O','U'; letter : set of allletter; begin write('Enter a vowel'); repeat c:=upcase(readkey); until c in vowel; write('Enter a letter'); repeat c:=upcase(readkey); until c in letter; end.

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents Back to Chapter 10 about complex data types To Chapter 12 about making custom units

file:///F|/Documents/Pascal_Doc/pas/pasl1010.html (7 of 7)8/3/2005 7:44:05

Pascal Tutorial - Chapter 12

Unit 1 Ready !
Hi ! Nice to meet you ! This time we will discuss about units. What is units anyway ? It's like having a library of commands, like crt unit, that we often use so long. Now, we're gonna make one. What's so different with units ? Well, units have structures like this :

{This code must be saved with name WHAT.PAS} unit what; interface uses .... var ... { Visible global variables } { Procedures & functions definitions } procedure myproc; function myfunc: word; : : implementation var ... { Invisible global variables } procedure myproc; begin : { the routines } : end;
file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (1 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

function myfunc:word; begin : { the routines } : end; : : :

{ other procedure's / function's routines }

begin : { Initialization code } : end.


That was the structure. The interface part contains definitions of procedures or functions and the variables. All definitions ( procedures, functions, and variables ) in this part are visible to the user of your unit. It means that the user who put your unit in their uses clause can invoke the declared procedure/function and can easily change the variables. The implementation part contains the contents of all procedures and variables which are either visible or not. Variable declaration in this part is NOT visible. Means that user cannot modify them. Only the procedure or function inside that unit can. Attention : Unit name must comply to the filename. Look at this example :

{ This unit must be stored in MYUNIT.PAS } unit myunit; interface uses crt;

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (2 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

var x : byte; y,z : integer; procedure a; procedure b; function c:byte; implementation var p,q,r : shortint; procedure a; begin : : end; procedure d; begin : : end; procedure b; begin : : end; function c: byte; begin : : end;

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (3 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

procedure e; begin : : end; begin : { Initialization code } : end.


Suppose I make a program using the unit above :

uses myunit; var n : byte; begin a; { b; { n:=c; { x:=1; { y:=-1; { z:=14; { d; e; p:=-1; end.

legal legal legal legal legal legal

} } } } } }

{ illegal, because it is invisible } { illegal, because it is invisible } { illegal, because it is invisible }

I can call procedure a, b, and function c, within my program, but not procedure d, and e. Access to variable x, y, and z is legal, but not to p, q, and r.

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (4 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

Procedure d and e can only be invoked INSIDE that unit, for example procedure b call procedure d. The invokability rules are just the same as normal program, i.e. d can call a, but a cannot call d. Easy, right ? Now, what's all this for ? If you have made procedures or functions and you think that you will use them a lot in the future, you may want this placed in a unit so that you can re-use them. Mind this : This is a VERY important feature and you shall heed it. Start thinking of procedures or functions that you often use, then place it in a unit. Think seriously, because I will ask you that in the quiz. Look at this unit (MYUNIT.PAS). It is a simple unit, but works !

unit myunit; interface uses crt; procedure procedure procedure function clearbkgr(color : byte); makewindow(x1, y1, x2, y2, color : byte); writexy(x, y : byte ; mess : string); ucase(st:string):string;

implementation procedure clearbkgr(color : byte); begin textbackground(color); clrscr; end; { Invisible to unit user, this is internal function } function fill(s : string; count : byte; what: char): string; begin
file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (5 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

fillchar(s,count+1,what); s[0]:=chr(count); fill:=s; end; procedure makewindow(x1, y1, x2, y2, color : byte); var s : string; i : byte; begin textattr:=color; s:=fill(s,x2-x1-1,#205); gotoxy(x1,y1); write(#201+s+#187); gotoxy(x1,y2); write(#200+s+#188); s:=fill(s,x2-x1-1,' '); s:=#186+s+#186; for i:=1 to y2-y1-1 do begin gotoxy(x1,y1+i); write(s); end; end; procedure writexy(x, y : byte; mess : string); begin gotoxy(x,y); write(mess); end; function ucase(st:string):string; var i : byte; begin for i:=1 to length(st) do st[i]:=upcase(st[i]); ucase:=st; end; begin
file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (6 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

{ Leave this empty if you don't have any initialization code } end.
What is the initialization codes ? If you think that you need to do routines before the unit is ready to use, you may do it in the main begin...end block. It's similar to preparation code, that is to prepare your unit. For example : You feel that some variables need to be set, do it in that section. If you need some setup code need to be established, do that too in the same way. Initialization codes is done before the begin...end block of the main program that use your unit. Suppose the program is like this :

: : uses unit_a, unit_b, unit_c; : : : begin : : end.


Initialization code in unit_a is done first, then unit_b, then unit_c, then the main begin...end. This is quite a short lesson, and this time EXTRA ! Converting numbers into bits, vice versa. Easy ! Divide the number by 2 :

53 2 ------ 1

(53 mod 2 = 1)

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (7 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

2 2 2 2

26 -----13 -----6 -----3 -----1

0 1 0 1

(53 (26 (26 (13 (13 ( 6 ( 6 ( 3 ( 3

div mod div mod div mod div mod div

2 2 2 2 2 2 2 2 2

= = = = = = = = =

26) 0) 13) 1) 6) 0) 1) 1) 1)

Stop here, since 1 is less than 2. So the binary form of 53 is 110101. Look at this example :

2 2 2 2 2 2

100 -----50 -----25 -----12 -----6 -----3 -----1

/|\ 41 0 | 2 ---- 1 2 | 20 0 | 2 ---- 0 2 | 10 1 | 2 ---- 0 2 | 5 0 | 2 ---- 1 2 | 2 0 | 2 ---- 0 2 | 1 1 | | Direction to read

37 ---18 ---9 ---4 ---2 ---1

1 0 1 0 0

So, 100 is 1100100 in binary, 41 is 101001, and 37 is 100101. Easy, no ? Now, how to convert binary digits (bits) into decimals ? Suppose we have :
G G G

1010010 = 1 x 26 + 1 x 24 + 1 x 21 = 64+16+2 = 82 0100001 = 1 x 25 + 1 x 20 = 32+1 = 33 0111100 = 1 x 25 + 1 x 24 + 1 x 23 + 1 x 22 = 32+16+8+4 = 60

Shorter one :

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (8 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12


G G

1001 = 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20 = 8+0+0+1 = 9 1011 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 8+0+2+1 = 11

Easy, right ? If you want to convert numbers to hexadecimal, it is just similar, but change the divisor 2 into 16, like this :

100 37 47 16 ----- 4 16 ---- 15 6 2 2

41 16 ---- 9 2

53 16 ------ 5 3 16 ---- 5

Means that 100 = 64 hex, 41 = 29 hex, 53 = 35 hex, 37 = 25 hex, 47 = 2F hex Any number exceed 9 is decoded like this :

10 into 11 into 12 into ------ 10 13 into 14 into 3A hex 15 into

A B C D E F

61 16 ------ 13 3 61 = 3D hex

44 16 ------ 12 2 44 = 2C hex

58 16 3 58 =

Converting back to decimal is the same :


G G G

3D Hex = 3 x 161 + 13 x 160 = 48 + 13 = 61 67 Hex = 6 x 161 + 7 x 160 = 96 + 7 = 103 103 Hex = 1 x 162 + 0 x 161 + 3 x 160 = 256 + 0 + 3 = 259

And so on.

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (9 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 12

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 11 about sets To Chapter 13 about text files

file:///F|/Documents/Pascal_Doc/pas/pasl1011.html (10 of 10)8/3/2005 7:44:09

Pascal Tutorial - Chapter 13

Read the Text Files

Hello, we meet again. I'm hope you're not bored yet. Now, we will discuss all things related to text files, like : 1. 2. 3. 4. 5. Reading text files Writing text files Appending text files Additional commands : flush Error handling

What is text files actually ? It's just like this file, the read-me files, AUTOEXEC.BAT and CONFIG.SYS. All files with extension .INI are text files. This time we will discuss how to write or read one. Look at this :

var F : text;
F is a variable of text file. The first thing we must do is associate it with a file name, for example :

assign(F,'README');
Before you can READ it, you must open it :

reset(F);
You can read the file line by line, just like inputting user. Suppose s is a string variable :

file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (1 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 13

readln(F, s);
Note that the difference between normal readln with this kind of readln is the file variable F. To know if user has reached end-of-file, function EOF will return true if the file has reached the end-of-file (EOF) marker :

if EOF(F) then writeln('This is the end of text file');


After using that file, DON'T FORGET TO CLOSE IT. How ?

close(F);
So, to read out all text file is just like this :

uses crt; var F : text; s : string; begin clrscr; write('Input file name to read : '); readln(s); assign(F,s); { associate it } reset(F); { open it } while not EOF(F) do { read it until it's done } begin readln(F,s); writeln(s); end; close(F); { close it } end.

file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (2 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 13

Easy and simple, right ? How to create one ? First thing you make is the same : associate the text variable with the filename using assign keyword. You then create it using rewrite instead of reset :

rewrite(F);
Then, use your logic : To write lines into it use writeln instead of readln :

writeln(F,s)

{ s is a string variable }

And after writing all inside, don't forget to close it with the same close. So, writing a text file is like this :

uses crt; var F : text; s : string; begin clrscr; write('Input file name to create : '); readln(s); assign(F,s); { associate it } rewrite(F); { create it } writeln('Just enter any text and followed by enter.'); writeln('To quit : Input an empty line.'); repeat readln(s); if s='' then break;

{ write it until done }

file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (3 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 13

writeln(F,s); until true; close(F); end. { close it }

Caution : If you do rewrite an existing file, it means that you delete it and create a new one. How can we just add some text without destroying it ? Well, use append instead. Change the rewrite in program above into append. Run them and see the difference. Note : Before you run them, create a two identical text file for test. You could duplicate this file, but : Don't forget to make a backup for the original. Pascal uses an internal buffer to store text-information. Buffer is a temporary place, usually an array or a pointer of a memory location. Pointer will be discussed in lesson 2. Buffer is employed to speed up (to cache) the process of reading or writing for any file, including text file. Buffer is limited in amount, but it is sufficient enough for processes in Pascal. If the buffer is full, Pascal flushed its contents to disk. Now, sometimes you need to make sure that the buffer is not exceeding its maximum size. But, wait ... ! You said that it is sufficient enough ? Yes, it is. But flushing buffer manually can save the day. Say, the power line is suddenly down and the buffer contents haven't been written to disk. It IS tragic, isn't it ? You don't want your program user say 'AAAAaaaarrgggh, my data is LOST !', do you ? Flushing buffer manually is done by adding flush(f); somewhere in your program before you close your file. Manual flush like this can only be done in text files. How about binary files ? Later, in the next chapter. Now, we found that writing or reading files may not be as smooth as we have thought. Sometimes, in reading file, the file meant is not found, or having bad sector so that we cannot read it. If we don't employ the error-handling routine provided in Pascal, Pascal just shout 'Hey ! This is an error' then quits the program without notification. This type of error-handling is pretty out-dated style since new error-trapping hadn't been
file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (4 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 13

invented in that time. But, I think it is sufficient for now, but it is a tiring task to do so. But, think about the user. Don't let the pain come. Here is the error-handling. Add this :

{$I-} : {$I+} any

--> to make Pascal calm down when error shows up --> Do file process --> to make Pascal detect what error is it if

The error can be detected by invoking the function IOResult. If IOresult is 0, it means no error. For example :

uses crt; var F : text; s : string; begin clrscr; write('Input file name to read : '); readln(s); assign(F,s); { associate it } {$I-} reset(F); { open it } {$I+} if IOresult<>0 then begin writeln('Error encountered in reading file ',s); halt; end; while not EOF(F) do begin readln(F,s);
file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (5 of 8)8/3/2005 7:44:12

{ read it until it's done }

Pascal Tutorial - Chapter 13

writeln(s); end; close(F); end.

{ close it }

Yes, that's easy. Invoking IOresult causes the error code to reset. You may want to save its contents first to detect what error is it. Suppose n is an integer variable. I just modify a part of above program :

: : {$I-} reset(F); {$I+} n:=IOResult; if n<>0 then begin writeln('Error encountered in reading file ',s); case n of 2: writeln('File not found'); 3: writeln('Path not found'); 4: writeln('Too many open files'); 5: writeln('File access denied'); 100: writeln('Disk read error'); 101: writeln('Disk write error'); 150: writeln('Disk is write protected'); 152: writeln('Drive is not ready'); 154: writeln('CRC error in data'); 156: writeln('Disk seek error'); 157: writeln('Unknown media type'); 162: writeln('Hardware failure'); else writeln('Various error'); end; halt;

file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (6 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 13

end; : :
You see that you can detect it. In order to make your program free of error in run time, you must do this to EVERY input/output command, that means to every readln, reset, rewrite, writeln, append, and flush. Only assign and close do not need this. Phew, that must be a very tedious task. Now that you have learnt error handling. Other error message can be seen in help (runtime error). Even this chapter is now finished, I still give you an EXTRA ! This extra is about detecting arguments / parameters passed in our program. Pascal provide two general "variables" that is : paramcount and paramstr. Paramcount is a word variable telling the number of parameters that is pas- sed into our program. Paramstr is an array of string containing the content s of the parameters. Example :

var n : word; begin for n:=1 to paramcount do begin writeln(paramstr[n]); end; end.
How can we test that program ? Should we get out of the IDE and run the EXE file ourself ? No need ! Borland Pascal provides a 'parameter simulation'. Check out the menu : Run then choose Parameters... You then asked the para- meters that will be passed into the program. Write any sentence or words in it then press enter. Run it and see what happens. Yes, it was just like running the EXE program in DOS prompt and passing the parameters manually. Easy, right ?

Where to go ?
Back to main page

file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (7 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 13

Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 12 about making custom units To Chapter 14 about binary files

file:///F|/Documents/Pascal_Doc/pas/pasl1012.html (8 of 8)8/3/2005 7:44:12

Pascal Tutorial - Chapter 14

How About Binary One

Hello, we meet again! Nice to see ya! I know you're eager to finish this last chapter of the first lesson. It's still about the file. It is similar to the previous chapter. If you don't understand chapter 13, you'd better not carry on, but re-learn it instead. This time, we will discuss : 1. 2. 3. 4. Typed files Untyped files File commands Directory commands

There are two kinds of binary files : 1. Typed files 2. Untyped files Typed file means that the file has a uniform format throughout its contents. This kind of file includes databases, because all of them contains the data records. Simply said, file of records. Untyped file means that the file contains no uniform data. Although you may have seen records stored in this kind of file, that file contains some additional information that may be different record structure. Simply said, file with no distinct records. First, we discuss typed files. Suppose you define a record like this :

type Temployee = record name address phone age

: : : :

string[20]; string[40]; string[15]; byte;

file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (1 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

salary end;

: longint;

Typed file of THAT record is defined like this :

var F : file of Temployee;


The steps of using typed file is just the same as using text file. 1. You associate it with file name using assign. 2. Open it, using reset, OR Create it, using rewrite. 3. Use it. Writeln in text file MUST BE CHANGED into Write and Readln with Read respectively. 4. Close it using close. All error handling and IOResult use is all the same, so that I don't have to re-mention it all over again. The difference is : If you open typed file with reset it doesn't mean that you can only read it (just in the text files), but you may write on it and modify it. The command rewrite is still the same, create a new one, discarding the file previous contents. Then, look at this example :

{ A crude database recording } uses crt; type Temployee = record name : string[20]; address : string[40]; phone : string[15]; age : byte; salary : longint;
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (2 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

end; var F c r s : : : : file of Temployee; char; Temployee; string;

begin clrscr; write('Input file name to record databases : '); readln (s); assign(F,s); rewrite(F); repeat clrscr; write('Name data } write('Address write('Phone write('Age write('Salary write(F,r); { Associate it } { Create it }

= '); readln(r.name); = = = = '); '); '); '); readln(r.address); readln(r.phone); readln(r.age); readln(r.salary);

{ Input

{ Write data to file }

write('Input data again (Y/N) ?'); repeat c:=upcase(readkey); {Ask user:Input again

or not } until c in ['Y','N']; writeln(c); until c='N';


file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (3 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

close(F); end.
Easy, right ? After creating database, display it. Modify the above program to read the file contents. This is the hint : 1. Change rewrite to reset. 2. After the second clrscr (inside repeat..until block), add : read(F,r); 3. Remove the line "write(F,r)" That's all. You may alter the displayed message to the appropriate one. Run it and see how it's done. Good ! You've done it ! Now, it's time to understand file pointer. In order to know the current position of the file, Pascal use a file pointer. It's simply points to the next record or byte to read. To move the file pointer, use seek :

seek(F,recordno);
The recordno simply said the record number. If you want to read the tenth record of the file at any instant, use this :

seek(F,9); 0 } read(F,r);

{ Data record number started from { r is the record variable }

You may conclude that it is easy to access the records. Say the record number, seek it, and read it. Any record number in range could be accessed. In range means not exceeding the maximum number of record inside that file. Therefore, it is called Random File Access. In the other hand, text files could not behave like that. So that it requires to be handled sequentially. Therefore, there comes the jargon Sequential File Access.

file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (4 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

Append DOES NOT work in typed files or untyped files. It is specially designed for text files. Then how can we append data to typed files ? Easy. Follow these steps : 1. Open the file with reset. 2. Move the file pointer after the last record using seek.

Reset causes file opened but the file pointer points to the first record. How can we
know the number of records that is stored inside a file ? Number of records can be calculated as follows :

totalrecord := filesize(f);
Here is an example of a 'crude' database. It creates a new database if it is not exist, otherwise it appends data.

{ A crude database recording } uses crt; type Temployee = record name : string[20]; address : string[40]; phone : string[15]; age : byte; salary : longint; end; var F : file of Temployee; c : char; r : Temployee; s : string; n : integer; begin clrscr;
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (5 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

write('Input file name to record databases : '); readln (s); assign(F,s); {$I-} reset(F); {$I+} { Associate it } { First, open it }

n:=IOResult; if n<>0 then { If it's doesn't exist then } begin {$I-} rewrite(F); { Create it } {$I+} n:=IOResult; if n<>0 then begin writeln('Error creating file !'); halt; end; end else { If it exists then } seek(F,filesize(F)); { Move file pointer to the last record } repeat : : : { All remains the same } : :
Now, how can we delete a data ? The only routine that Pascal provides is Truncate. It deletes all data starting from where file pointer points to the end of file. You may wonder how to delete a single data record. This is how : Suppose the record number
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (6 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

you want to delete is stored in n.

for i:=n to totalrecord-1 do begin seek(f,i); read(f,r); seek(f,i-1); write(f,r); end; seek(f,totalrecord-1); truncate(f); dec(totalrecord);
Yes, you move the next record to the deleted record. The second next to the next and so on until the end of data. After that, you can safely truncate the last record, since the last record is already stored in record number totalrecord-1 and the last record would be a mere duplicate. Last step you must make is that you must adjust the total record to comply with present situation (after deletion). Easy, right ? Oh, yes ! I forgot to mention : Flush cannot be applied to binary files. It's just for text files. It is unpractical to always having file pointer tracked. You can obtain the file pointer position by using filepos :

n:=filepos(F);
N will hold the current file position (the record number). That's all about typed files. You may want to see this program for better details. Run it and learn how it works.

{ A crude database recording } uses crt;


file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (7 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

type Temployee = record name address phone age salary end; var

: : : : :

string[20]; string[40]; string[15]; byte; longint;

F : file of Temployee; c : char; r : Temployee; s : string; n : integer; begin clrscr; write('Input file name to record databases : '); readln (s); assign(F,s); {$I-} reset(F); {$I+} { Associate it } { First, open it }

n:=IOResult; if n<>0 then { If it's doesn't exist then } begin {$I-} rewrite(F); { Create it } {$I+} n:=IOResult; if n<>0 then begin writeln('Error creating file !'); halt; end;
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (8 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

end else begin { If it exists then } n:=filesize(F); { Calculate total record } seek(F,n); { Move file pointer PAST the last record } end; repeat clrscr; writeln('File position : ',filepos(f)); write('Name = '); readln(r.name); { Input data } write('Address = '); readln(r.address); write('Phone = '); readln(r.phone); write('Age = '); readln(r.age); write('Salary = '); readln(r.salary); write(F,r); { Write data to file } write('Input data again (Y/N) ?'); repeat c:=upcase(readkey); { Ask user : Input again or not } until c in ['Y','N']; writeln(c); until c='N'; close(F); end.
Before you fully understand typed files, DO NOT CONTINUE to untyped one, it will just make you more confused. Text files are usually used for INI files or setting files. Or, if your game needs special setup, you can use this skill to modify AUTOEXEC.BAT, CONFIG.SYS or even *.INI in WINDOWS directory. Typed files are usually done for recording high scores of your game, while untyped ones are for reading your game data : pictures, sounds, etc. Serious applications make an extensive use of file. Databases usually use typed files. Text files are used for making memos. Untyped ones is for reading pictures and sounds,
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (9 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

for perhaps, you want to make presentations or just displaying the company logo.

Untyped Files
Now, we're going to discuss the untyped files. The basics is all the same. Imagine you have a typed file, but the record is one byte long. The declaration is a bit different from typed files :

var F : file;
This declare F as untyped files. Assign and Close are still the same, but Reset and Rewrite are a little bit different. Instead of just passing the F, you have to specify the "record size" for that file like this:

reset(F,1); Rewrite is just similar. What is the "record size" for? It is used to specify the number
of bytes each time you read or write from that file. Let me explain that a bit later. The command write and read cannot be used in untyped file. Use blockwrite and blockread instead. Here is the syntax :

blockread (f,buffer,count,actual); blockwrite(f,buffer,count,actual); f buffer count actual is is is is the file variable. your own buffer, not Pascal's. the number of records you want to read/write. the number of bytes that has been read/written.

The parameter count in both commands will determine how many records (not bytes) you will read or write. Effectively, the total number of bytes (read or written) is count*record_size bytes. This is why you must set the record size in the first place because Pascal doesn't know that.
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (10 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

In untyped files, you must prepare a buffer. A buffer can be records, arrays, or even pointers. Usually, programmers use array instead of records. But, usually, if the file has certain structure, like graphic formats, programmers use records. Let's first concern about array as the buffer. Suppose I declared buffer as array of bytes :

var buffer : array[1..2048] of byte; count, actual : word; f : file;


Since we have an array of bytes for the buffer, we have to set the record size to 1. We can simply consider that each array element counts as one byte. If you have array of integers or words, you may as well set the record size into 2. Reading is done by this :

: reset(f,1); { Setting record size to 1 } : : count:=sizeof(buffer); blockread(f,buffer,count,actual);


This example will read 1*2048 bytes because record size is set to 1 and the size of the buffer (specified in count) variable is 2048. Variable actual holds the number of bytes that is actually read from the disk. Likewise, writing to disk is done through blockwrite :

: rewrite(f,1); : : count:=sizeof(buffer);
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (11 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

blockwrite(f,buffer,count,actual);
You can even specify the number of bytes you want to read. Suppose you want to read just 512 bytes from a file :

blockread(f,buffer,512,actual);
Writing 512 bytes is just similar to reading. Now, how if the buffer is a record ? Suppose I declare the record :

type THeader = record tag : string[4]; width, depth : word; bitperpixel : byte; end; var hdr : THeader;
That kind of header is one example of reading picture file header. Usually, after reset, programmer has to read the header to check validity. Reading the header can be done by blockread :

blockread(f,hdr,sizeof(hdr),actual);
The operator sizeof returns the number of bytes occupied by the operand or parameter automatically (so that you don't have to count it manually). If the file is good, the header is fully read. That can be checked by this :

if actual=sizeof(header) then good header } :


file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (12 of 23)8/3/2005 7:44:17

{ The file has a

Pascal Tutorial - Chapter 14

:
But .... wait ! I saw somebody using untyped file with write and read. Well, that kind of person treating typed file as untyped one. That causes a LOT of pain. That's why I'm not going to teach it. But, if you insist, you can write me. That's all about untyped files.

File Commands
Now, we're going to discuss other file commands : 1. 2. 3. 4. 5. 6. 7. 8. 9.

Rename Erase Getenv FSearch, FExpand and FSplit FindFirst and FindNext UnpackTime and PackTime GetFTime and SetFTime GetFAttr and SetFAttr DiskFree and DiskSize

Number 3 through 9 need DOS unit

Rename, just like its name is to rename files. You must assign the old file name to a file variable, (the file is not necessarily be opened) then use rename :

assign(f,oldname); rename(f,newname); Erase, is to erase files. You assign the file you want to erase, then erase it. You may
NOT open the file. If you've already opened it, close it first before erasing !

assign(f,filename); erase(f);
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (13 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

You have used the crt unit so long and nothing else. Now, it's time to corporate DOS unit, so you'll probably do this :

uses crt, dos;


You need no files to add as crt and dos are both in SYSTEM.TPL. SYSTEM.TPL is always loaded when Pascal starts. Why do we need DOS unit ? Well many of file routines (that has been mentioned as number 3 thru 9) is in DOS unit. Also, interrupt handling, system time and other handy things reside in it. Let's cover the file-handling routines. Getenv

Getenv fetches the environment string of DOS. Go to command prompt of DOS, then type SET then press Enter. DOS displays all the environment string, such as PATH, PROMPT, etc. This is example of how to get the PATH contents (s is a string) :

s:=Getenv('PATH');
Getting PROMPT is similar : s:=Getenv('PROMPT'); FSearch

FSearch do searching files in a specified directory. Suppose you want to search for FORMAT.COM in DOS and WINDOWS directory :

uses dos; var s : string; begin s:=FSearch('FORMAT.COM','C:\DOS;C:\WINDOWS');


file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (14 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

if s='' then writeln('FORMAT.COM not found') else writeln('FORMAT.COM found in ',s); end.
When found, s returns complete path and filename, otherwise empty. You may extend the directory list (the second parameter of FSearch) using semicolon such as :

... FSearch( ... , 'C:\DOS;C:\WINDOWS;C:\SCAN;C:\TOOL');


You may wonder that you can even search a file in the PATH environment variable. Yes, you COULD ! Do it like this :

... FSearch( ... , getenv('PATH'));


FExpand

FExpand expands a simple file name into a full name (drive, full directory, and the file
name itself). It is especially useful when user inputs a relative directory, like this (s is a string) :

s:=FExpand('..\README');
S will be like this (for example) : 'C:\PASCAL\LESSON.1\README' FSplit It is just contrary to FExpand, splits a fully qualified name into directory, file name, and extension. Example :

var
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (15 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

s d n e

: : : :

string; dirstr; namestr; extstr;

: : : s:='C:\WINDOWS\WIN.INI'; fsplit(s,d,n,e); { d = 'C:\WINDOWS\', n = 'WIN', e = '.INI' }


Look at this program for better details.

uses dos; var s : string; d : dirstr; n : namestr; e : extstr; begin s:=FSearch('FORMAT.COM',getenv('PATH')); if s='' then begin writeln('FORMAT.COM not found'); halt; end; writeln('FORMAT.COM found in ',s); fsplit(s,d,n,e); writeln(d,' ',n,' ',e); end.

file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (16 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

FindFirst and FindNext

FindFirst and FindNext is used just like dir command in DOS. It uses the TSearchRec record tag. The syntax of them :

findfirst(qualifier,attribute,searchrec); findnext(searchrec); qualifier can be '*.*' or '*.PAS' or any wildcard, just like the parameter in dir
command. The attribute can be :
Attribute If you want to search for

ReadOnly read-only files Hidden hidden files SysFile system files VolumeID disk volume label Directory directories Archive archive files AnyFile any files

TSearchrec is defined as follows :

type TSearchRec = record Fill: Attr: Time: Size: Name: end;

array[1..21] of Byte; Byte; Longint; Longint; array[0..12] of Char;

Suppose I have the variable s declared as TSearchrec, and I'm using the findfirst to search '*.EXE' with any attribute :

file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (17 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

findfirst('*.EXE',AnyFile,s); s.name s.size s.time created s.attr holds the name holds the size in bytes holds the date and time when the file is holds the attribute

You should never touch the fill field. It is classified. I personally don't know what does it for, only folks in Microsoft did, perhaps.

FindFirst is used to INITIATE the search. FindNext is used to do the subsequent search. You may repeat FindNext as many as you want to retrieve all the filenames you desire. If there are no file names left, the variable DosError is set to 18. For safety reason, you could repeat findnext while the DosError remains 0, like this :

uses dos; var s : TSearchrec; q : string; begin write ('Enter qualifier to search = '); readln(q); findfirst(q,AnyFile,s); while DosError=0 do begin writeln(s.name); findnext(s); end; end.

file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (18 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

You may wonder how file date and time can be depicted as long integer. DOS is actually packed them. So, after the call of findfirst and findnext, the s.time is in a packed form. How to unpack them ? Use unpacktime. The syntax is :

unpacktime(s.time,dt); s.time is the packed form, dt is the unpacked form. Dt is a record of DateTime,
defined as follows :

type DateTime = record Year,Month,Day,Hour, Min,Sec: Word; end;


So, after unpacktime : dt.year holds the year dt.month holds the month, and so on (respectively). Look at the following example, that display the file with its size and its date/time :

uses var dt s q

dos; : DateTime : TSearchrec; : string;

begin write ('Enter qualifier to search = '); readln(q); findfirst(q,AnyFile,s); while DosError=0 do begin
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (19 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

unpacktime(s.time,dt); write (s.name :15, s.size:8,' '); write (dt.month:2,'/',dt.day:2,'/',dt.year:4,' writeln(dt.hour :2,':',dt.min:2,':',dt.sec); findnext(s); end; end.

');

How about the attribute ? Is it packed too ? Not exactly. To detect each attribute you must do this :

if s.attr and ReadOnly = ReadOnly then write('Read only '); if s.attr and Hidden = Hidden then write('Hidden '); if s.attr and SysFile = SysFile then write('System '); : : and so on.
You can do that detection routine to any attribute name shown above EXCEPT for AnyFile. How can I search for ReadOnly or Hidden files only, but not archives and system files ? Do this at findfirst :

findfirst(q,ReadOnly or Hidden,s);
You may combine the attribute with OR, not and. Also, you may not combine AnyFile with others, because the combination would not take effect. Actually, processing attributes is a bit-wise operation, which I haven't taught you yet (later in lesson 2). But I made it as simple as possible, so that you can understand.
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (20 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

PackTime, GetFTime, and SetFTime

UnpackTime has been described above. Now you may wonder how to PackTime. PackTime is used like this :

packtime(dt,n); dt is DateTime variable (unpacked time), n is a long integer holds packed time. What does it for ? It's for setting the file time by setftime :

setftime(f,n); F is any file variable (text, typed or untyped), n is the packed form of date/time. Don't
forget to assign f with a file name first.

GetFTime returns the date/time of the file :

getftime(f,n); F and n is the same as in setftime. After getftime, don't forget to unpack it first with unpacktime.
GetFAttr and SetFAttr Likewise, you can set or get a file's attribute using setfattr and getfattr. Suppose f is any file variable (text, typed, or untyped), and n is a word variable :

getfattr(f,n);
This will get the file attribute in n. Don't forget to associate f with a file name first. Detecting the attribute is just the same as shown in findfirst and findnext. Setting attribute takes the same syntax. Suppose you want to set the file as hidden and read-only :
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (21 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

n:=ReadOnly or Hidden; setfattr(f,n);


DiskFree and DiskSize

DiskFree returns the number of bytes free in a specified drive. DiskSize returns
the disk total size of a drive. Both take a byte as parameter :

n:=DiskFree(d); n:=DiskSize(d);

{ d is a byte, n is a longint }

d is the drive qualifier. D = 0 means that you want to search info in the current drive, d = 1 for drive A, d = 2 for drive B, d = 3 for drive C and so on. If n = -1, means that you have input an invalid drive.

Directory commands
We'll discuss these directory commands :

1. 2. 3. 4.

Chdir Mkdir Rmdir Getdir

Chdir, Mkdir, and Rmdir is just the same as DOS command cd, md, and rd.
All of them takes a string parameter :

chdir('\DOS'); mkdir('\TEMP'); rmdir('\TEMP');

{ same as cd\dos } { same as md\temp } { same as rd\temp }

Getdir is used to get the directory, just like you type cd in DOS prompt, then press enter. The difference is that getdir can be used to get the current directory of any
file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (22 of 23)8/3/2005 7:44:17

Pascal Tutorial - Chapter 14

drive, not just current drive as cd does. Getdir syntax is like this :

getdir(d,s);

{ d is a byte, s is a string }

d is the drive qualifier. D = 0 means that you want to search info in the current drive, d = 1 for drive A, d = 2 for drive B, d = 3 for drive C and so on. S will return the directory. If d is invalid s just say the root directory. Suppose d:=23 (drive W) is invalid, s will return 'W: \' as it were a root directory. Phew ! A tedious lesson has finished. That's all folks ! If you DO understand what I've taught so long, then congratulations ! If you might want to continue the second lesson, wait for another issue ! So long !

Where to go ?
Back to main page Back to Pascal Tutorial Lesson 1 contents To the quiz Back to Chapter 13 about text files

file:///F|/Documents/Pascal_Doc/pas/pasl1013.html (23 of 23)8/3/2005 7:44:17

Pascal Lesson 1 : Quiz 1

Quiz for Chapter 1


If you haven't read the lesson, please do so ! Time : 45 minutes

Part I (Each scores 5)


What does structured programming mean ? What is the line uses for ? Pascal programs always begin with _______ and end with _______ What is the difference between Write and Writeln ? How can we write two blank lines on the screen ? Mention at least 5 variable type names with their range and types ! How can we get user's input ? What is the difference between Read and Readln ? Why can't we enter the value 0.75 to word variables ? How can we view and pause the screen after the program ran ? How can we declare variables in Pascal ? What key should we press to run a program in Borland Pascal 7.0 ? How can we limit the fractional output to 3 places before and after the decimal point ? 14. Explain how can the string be limited on display. 15. Can we enter the value "1/4" to Real variables ? 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

Part II (number 1 scores 5, else scores 10 each)


1. Make a program to write your name and your age on screen. 2. Make a program to input user's comment and limit it to 20 characters. 3. Make a program to calculate the area of a circle. Limit the fractional part 3 places before decimal point and 4 places after decimal. That's all ! Good luck !

Where to go ?
file:///F|/Documents/Pascal_Doc/pas/pasq1000.html (1 of 2)8/3/2005 7:45:09

Pascal Lesson 1 : Quiz 1

Back to chapter 1 Advance to chapter 2, about extending simple program To lesson 1 contents My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1000.html (2 of 2)8/3/2005 7:45:09

Pascal Lesson 1 : Quiz 2

Quiz for Chapter 2


Time : 30 minutes
Read Chapter 2 if you haven't done so ! Each scores 10 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. What is TextColor and TextBackGround for ? List the value accepted as TextColor parameter with its color ! The same as number 2, but for TextBackGround. How can we switch between text modes ? Mention at least three values that is accepted for changing text modes. How would it be if we specify X and Y parameter in GotoXY exceeds the screen resolution ? Explain shortly all about text modes in PC ! Why must we add Delay between Sound and NoSound ? If we omit NoSound statement, what will happen ? How was the formula to assign colors in TextAttr ?

That's all folks ! Good luck !

Where to go ?
Back to chapter 2 Advance to chapter 3, about branching (IF) To lesson 1 contents
My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1001.html8/3/2005 7:45:13

Pascal Lesson 1 : Quiz 3

Quiz for Chapter 3


Now, we'll begin the quiz. If you haven't got a good grip on chapter 3, read that chapter first.

Time : 45 minutes.
Number 1 to 5 scores 12 each, number 6 and 7 scores 20 each. What does Pascal provide in order to perform conditional branching ? Explain briefly each syntax ! How could we do nested branching ? Is it legal ? Tell us how to trap arrow keys ! How could we trap function keys (F1 through F10) ? How is it similar to trapping arrow keys ? 6. Determine the output if i=5 and j=3 for the following excerpt : 1. 2. 3. 4. 5.

if i<4 then writeln('Need more experience')


else begin writeln('Yes, you have it !'); if j<=3 then writeln('But, I would prefer better ones.') else writeln('Superb !'); writeln('Congratulations !'); end; writeln('Thank you for using this program !');

7. Make a program to ask ages. Classify the ages into these categories :
Age Comment

<2 2 <= age < 12 12 <= age < 18 18 <= age < 24 24 <= age < 40 40 <= age < 55 55 <= age < 65 age <= 65

You are a baby ! You are a kid ! You are a teenager ! You are a young adult ! You are an adult ! You are middle aged ! (Give comments yourself) (Give comments yourself)

Where to go ?
Back to chapter 3
file:///F|/Documents/Pascal_Doc/pas/pasq1002.html (1 of 2)8/3/2005 7:45:23

Pascal Lesson 1 : Quiz 3

Advance to chapter 4, about constants To lesson 1 contents


My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1002.html (2 of 2)8/3/2005 7:45:23

Pascal Lesson 1 : Quiz 4

Quiz for Chapter 4


Here the quiz comes ....If you haven't got a good grip on chapter 4, read that chapter first.

Time : 15 minutes.
1. 2. 3. 4. How could we declare constants in Pascal ? Give some example. Could we modify constants ? Explain briefly. Give some example of predefined constants in Pascal. Now, modify your existing circle program (calculate the circle's area) using constants.

Each has 25 points. Go for it ! Good luck !

Where to go ?
Back to chapter 4 Advance to chapter 5, about looping To lesson 1 contents
My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1003.html8/3/2005 7:45:29

Pascal Lesson 1 : Quiz 5

Quiz for Chapter 5


Now, let's have a quiz ! If you haven't read the lesson, you had better do it now before starting.

Part I : Theory (Solved in 30 minutes)


1. Explain the characteristics of all three loop syntaxes in Pascal ! 2. Explain the differences between them ! 3. Suppose the output is :

4. 5. 6. 7. 8. 9. 10.

1 2 3 4 5 6

Write an excerpt using all three syntaxes ! 11. Explain how to nest one syntax to another !

Part II : Practical
In part II, I will give you the sample input and output. You must write an excerpt, using only ONE of the loop syntaxes provided by Pascal. The choice is up to you. As long as they can be run correctly, you get the points. Tip : Before entering this quiz, practise a lot ! A. Easy (Solved in 25 minutes)

1. Input : 5 Output : 1 2 3 4 5 4 3 2 1 4 5 Input : 1 4 5


file:///F|/Documents/Pascal_Doc/pas/pasq1004.html (1 of 5)8/3/2005 7:45:36

2. Input : 5 Output : 1 2 3 1 2 3

Pascal Lesson 1 : Quiz 5

Output : 1 4 5

1 2 3 1 2 3

4 5 3. Input : 5 4 5 Output : 1 2 3 4 5 4 5 5. Input : 5 Output : 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5

1 2 3 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 5 1 2 3 4 4. Input : 5 Output : 1 1 1 1 1

2 2 3 2 3 4 2 3

6. Input : 5 Output : 1 1 1 2 3 3 4 4 4 5 5 5 5 2 2 2 3 3 3 4 4 4 5 5 5

Here is the criteria -- If you solve it correctly in :


Time in minutes Category

<= 3 3 < time <= 6 6 < time <= 10 10 < time <= 15 15 < time <= 20 20 < time <= 25

Amazing ! Great ! Very good ! Good ! Average Below average

file:///F|/Documents/Pascal_Doc/pas/pasq1004.html (2 of 5)8/3/2005 7:45:36

Pascal Lesson 1 : Quiz 5

> 25 B. Intermediate (Solved in 30 minutes)

Poor

7. Input : 5 Output : 1 1 2 1

1 1 2 1 1 2 3 2 1

8. Input : 5 Output : 1 1 1 1 2 2 1 2 3 1 2 2 1 1 1 1 2 1 2 2 1 1 1 1 1

2 1 1 2 3 4 3 2 1 2 1 1 2 3 4 5 4 3 2 1 1 1 1 1 2 1 2 2 1 1 1 1 2 3 2 1 3 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 3 3 2 1 2 3 2 1

9. Make fibonacci series. If input is n, write all the series up to n numbers : Input : 10 Output : 1 1 2 3 5 8 13 21 34 55 The first and the second numbers of fibonacci series are 1. The third is the sum of the first and the second. The fourth is the sum of the second and the third. The fifth is the sum of the third and the fourth, so on. 10. Make a factor tree.

11. 12. 13. 14. \ 15. 50 16.

Input : 100 Output : Output :

Input 100 1001 / /\ 2 7 143 /

: 1001

file:///F|/Documents/Pascal_Doc/pas/pasq1004.html (3 of 5)8/3/2005 7:45:36

Pascal Lesson 1 : Quiz 5

\ 17. 25 18. 19. a prime !

/\ 11 2 13 /\ 5 5 Input : 5 Output : 5 is

20. Text animation : Input : abcdefghijklmno Output : The text will bounce around the screen until a key is pressed. For better details, run 11.exe. Note : Original filesize is 4075 bytes. Do not disassemble 11.exe ! It is considered as cheating !! Here is the criteria -- If you solve it correctly in :
Time in minutes Category

<= 5 5 < time <= 10 10 < time <= 15 15 < time <= 20 20 < time <= 25 25 < time <= 30 > 30 C. (not so) Hard (Solved in 45 minutes)

Amazing ! Great ! Very good ! Good ! Average Below average Poor

Create a game just like a space invader (you've got to know that), but in normal text mode (80x25). Use colors as pretty as possible. Normally space invader have a matrix of enemies, but I make allowances to you. You just make only one enemy. If the enemy is destroyed, it just reappears at a different place and the player gains a point. Each players have 3 ships to play. If all ships are used up (shot down) you show Game Over message and ask if the player want to play again. Each enemy ship scores 10. Player got an extra ship when reaches 500 and its multipliers (1000, 1500, 2000,...). Players can only have one shot. Before the fire is out, players can not trigger another shot. The same rule applies for the enemy. Player could only move his/her ship horizon tally (left or right), not vertically or diagonally. The enemy may come closer to the ship after it reaches the edge (of left or right). I mean that the enemy ship go left or right, after it
file:///F|/Documents/Pascal_Doc/pas/pasq1004.html (4 of 5)8/3/2005 7:45:36

Pascal Lesson 1 : Quiz 5

touches the edge, it advances a bit -- just like a normal space invader game. Make it as fancy as possible. If you made it correctly in :
Time in minutes Category

<= 15 15 < time <= 25 25 < time <= 30 30 < time <= 35 35 < time <= 40 40 < time <= 45 > 45

Amazing ! Great ! Very good ! Good ! Average Below average Poor

Where to go ?
Back to chapter 5 Advance to chapter 6, about procedures and functions To lesson 1 contents
My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1004.html (5 of 5)8/3/2005 7:45:36

Pascal Lesson 1 : Quiz 6

Quiz for Chapter 6


Let's quiz ! If you haven't read the lesson, you had better do it now before starting.

Time : 60 minutes
1-8 scores 5, 9 scores 10, 10 scores 50 1. Explain the syntax of procedures and functions. 2. Explain the differences between them. 3. Explain the differences between pass by value and pass by reference. Give an example. 4. Can procedures nest each other ? Explain and give example ! 5. Explain the differences between global variable and local variable. 6. Explain the rule of calling procedures / functions. 7. Explain how recursive calls and forward calls works. 8. Tell me the differences between recursive calls and forward calls. 9. Make a recursive algorithm of fibonacci series ! 10. Make a guessing number game using functions and procedures. You hold a random number between 1 to 100, and user have to guess. If the guess is smaller than it suppose to be, you should say 'lower'. If the guess is higher, say 'higher'. If it is the same, then user wins. User has 8 chances to guess. If chances are used up, game over. Score is number of chances left times 10. If user wins, get another number and go on. Don't forget to restore the chances back to 8. If game over, ask him/her to play again. Here are some hint :

11. function 12. shortint; function 13. string; function 14. function 15. function 16. procedure 17. string); procedure 18.

compare(number, guess : byte): advice(comparison : shortint): asknumber: byte; yesno : boolean; score(guessleft : byte):longint; writeat(x,y : byte; sentence: setupscreen;

file:///F|/Documents/Pascal_Doc/pas/pasq1005.html (1 of 2)8/3/2005 7:45:47

Pascal Lesson 1 : Quiz 6

Where to go ?
Back to chapter 6 Advance to chapter 7, about arrays To lesson 1 contents
My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1005.html (2 of 2)8/3/2005 7:45:47

Pascal Lesson 1 : Quiz 7

Quiz for Chapter 7


Let's quiz ! If you haven't read the lesson, you had better do it now before starting.

Theory (15 minutes. Each scores 5)


1. 2. 3. 4. 5. 6. What is an array ? What is it for ? How is the declaration ? Explain how to use it ! How can we declare two or more dimension in array and explain the usage. Suppose we have n : array[char] of byte; Is it valid ? Explain.

Practices
A. Easy (10 min) 1. Make a Pascal triangle, example :

2. 3. 4. 5. 6. 7. 8. 9. 10.

Input : 7 Output :

1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1

11. Suppose you have an array 100 elements of random numbers. Find its maximum and minimum value of the elements.

B. Intermediate (20 min) 3. Make matrix operations : addition, subtraction, multiplication and division. You may limit the maximum dimension of the matrix.
file:///F|/Documents/Pascal_Doc/pas/pasq1006.html (1 of 3)8/3/2005 7:45:58

Pascal Lesson 1 : Quiz 7

4. Make a menu that highlights the selection. User can press up and down arrow and press enter to make a choice.

C. Challenge (45 min. Choose one of these) 5. Make a jackpot game. It has 4 digits. Each digits is represented by numbers. Jackpot can have 4 players to play. Each players has $500 at start and must place their bet of $20 every game. Winning points :
If Bet returned plus bonus

Any two numbers are the same Any three are the same All four numbers are the same All four are in sequence e.g. 1 2 3 4 or 5 4 3 2 All four are even or all odd e.g. 1 7 5 3 or 2 6 4 8 All four are prime numbers Two numbers are the same and the other two are the same too None of above

10% 25% 100% (bet doubled) 50% 5% 10% 75% bet lost

6. Remember, in one digit, possible primes are : 2, 3, 5, and 7 only. Example :


Sequence Bonus

1122 2121 2122 9537 7235 4321 1832

75% 75% 25% 5% 10% 50% lost

7. Players lose if he/she has less than $20 and cannot continue the game. The winner is who can stay after all players are gone. 8. Make a blackjack game for 4 players. Players put their bet in the pot. The winner get the pot. Initially, players has $500. Each bet minimal $5. Players lose if he/she has less than $5 and can not continue the game. The winner is who can stay after all players are gone. Winning rank : 1. Instant blackjack (Ace of spade and Jack of spade)
file:///F|/Documents/Pascal_Doc/pas/pasq1006.html (2 of 3)8/3/2005 7:45:58

Pascal Lesson 1 : Quiz 7

2. Blackjack (any Ace and jack of the same pattern) 3. Blackjack (any mixed ace and jack) 4. Indirect blackjack (all summed up of 21) 5. Bingo (5 cards already but is 21 or less/not burnt out) 6. Any number (the bigger the score, is the winner) If you have two or more players won with the same rank, you should divide the pot equally. If all is burnt (all cards are 22 or up), then there are no winner, and the pot stays, but players should place another bet in the pot for the next game. Hint : Heart ASCII code is 3, Diamond is 4, Club is 5, and 6 for Spades. 9. Make a poker game. The rule is the same as blackjack, but you may raise, drop, or follow. (Just like normal poker game) 10. Space invader revisited. Last time you made space invader. Now you make a full version of Space Invader with matrices of enemies. You may extend it with bonus stages or UFO. All rules remain the same.

Where to go ?
Back to chapter 7 Advance to chapter 8, about strings To lesson 1 contents
My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1006.html (3 of 3)8/3/2005 7:45:58

Pascal Lesson 1 : Quiz 8

Quiz for Chapter 8


Let's have a quiz ! If you haven't read the lesson, you had better do it now before starting.

Practices (15 min)


1. Make a set of FUNCTIONS to perform these tasks : H Make all letters in a string upcase. H Eliminate trailing spaces. H Eliminate leading spaces. H Change leading spaces with leading zeroes. H Count the number of word in a sentence 2. Make a procedure to replace a substring within a string with another substring. 3. Revise the makewin procedure in chapter 5. Use fillchar instead of dirty loops.

Entertainment (30 min)


4. Make a text animation. Display a sentence in the center of the screen. Let all characters fall one by one. Skip the spaces ! 5. Make a text animation. Run 12.exe from 12.ZIP. It should be 4605 bytes. 6. Make a game of garbled word. Suppose you have a word. Garble it so that the sequence can not be read. User must guess the word.

Challenge (45 min each)


7. Make a simple calculator that can perform addition, substraction, multiplication and division. User input the equation then press enter. You should give the answer. Example : Input : 6*3+4*(5+3) Output : 50 You MUST regard the precendence of operators. 8. Make a game just like Wheel of Fortune, for 3 players. All the rules are the same as Wheel of Fortune. You may use random number to simulate spin wheel.

Advanced (14 days) (You may do it or not, optional)


User input a sentence. You must identify each word in a sentence, e.g. :
file:///F|/Documents/Pascal_Doc/pas/pasq1007.html (1 of 2)8/3/2005 7:46:06

Pascal Lesson 1 : Quiz 8

Yesterday, I walked around the park with my dog at 10am.


Output Parsed Words Kind of Word

Yesterday I walked around the park with my dog at 10am

time signal noun verb verb remarks definitive particle noun particle possessive particle noun particle time signal

That's all. If you do number 9 correctly within 14 days, you are superior in computer programming.

Where to go ?
Back to chapter 8 Advance to chapter 9, about records To lesson 1 contents My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1007.html (2 of 2)8/3/2005 7:46:06

Pascal Lesson 1 : Quiz 9

Quiz for Chapter 9


Now, I assume all of you understand the theory. I hate theorems. I'm sure you do too. Therefore, from now one, I will give you practical quizzes only. Theory quiz will be given ONLY if I feel it is necessary.

(150 minutes)
1. Make a database. The record tag, TStudent consists of these fields :
Field Length

Name 20 Address 40 Phone 15 Quiz number, from 1 to 100 Midtest number, from 1 to 100 Finaltest number, from 1 to 100 2. User could : H add new student H edit existing student H view student in a list H delete the record H sort the record according to : I Quiz I Midtest I Finaltest I Overall value (Overall value = 0.1*Quiz + 0.4*Mid + 0.5*Final) You must :
H H H H

Provide a menu-driven feature, using arrow key. Make use of up and down arrow with PgUp and PgDown. At least be able to handle 200 students. Apply quick sort for at least one key-sort.

file:///F|/Documents/Pascal_Doc/pas/pasq1008.html (1 of 2)8/3/2005 7:46:15

Pascal Lesson 1 : Quiz 9


H H

Provide 'Quit to OS' feature. Apply search for student name first before editing the data.

Tip : Use array of records. If you are done this correctly within 100 minutes, you are superior to others. 100 to 120 minutes, you are average programmer. 120 to 150 minutes, you are slow programmer.

(40 minutes)
2. Change your Space Invader enemy array into array of records, containing the x, y position of each enemy and whether it is died already or not. That's all for this time.

Where to go ?
Back to chapter 9 Advance to chapter 10, about complex data structure. To lesson 1 contents My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1008.html (2 of 2)8/3/2005 7:46:15

Pascal Lesson 1 : Quiz 10

Quiz for Chapter 10


Have you read the lesson ?

Time : 30 min each


1. Change your space invader game into enumerated types, i.e. : enemy = (robot, alien, ufo); etc... 2. Change your poker or black jack game into enumerated types :

turf = (spade, heart, club, diamond);


numbers = (two, three, ... , jack, queen, king, ace);

Note : Changing your program doesn't mean you must alter all of the logic ! If you have designed the program carefully, you only make a little alteration. I intend this to build up your program design skill.

Challenge : 3 days
Make a pac-man game, using your own random number generator. You have 4 ghosts in the game, player has 5 lives. It has a super bullet that makes ghosts eatable. Just like a normal pac-man. You must record the highest score, but not in file (since I haven't taught you yet). Hint :
G G G G

Use C as the Pac-man Use . as the small bullet Use * as the big bullet For ghosts you may use U

Make this as interesting as possible. That's all !

Where to go ?

file:///F|/Documents/Pascal_Doc/pas/pasq1009.html (1 of 2)8/3/2005 7:46:25

Pascal Lesson 1 : Quiz 10

Back to chapter 10 Advance to chapter 11, about sets. To lesson 1 contents My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1009.html (2 of 2)8/3/2005 7:46:25

Pascal Lesson 1 : Quiz 12

Quiz for Chapter 12


Don't continue if you have not yet understand the lesson

Time : 15 min
Make a unit consist of functions that convert binary numbers into decimals and vice versa, also hexadecimal numbers to decimals, vice versa. Use it in a sample program. Hints : Use strings for output for binary and hexadecimal numbers.

Time : 45 min
Rebuild your black jack and poker game. You must build a unit for the cards since both games use cards. You must at least provide : 1. ShuffleCard for shuffling the deck, especially at the beginning. 2. RetrieveCard retrieve a card, return 0 for no card is left. 3. DrawCard (cardtype; x,y) This draws cardtype card at x,y. Example : DrawCard(JackofSpade,14,5) will draw Jack of Spade at 14,5 You must hide as much information as possible inside the unit, prevents user to modify your status. That's all ! Good bye !

Where to go ?
Back to chapter 12 Advance to chapter 13, about text files. To lesson 1 contents My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1011.html8/3/2005 7:46:33

Pascal Lesson 1 : Quiz 13

Quiz for Chapter 13


Don't continue if you have not yet understand the lesson

Time : 15 min
1. Make a simulation of DOS' type command. 2. Make a program that could copy text files.

Challenge :
In 45 minutes, you shall have written a readme program that could access just every text file. You may limit the maximum number of lines of the file say 400 lines.

Where to go ?
Back to chapter 13 Advance to chapter 14, about binary files. To lesson 1 contents My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1012.html8/3/2005 7:46:42

Pascal Lesson 1 : Quiz 14, Final Exam

Quiz for Chapter 14 (Final Exam)


Don't continue if you have not yet understand the lesson

Time : 30 min for number 1 to 3


1. Make a function to detect whether it is a text file or not. Return true if it does. 2. Make a simulation for dir command in DOS. 3. Make a simulation for copy command in DOS.

This number : 3 hours.


Modify your database in chapter 9 so that it can safe data into disk. All features remains the same, except : 1. Re-design your menu as pull-down menu. 2. Your program has File menu, that has options : New, Save, Load, Save As, and Exit 3. Make a security option, i.e. input passwords before entering. User can modify the password as they like.

This number : 5 whole days.


Make a text editor, just like DOS that has feature : Pull-down menu. File menu MUST have : New, Save, Load, Save As, and Exit. You may limit the maximum number of lines that can be edited : 500 lines. You MUST provide at least 4 Edit functions : Cut, Copy, Paste, and Clear. You MUST provide the shortcut keys for each menu. You MUST provide at least 3 Search functions : Find, Find-and-Replace, and Repeat. 7. You MUST warn user if their files hasn't been saved when quitting or loading other file. 1. 2. 3. 4. 5. 6.

This number : 1 hour.

file:///F|/Documents/Pascal_Doc/pas/pasq1013.html (1 of 2)8/3/2005 7:46:53

Pascal Lesson 1 : Quiz 14, Final Exam

Modify your Space Invader so that it could save top 10 high scorer.

Challenge : 10 days
Make a file manager or Explorer. You must be able to do : Shows the directory tree, can be collapsed or expanded. Select/Unselect files. Copying, renaming, and moving single/multiple selected files and directory. Change, create, and remove directory Deleting files and directories (like deltree). Perform a Hexadecimal view of any selected file. Sort filenames according : name, extension, date/time of creation and filesize. Can display files of certain attribute. User is asked what attribute does he/she want to view at. 9. Perform an error handler so that whenever error occurs, it just never quits, but asked user what to do (Retry, Ignore or Cancel). 1. 2. 3. 4. 5. 6. 7. 8. Formatting disks will be discussed in lesson 2, so don't bother doing it.

Where to go ?
Back to chapter 14 To lesson 2 contents To lesson 1 contents
My main tutorial homepage

file:///F|/Documents/Pascal_Doc/pas/pasq1013.html (2 of 2)8/3/2005 7:46:53

Anda mungkin juga menyukai