Thursday, January 26, 2017

Lecture 11 - Pascal Programming (Part V)



Array Processing
Array is a complex data structure which consist of number of elements of the same data type. These elements are located in consecutive bytes in the random access memory. Since we have a common name to access all elements of the array the position of the element should be given to access the array element. This is called the index or subscript. These arrays can be single dimensional or multi-dimensional.

Single Dimensional Arrays

VAR
Table : ARRAY[1..5] of integer;



The above array consist of 5 integer elements and the subscript of the elements are integer numbers from 1 to 5.

Table[2] := 10; { Store value 10 on 2nd element }
Table[4] := Table[2] + 5; { Store value 15 on 4th element }

TYPE
Table Type : ARRAY['B'..'E'] of real;
VAR
Num: TableType;



Above array is somewhat unusual and it shown the flexibility of array index in Pascal language. The array index can be :

  • Array index can be in any range, need not always start from 1.
  • Even characters can be used because it is also an ordinal type.
Following program uses an integer array of 5 elements and display the integers entered from the keyboard in reverse order of entry.

PROGRAM Example9;
VAR
Table . Array[1..Max] of integer;
Index : integer;

BEGIN
Writeln('Enter Integers separated by spaces');
FOR index:= 1 to Max DO
Read( Table[index] );

Writeln('The integers in reverse order are :'),
FOR index:= Max DOWNTO 1 DO
Write( Table[index]:4 );
Writeln;
END.


Two-dimensional Arrays
In Pascal we can define arrays which can spread in more than one direction. The most simplest of this is the two-dimensional array. Consider the following example which defines 2D char array which is used for a simple encryption process in the subsequent program.

VAR
Table : Array[1 ..4,1..5] of char,



Following program uses the shown 2D char array to input a string of 20 characters and encrypt it with simple transformation.

PROGRAM Examplell;
VAR
Table : Array[1 ..4,1..51of integer;
Rindex, Cindex : integer;

BEGIN
Writeln('Enter a sentence up to 20 character : ');
FOR Rindex:= 1 TO 4 DO
FOR Cindex:= 1 TO 5 DO
Read( Table[Rindex,Cindex] );

Write('Encrypted string is : ')
FOR Cindex:= 1 TO 5 DO
FOR Rindex.= 1 TO 4 DO
Write( Table[Rindex,Cindex] );
Writeln;

Write('Plaintext string is : ')
FOR Rindex:= 1 TO 4 DO
FOR Cindex:= 1 TO 5 DO
Write( Table[Rindex,Cindex] );
Writeln;
END.


Records

This is the other complex type which can consist of many fields. But unlike arrays all fields are not of the same type and therefore record can be defined as given below.

TYPE
StudentRecord = RECORD
ld : integer;
Name: String;
Avg : Real;
END;
VAR
MyData : StudentRecord;

You can have nested records by defining any field of the record also as another record type. For example DOB can be defined as DateType which consist of the fields DD, MM and YYYY


Following program read Student ID, Name and Avg into a student record and display them on the screen.

PROGRAM Examplel2;
TYPE
StudentRecord = RECORD
ld : integer;
Name: String;
Avg : Real;
END;
VAR
MyData : StudentRecord;

BEGIN
Write('Enter Student ld . ');
Readln( MyData.td );
Write('Enter Student Name : ,);
Readln( MyData.Na*"' );
Write('Enter Student Avg ; ,);
Readln( MyData.Avg');

Writeln('ld -',MyData.td );
Writeln('Name -',MyData.Name );
Writeln('Avg -', MyData.Avg );
END.


No comments:

Post a Comment

Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.