Tuesday, October 27, 2009

9. Arrays in Pascal

If we want to store 5 names in the computer, we have to name them (name1,name2 etc.) and assign values to them individually. To retrieve also we have to retrieve them individually. Imagine how tedious it is going to be if we are writing a program to do pay roll for a walmart.

To make life easy, Pascal gave us, array. Pascal reserves space as indicated in the array definition and allowed us to access the space through an index. There are two ways of defining an array:

1. Here I defined a type called numbers with 15 spaces reserved

type numbers = array[1..15] of integer;

Here I equated a variable to numbers array

var numb : numbers;


2. var name :array[1..20] of string;
(* Here I defined array named name with 20 spaces*)

Following program describes the manipulation of arrays in Pascal:
Program program9;
(*Author: Rao S Lakkaraju*)
(* My pascal program9*)
{ Example of arrays }
uses crt,graph;

type numbers = array[1..15] of integer;
var numb : numbers;
name :array[1..20] of string;
index1 : integer;

begin
clrscr;
textbackground(white);
textcolor(red);
writeln(' My pascal program9');
writeln(' Array Values');

for index1 := 1 to 10 do
begin
numb[index1] := index1 + 1;
write(numb[index1]);
end;

writeln;
writeln('please enter name');
readln(name[1]);

for index1 := 2 to 10 do
begin
name[index1] := name[1]+ name[index1 - 1] ;
write(name[index1]);
writeln('yes');
end;

readln;
end.

Summary: Best way to store multiple values of a same type of variable in Pascal is the array[]. Pascal reserves space depending on the size and type of the array. We can use an index to store and retrieve values for processing.

Monday, October 26, 2009

8. Procedures in Pascal

We can look at the Pascal procedures as extensions to pascal functions except we do not assign value to the function name. Another difference is if we define multiple parameters in the procedure, each should be ended by a semicolon.
Here is a program which includes a procedure for converting minutes into hours and minutes. I introduced two new statements for crt, textbackground(white) and textcolor(red). The program is self explanatory.

Program program8;
(*Author: Rao S Lakkaraju*)
{ My pascal program 8 }

uses crt,graph;
var minutes : integer;

(* Procedure for converting minutes into hour and minutes *)

procedure time(minutes : integer);
var hr,min : integer;

begin
hr := minutes div 60;
min := minutes - hr * 60;
writeln;
textbackground(white);
textcolor(red);
write(minutes,' minutes equal to ', hr:2,' hours and ');
write(min:2, ' minutes');
writeln;
end;


{ Main Program Begins here }
begin
clrscr;
writeln(' My eigth pascal program ');
writeln('Please enter minutes ');
readln(minutes);

(*Call to the Procedure*)
time(minutes);

readln;
end.

Summary: If multiple statements are repeatedly used in a program, they could be put together to form a named procedure and be called by simply the procedure name wherever the statements are needed. Calling the procedure is simple, procedure name with parameters in parenthesis.

Sunday, October 25, 2009

7. Functions in Pascal

If we want to do same thing again and again, we write all the instructions to do that job at one place and call them whenever it is needed. In Pascal we have two language structures, functions and procedures, for repetitive tasks. Basically we write code to calculate the value with generic variables and give a name to it. We call the code using the given name with parameters if any whenever we need it.

Here we write the function to return square of a given number. The code follows:
function sqr(z : integer) : integer; begin sqr:= z * z; end;
z is an integer parameter. We can add more parameters with "," in between. The function name sqr declared as integer gets assigned the value of the computation. We call this function by inserting function name.
EX: y := sqr(x);

Program Program7;
{ My seventh pascal program }

uses crt,graph;
var x,y : integer;


function sqr(z:integer) : integer;
begin
sqr := z * z;
end;

{ Program Begins Here}
begin
clrscr;
writeln('My seventh Pascal program');

for x := 1 to 10 do
begin
y := sqr(x);
writeln('square of ':10,x:4, ' is ', y:4);
end;

readln;

end.

Summary: Pascal functions are used to package repetitive instructions and call them when they are needed. Every function have a name. The body begins with a begin followed by statements and ends with end;The computed value is returned to the name of the function with the specified type.

