Arrays c4 c5

29
Pascal Programming Language Omar ElSabek & Fayez G hazzawi IT Engineering 3 th year UNKNOWN Department

Transcript of Arrays c4 c5

Page 1: Arrays c4 c5

Pascal Programming Language

Omar ElSabek & Fayez GhazzawiIT Engineering3th year – UNKNOWN Department

Page 2: Arrays c4 c5

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.

Page 3: Arrays c4 c5

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.

Page 4: Arrays c4 c5

Program Lesson1_Program1 (input,output);

Var

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

Begin

…………

End.

Page 5: Arrays c4 c5

Program Lesson1_Program1 (input,output);

Type

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

Var

A , B : myArray;

Begin

…………

End.

Page 6: Arrays c4 c5

Program Lesson1_Program1 (input,output);

Type

myintArray = array [1..9] of integer;

Var

A : myintArray;

mycharArray : array [1..26] of char;

Begin

…………

End.

Page 7: Arrays c4 c5

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

Page 8: Arrays c4 c5

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.

Page 9: Arrays c4 c5

How Can I Fill The Whole Items in my Array ???

Remember The Loop Statements :D

Page 10: Arrays c4 c5

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 ?

Page 11: Arrays c4 c5

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,

Page 12: Arrays c4 c5

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 ?

Page 13: Arrays c4 c5

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,

Page 14: Arrays c4 c5

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.

Page 15: Arrays c4 c5

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.

Page 16: Arrays c4 c5
Page 17: Arrays c4 c5

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;

Page 18: Arrays c4 c5
Page 19: Arrays c4 c5

Program Lesson1_Program1 (input,output);

Var

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

Begin

…………

End.

Page 20: Arrays c4 c5

Program Lesson1_Program1 (input,output);

Type

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

Var

A , B : myMatrix;

Begin

…………

End.

Page 21: Arrays c4 c5

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

Page 22: Arrays c4 c5

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.

Page 23: Arrays c4 c5

In Order To Fill The Whole Matrix

As Same As ArraysThe Loop Statements are the BEST :D

Page 24: Arrays c4 c5

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.

Page 25: Arrays c4 c5

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

Page 26: Arrays c4 c5

11111

22222

33333

Page 27: Arrays c4 c5

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.

Page 28: Arrays c4 c5

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.

Page 29: Arrays c4 c5