KEMBAR78
Arrays in Pascal | PDF | Data Type | Computer Engineering
0% found this document useful (0 votes)
3 views29 pages

Arrays in Pascal

The document provides an overview of arrays and two-dimensional arrays (matrices) in the Pascal programming language, explaining their structure, initialization, and methods for filling them with data. It includes examples of declaring arrays, filling them at compile time and run time, and using loops to populate their elements. Additionally, it discusses potential runtime errors and demonstrates how to manipulate and display data in arrays and matrices.

Uploaded by

Drexla Ree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Arrays in Pascal

The document provides an overview of arrays and two-dimensional arrays (matrices) in the Pascal programming language, explaining their structure, initialization, and methods for filling them with data. It includes examples of declaring arrays, filling them at compile time and run time, and using loops to populate their elements. Additionally, it discusses potential runtime errors and demonstrates how to manipulate and display data in arrays and matrices.

Uploaded by

Drexla Ree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Pascal Programming Language

Omar ElSabek & Fayez Ghazzawi


IT Engineering
3th year – UNKNOWN Department
Arrays

The Array is a powerful data structure that stores variable data having the
same data type. It is just like a small fixed number of boxes linked together
one after the other storing things that are related to each other. An array is
said to be a static data structure because, once declared, its original size that is
specified by the programmer will remain the same throughout the whole
program and cannot be changed.
so the Array

is a locations collection for data storage, every


location named “Item” and every item has a specific
value ,and the array has a specific type.
Program Lesson1_Program1 (input,output);
Var

myArray : array [x..y] of type;

Begin

…………

End.
Program Lesson1_Program1 (input,output);
Type

myArray = array [x..y] of type;

Var
A , B : myArray;
Begin

…………

End.
Program Lesson1_Program1 (input,output);
Type
myintArray = array [1..9] of integer;
Var
A : myintArray;
mycharArray : array [1..26] of char;
Begin

…………

End.
Filling the Array

After we define our array ..


Now we’re filling it ^_^
We can fill the array in two ways:
• Initialize in Compile Time
• Read from the keyboard in Run Time
Program Lesson1_Program1 (input,output);
Var
A : array [1..10] of integer ;
Begin
A[1] := 9; A[2] := 8; A[3] := 7; ……
End.

Program Lesson1_Program1 (input,output);


Var
A : array [1..10] of integer ;
Begin
read(A[1]); read(A[2]); read(A[3]); ……
End.
How Can I Fill The Whole
Items in my Array ???
Remember The Loop Statements :D
Program Lesson1_Program1 (input,output);
Var
myintArray : array [1..9] of integer;
i : integer;
Begin
for i:=0 to 9 do
myintArray[i] := i+10;
for i:=0 to 9 do
write (myintArray[i] , “ , “);
End.

Run Time Error … why ?


Program Lesson1_Program1 (input,output);
Var
myintArray : array [-10..-1] of integer;
i : integer;
Begin
for i:=-10 to -1 do
myintArray[i] := i+10;
for i:=-10 to -1 do
write (myintArray[i] , “,“);
End.

0,1,2,3,4,5,6,7,8,9,
Program Lesson1_Program1 (input,output);
Var
myintArray : array [-10..-1] of integer;
i : integer;
Begin
for i:=1 to 10 do
myintArray[i] := i+10;
for i:=1 to 10 do
write (myintArray[i] , “,“);
End.

Run Time Error … why ?


Program Lesson1_Program1 (input,output);
Var
myintArray : array [1..10] of integer;
i : integer;
Begin
for i:=1 to 3 do
myintArray[i] := i;
for i:=1 to 10 do
write (myintArray[i] , ‘,’);
End.

1,2,3,0,0,0,0,0,0,0,
Program Lesson1_Program1 (input,output);
Const n = 10
Var
myintArray : array [1.. n] of integer;
x,i : integer;
b : boolean;
Begin
readln(x);
b := false;
for i:=1 to n do
if (myintArray[i] = x) then
b := true;
writeln(b);
End.
Program Lesson1_Program1 (input,output);
Const n = 10
Var
myintArray : array [1.. n] of integer;
x,i : integer; b : boolean;
Begin
for i:=1 to n do
for j:= i+1 to n do
if (myintArray[i] > myintArray[j]) then
begin
x := myintArray[i];
myintArray[i] := myintArray[j];
myintArray[j] := x;
end;
for i:=1 to 10 do
write (myintArray[i] , ‘,’);
End.
Two Dimensions Array (Matrix)

The Two Dimensions Array (Matrix)


is a Square Array that has rows
and columns .

my2DArray : Array[1..5 , 1..5] of integer;


Program Lesson1_Program1 (input,output);
Var

myMatrix : array [x1..x2,y1..y2] of type;

Begin

…………

End.
Program Lesson1_Program1 (input,output);
Type

myMatrix = array [x1..x2,y1..y2] of type;

Var
A , B : myMatrix;
Begin

…………

End.
Filling the Matrix

After we define our matrix ..


Now we’re filling it ^_^
We can fill the array in two ways:
• Initialize in Compile Time
• Read from the keyboard in Run Time
Program Lesson1_Program1 (input,output);
Var
A : array [1..10,1..10] of integer ;
Begin
A[1,1] := 9; A[1,2] := 8; A[1,3] := 7; ……
A[2,1] := 6; A[2,2] := 5; A[2,3] := 4; ……
……
End.

Program Lesson1_Program1 (input,output);


Var
A : array [1..10] of integer ;
Begin
read(A[1,1]); read(A[1,2]); read(A[1,3]); ……
read(A[2,1]); read(A[2,2]); read(A[2,3]); ……
……
End.
In Order To Fill The Whole
Matrix
As Same As Arrays
The Loop Statements are the BEST :D
Program Lesson1_Program1 (input,output);
Var
a: array [1..3, 1..3] of integer;
i,j : integer;
begin
for i:=1 to 3 do
begin
for j:=1 to 3 do
a[i,j]:= i;
end;
End.
Program Lesson1_Program1 (input,output);
Var
a: array [1..3, 1..5] of integer;
i,j : integer;
begin
for i:=1 to 3 do
begin
for j:=1 to 5 do
a[i,j]:= i;
end;
End.

What is the output ? :D


1 1 1 1 1

2 2 2 2 2

3 3 3 3 3
Program Lesson1_Program1 (input,output);
Var
a: array [1..3, 1..5] of integer;
i,j : integer;
begin
for i:=1 to 3 do
for j:=1 to 5 do
a[i,j]:= i;

for i:=1 to 3 do
begin
for j:=1 to 5 do
writeln(a[i,j] ,’ ’);
writeln;
end;
End.
Program Lesson1_Program1 (input,output);
Const n = 100;
Var
myintMatrix : array [1..n,1..n] of integer;
x, i,j : integer;
Begin
readln(x);
for i:=1 to x do
for j:=1 to x do
readln (myintMatrix[i,j]);

for i:=1 to x do
writeln (myintMatrix[i,i]);

End.

You might also like