Saturday, October 24, 2009

6. Output Manipulation

We use computers to do complex things faster and would like to present results in a prettier looking understandable format. There are various techniques to do that and we will examine one by one.

1. Using a writeln statement without any parameters to skip a line. EX: writeln;

2. Using string variables in a writeln statement.
Ex: writeln('First Name Last Name'); **** Prints heading First Name Last Name as written in the string.

3. Using field parameters for variables in writeln statement.
Ex: writeln('First name' : 10, 'Last Name' : 12);
Write First Name in 10 spaces. Write Last Name in 12 spaces. Right justified always.

4. using field parameter for decimal places in the case of real numbers.
Ex: writeln('number3= ': 10, number3 :12:2);
Write the string number3= in 10 spaces and the number3 real variable in 12 places with 2 decimals.

In the case of wrong space field specification, computer will correct and display the whole value of the variable.

Program using these techniques follows:

Program program6;
(*Author: Rao S Lakkaraju*)
{ This is my sixth pascal program }
(* Output Manipulation in Pascal *)
uses crt,graph;
var n,number1, number2,number3 : integer;
interest1, interest2,interest3 : real;
name1, name2, name3 : string;
rich : boolean;

(* Program begins here *)
begin
clrscr;
number1 := 1;
number2 := 10;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := ' Lakkaraju';

(**** Lesson 4 *****)
for number3 := number1 to number2 do write(number3);

write(' ');

for number3 := number2 downto number1 do begin write(number3); end;

writeln(' ');
name3 := name1 + name2;
interest3 := interest1 + interest2;

textbackground(white);
textcolor(Red);

Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('number3 = ', number3);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('interest3 = ', interest3);
writeln('My Name is ', name3);

(**** Lesson 5 *****)
rich := false;
if not rich then writeln('I am not rich ')
else writeln('I am rich');

while rich = false do rich := true;

n := 1;
repeat n := n + 1; Writeln('I am in repeat'); until n = 8;

n := 2;
case n of 2,3,6 : writeln('value of n is ', n);
otherwise writeln('end of case'); end;

(**** Lesson 6 *****)
writeln;
writeln('First Name' : 10, 'Last Name':12);
writeln;
writeln(name1:10, name2:12);
writeln;
writeln('interest3=' :10, interest3:12:2);
writeln;
writeln('My sixth Pascal Program');
readln;

(* Program ends here *)
end.

Summary: Output from write statements can be formatted by specifying field lengths for each variable to be printed.

Friday, October 23, 2009

5. Condition processing in Pascal

There is another type of variable in pascal called boolean. It can have only two values, true or false. A statement which results in a true or false value is a boolean expression. ex: n = 8 results in a true value for the expression if n equalto 8. Note the equalto (=) sign in the expression which is different from assignment sign(:=).

Pascal provides language structures to control conditional processing. They are as follows:

1. if --condition-- then --statement1-- else --statement2--;
condition could be a = 10 or boolean. Notice the equal(=) sign in the condition.
Statement1 may not have ';' at the end, if it is followed by else. If it has results could be what you do not expect.

2. while --condition-- do --statement;
As long as the While condition is satisfied the statement processing continues. As such the condition should be changed in the statement otherwise the processing continues for ever.

3. Repeat ---- statements ---- until ---- boolean expression ----;
As noted in While the boolean expression value should change sometime or other, otherwise processing continues for ever.

4. Case --variable-- of --vales of variable-- : statement1; otherwise statement2; end;
ex: case n of 2,3,6 : writeln('value of n is ', n); otherwise writeln('end of case'); end;
For values of n equal to 2,3 or 6 write (value of n is) otherwise write (end of case).

Program depicting the conditional statements follows:

Program program5;
{ Author: Rao S Lakkaraju }
{ This is my fifth pascal program }
(* Condition Processing in Pascal *)

uses crt,graph;
var n,number1, number2,number3 : integer;
interest1, interest2,interest3 : real;
name1, name2, name3 : string;
rich : boolean;

begin
clrscr;
number1 := 1;
number2 := 10;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := ' Lakkaraju';

(**** Lesson 4 *****)
for number3 := number1 to number2 do write(number3);
write(' ');
for number3 := number2 downto number1 do begin write(number3); end;
writeln(' ');
name3 := name1 + name2;
interest3 := interest1 + interest2;
Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('number3 = ', number3);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('interest3 = ', interest3);
writeln('My Name is ', name3);

(**** Lesson 5 *****)
rich := false;
if not rich then writeln('I am not rich ')
else writeln('I am rich');
while rich = false do rich := true;
n := 1;
repeat n := n + 1; Writeln('I am in repeat'); until n = 8;
n := 2;
case n of 2,3,6 : writeln('value of n is ', n);
otherwise writeln('end of case'); end;

writeln('My fifth Pascal Program');
readln;
end.

Summary: Pascal provides a variable called boolean which could have only two values true or false. A boolean expression is one which after evaluation results in a boolean true or false value . Pascal provides statement execution depending on the value of a boolean expression. These pascal structures are if, while, repeat and case.

Thursday, October 22, 2009

4. Manipulating Pascal Variables

Variable name implies that it can take many values. The way we do that is to assign values at different times by an assignment statement like num1 := 10;. Writing multiple statements for assigning values is a cumbersome affair. For this purpose pascal has a for statement. It comes in two flavours, we can increase the value of a variable in steps of 1 or decrease the value of a variable in steps of one.

In the following program the for statements are self explanatory. After do we can have only one statement. If we want to have a bunch of statements after the do, we use the begin, multiple statements, end;. We can also see how strings are added. I want to mention write statement writes on the same line whereas writeln moves to a different line after writing the line.

Program Follows:
Program program4;
(* Author: Rao S Lakkaraju *)
{ This is my fourth pascal program }
(* Manipulating Pascal Variables *)
uses crt,graph;
var number1, number2,number3 : integer;
interest1, interest2,interest3 : real;
name1, name2, name3 : string;

begin
clrscr;
number1 := 1;
number2 := 10;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := ' Lakkaraju';
for number3 := number1 to number2 do write(number3);
write(' ');
for number3 := number2 downto number1 do begin write(number3); end;
writeln(' ');
name3 := name1 + name2;
interest3 := interest1 + interest2;
Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('number3 = ', number3);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('interest3 = ', interest3);
writeln('My Name is ', name3);

writeln('My fourth Pascal Program');
readln;
end.

Summary: We can vary the value of a pascal variable by using for statements. we can use begin... end; block to put together a bunch of pascal statements for execution as a block. We can add string variables.

Wednesday, October 21, 2009

3. Variables in Pascal

Variables are entities which vary in their life time. For example Numbers, they could be 1 to 10,000 or they could be 1.25 to 10000.85. Characters could be A to Z or ARAM to Zooram. So the computer needs to know what type of variable we are dealing with or manipulating in the program.

numbers 1 to say 10000 which do not have a decimal are called INTEGER. Those which have decimal are called REAL. variables which represent a group of characters are called STRING.

Let us make a program with variables. Our aim is to use 3 types of variables and print their values using Pascal program. Here is the program. Try it on your computer.



Program program3;
uses crt,graph;
var number1, number2 : integer;
var interest1, interest2 : real;
var name1, name2 : string;
{ This is my third pascal program }
begin
clrscr;
number1 := 21;
number2 := 1004;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := 'Lakkaraju';
Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('My Name is ', name1, ' ', name2);

writeln('My third Pascal Program');
readln;
end.

The symbol ' :=' is called assignment symbol. number1 := 21 means number1 = 21.
name1 := 'Rao' is an assignment but it is a string assignment.
Uses crt,graph; I want the computer to use crt and graphics related language also.
clrscr; means clear the screen before writing. I am using crt language.

Summary: Variables are three types, integer,real and string. integer numbers are numbers without decimals, real numbers are numbers with decimals and string variables are combination of characters. by enclosing combination of characters by quotation marks, we can assign to a string variable, ex: 'David'.

Tuesday, October 20, 2009

2. How to write a pascal program

In general if we want to get anything done by someone, we have to tell exactly what we want. Same is the case with the computers more so than anything else. Unless we specify what we want exactly in the language it can understand, computer can not do our work. If it does the work, the results may be unpredictable and there is no meaning in blaming the computer for our short comings. Following are the steps you consider before telling the computer:

1. First we have to write down what the computer has to do for us.

2. Next we have to determine what basic materials the computer needs to do that work.

3. Next we have to tell the computer step by step what to do with those basic materials to produce the product we desire. Remember computer does things faster than us. If we give wrong instructions, it will give wrong results faster.

Let us try a program. We write a line saying " My first pascal program".
We are going to use pascal compiler. We write instructions for the computer to print "My first pascal program".

Program follows:

Program program1;
{ This is my first program }
begin
writeln(' My first pascal program ');
readln;
end.

Program program2;
uses crt,graph;
var number1, number2 : integer;
var interest1, interest2 : real;
var name1, name2 : string;
{ This is my second program }
begin
writeln('My second Pascal Program');
readln;
end.





The first line of the program says: Program name is program1. Notice the semi colon. Every statement ends with a semi colon ";".Exception is last statement "end.". It ends with a period '.'.

The second statement is a comment. Comments are enclosed by "{" and "}". Notice we do not put any semicolon. Comments are not compiled by the compiler.

The beginning of a program begins with "begin" and ends with "end.".
In between these two we have to give instructions to the computer.we have two instructions. They are called statements.

First statement is to write a line: "My first pascal program".

Second statement is to go to the next line and wait for my input. If we do not put this statement, the computer writes the line " My first pascal program". The second statement makes the computer wait for our input so that we can see the line. Just press ENTER on the key board and the execution goes to next statement.

The final statement is "end." which states this is the end of the program. The execution is complete.

Summary:Pascal program consists of statements, each specifying a step to be done by the computer. Each statement ends with a semi colon(;). The first statement 'Program' states the name of the program to the computer. The program starts with 'begin' and ends with 'end.'. In between these two we can write statements to the computer.

Monday, October 19, 2009

1. Pascal compiler

There are multiple flavors of Pascal compilers. I downloaded a free open source Pascal compiler for WINDOWS (latest release 2.2.4) and installed it. This is very easy to do.
Please go to the following site for the down load:

http://www.freepascal.org/download.var

Once you downloaded it and installed the Pascal compiler, you will see a Free Pascal IDE icon, FPC icon on the computer screen. Double click the icon and you will see a IDE screen to input your Pascal program. Please study various options on the top of the screen. We will be using FILE EDIT RUN COMPILE options very extensively.

The cursor is always at an insert character position. We can type in any characters we want. Using Edit, we can edit the information we entered just like in any editor.

Once a program is ready we can Compile the instructions we typed for machine language translation or use RUN for compiling and executing the Object(machine language instructions).

Sunday, October 18, 2009

0. Introduction

All computers work using a language called machine language. These machine language instructions are etched into computer chips.The instructions of the machine language consists of zeros and ones as the computer understands only binary(0,1). These binary instructions are very cumbersome and difficult to remember.

Scientists created what is called an ASSEMBLER language which has simple English equivalents to binary instructions, like ADD to add two numbers or SUM to sum two numbers. They designed what is called a translator which converts these instructions into binary computer instructions.And the computer will do whatever we want by executing these machine instructions. Still it is difficult to write instructions step by step in assembler.

To make languages much more friendlier using common day today English words, scientists started incorporating their laboratory lingo into computer languages and created translators to convert these instructions into binary machine language. The translator is called a COMPILER. This is the origin of higher level languages.First language designed with this concept is FORTRAN which stands for formula translation. If you write instructions in FORTRAN, like a=b+c etc, the translator translates these instructions into machine language instructions which the computer executes. The set of instructions we write in the language is called a computer program.The art of writing these instructions is called programming.

Many higher languages were developed after FORTRAN. COBOL for Business people,SPSS for statisticians,GRAPHICS to manipulate pictures on the crt screen etc. As the demand for computer processing grew, the need for people to write instructions in various languages grew. These people are called programmers and the need of programmers became acute as computers became work horses, doing routine work.

Pascal is a language developed to teach students various complexities in programming for the computers. Once you have the basics you can adopt them to any language you choose depending upon your profession.