Computer Programming – 1

Computer Programming I
-7Emel KURUOĞLU KANDEMİR, Ph.D.
Alper Vahaplar, Ph.D.
Dokuz Eylul University, Faculty of Sciences
Department of Computer Science
[email protected]
Computer Programming - 1
Computer Programming - 1
[email protected]
1
Alper VAHAPLAR
2
1
Arrays in Pascal
 An Array is a powerful data structure that stores variable
data having the same data type.
 Arrays are variables that are made up of many variables
of the same data type but have only one name.
Computer Programming - 1
Alper VAHAPLAR
3
Array A
Exercises
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Computer Programming - 1
Alper VAHAPLAR
4
2
Arrays in Pascal
 <arrayName> : Array[start..end] of <Data Type>;
 var
a: array[1..5] of Integer;
begin
a[1] := 12;
a[2] := 23;
a[3] := 34;
a[4] := 45;
a[5] := 56;
end.
Computer Programming - 1
Alper VAHAPLAR
5
Arrays in Pascal
 Ex:
var sayi:array[1..5] of integer;
i:integer;
begin
 sayi[1]
for i:=1 to 5 do
sayi[2]
sayi[i] = i*i;
sayi[3]
end.
sayi[4]
sayi[5]
Computer Programming - 1
Alper VAHAPLAR
=
=
=
=
=
1
4
9
16
25
6
3
Arrays in Pascal
 Ex:
uses dos;
var gunler : array [1..7] of string;
gun, ay, yil, hgunu: word;
begin
getdate(yil, ay, gun, hgunu);
writeln(gun, ‘/’,ay, ‘/’,yil, ‘-’, hgunu);
gunler[1]:=‘Pazartesi’;
gunler [2]:=‘Salı’;
gunler [3]:=‘Çarşamba’;
gunler [4]:=‘Perşembe’;
gunler [5]:=‘Cuma’;
gunler [6]:=‘Cumartesi’;
gunler [7]:=‘Pazar’;
writeln(gunler[hgunu]);
readln;
end.
Computer Programming - 1
Alper VAHAPLAR
7
Arrays in Pascal
 Ex: Find the average and standart deviation of grades for
a class of 5 students.
 Print the name of the student with the highest and
smallest grades.
Computer Programming - 1
Alper VAHAPLAR
8
4
Exercises
 Write a program to display the frequency table for the
given numbers until the user enters 0.
Var adet:array[1..9] of integer;
sayi:integer;
begin
write(‘Bir sayı giriniz ‘);readln(sayi);
while sayi<>0 do begin
inc(adet[sayi]);
end;
for i:=1 to 9 do writeln(i, ‘ – ‘, adet[i]);
end.
Computer Programming - 1
Alper VAHAPLAR
9
Exercise
 Fibonacci :
 First two terms are 0 and 1
 The next term is the sum of previous two terms.
 Question:
 Find the first 24 terms of Fibonacci Series
 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,
377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711,
28657
Computer Programming - 1
Alper VAHAPLAR
10
5
Golden Ratio / Golden Section
 Divine Proportion
 Phidias (490-430 BC)
 Plato (427-347 BC)
 Euclid (325-265 BC)
 Fibonacci (1170-1250)
Computer Programming - 1
Alper VAHAPLAR
11
Golden Ratio / Golden Section
Computer Programming - 1
Alper VAHAPLAR
12
6
Golden Ratio - Geometry
Computer Programming - 1
Alper VAHAPLAR
13
Golden Ration - Life
Computer Programming - 1
Alper VAHAPLAR
14
7
Computer Programming - 1
Alper VAHAPLAR
15
Computer Programming - 1
Alper VAHAPLAR
16
8
Computer Programming - 1
Alper VAHAPLAR
17
Exercise
 Fibonacci :
 First two terms are 0 and 1
 The next term is the sum of previous two terms.
 Question:
 Find the first 24 terms of Fibonacci Series
 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,
377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711,
28657
 Find the ratio of fib(n)/fib(n-1) for the first 24 terms.
Computer Programming - 1
Alper VAHAPLAR
18
9
Strings as Arrays
 Each string variable is an array of characters.
 Ex:
var s:string;
i:integer;
begin
write(‘Bir metin giriniz’); readln(s);
for i:=1 to length(s) do writeln(s[i]);
end.
Computer Programming - 1
Alper VAHAPLAR
19
Strings as Arrays
 Reverse a given string
 Ex:
var s:string;
i:integer;
begin
write(‘Write a word:’); readln(s);
for i:=length(s) downto 1 do write(s[i]);
readln;
end.
Computer Programming - 1
Alper VAHAPLAR
20
10
Strings as Arrays
 Count the number of ‘a’ in a given string
 Ex:
var s:string;
adet,i:integer;
begin
write(‘Write a word:’); readln(s);
for i:=1 to length(s) do if s[i]=‘a’ then inc(adet);
writeln(‘Number of a = ‘, adet);
readln;
end.
Computer Programming - 1
Alper VAHAPLAR
21
Exercises
 Array indices can be characters.
 Ex:
var s: array[‘a’..’z’] of integer;
x: char;
begin
s[‘a’]:=16;
s[‘b’]:=25;
s[‘c’]:=9;
for x:= ‘a’ to ‘c’ do writeln(s[x]);
end.
Computer Programming - 1
Alper VAHAPLAR
22
11
Exercises
 Count the frequencies of each letter in a given string.
Display the frequencies which are greater than 0.
var s:string;
say:array[‘a’..’z’] of integer;
i: integer;
a: char;
begin
write(‘Cümleyi giriniz ‘);readln(s);
for i:=1 to length(s) do inc(say[st[i]];
f0r a:=‘a’ to ‘z’ do if say[a]>0 then writeln(a,’-’,say[a]);
end.
Computer Programming - 1
Alper VAHAPLAR
23
Exercise
 Create an array of 50 random integers between 0 and 9.
 Find the frequencies of these numbers;
 Draw the dot-plot diagram.
 Sort these numbers. (Selection Sort)
Computer Programming - 1
Alper VAHAPLAR
24
12
Arrays – Sorting (Selection Sort)
uses crt;
var i,j,t, yer, enk:integer;
s:array[1..10] of integer;
begin
randomize;
for i:=1 to 10 do begin
s[i]:=random(100);
writeln(s[i]);
end;
Computer Programming - 1
for i:=1 to 9 do begin
enk:=s[i]; yer:=i;
for j:=i+1 to 10 do begin
if s[j]<enk then begin
enk:=s[j];
yer:=j;
end;
end;
if yer<>i then begin
s[yer]:=s[i];
s[i]:= enk;
end;
end;
writeln(‘Sorted Array:’);
for i:=1 to 10 do writeln(s[i]:2);
readln;
end.
Alper VAHAPLAR
25
Arrays – Sorting (Bubble Sort)
uses crt;
var s:array[1..10] of integer;
var i,j,t:integer;
begin
clrscr;
randomize;
for i:=1 to 10 do begin
s[i]:=random(100);
writeln(i,'-‘,s[i]);
end;
Computer Programming - 1
Alper VAHAPLAR
for i:=1 to 9 do begin
for j:=10 downto i+1 do begin
if s[j]<s[j-1] then begin
t:=s[j];
s[j]:=s[j-1];
s[j-1]:=t;
end;
end;
end;
writeln('Sorted Array:');
for i:=1 to 10 do writeln(s[i]);
readln;
end.
26
13
Arrays – 2 dimensional
 Arrays :An array is a group of elements , all of the
same type and indexed by a set of values. An array
is similar to a vector in mathematics.
 Vector and Matrix Representations Vectors and
Matrices are naturally represented by 1- and 2dimensional arrays in Pascal.
Var
Myvec : array[1..10] of REAL;
MyMatrix: array[1..10,1..5] of integer;
Computer Programming - 1
Alper VAHAPLAR
27
2-d Arrays–Matrix of Random numbers
uses crt;
var
m:array[1..3,1..4] of integer;
i,j:smallint;
begin
clrscr;
for i:=1 to 3 do begin
for j:=1 to 4 do begin
m[i,j]:=random(100);
write(m[i,j]:4);
end;
writeln;
end;
readln;
end.
Computer Programming - 1
Alper VAHAPLAR
28
14
Arrays – 2 dimensional
 Use a 2-dimensional array to build a lottery coupon of 6/49.
uses crt;
var loto: array[1..8,1..6] of integer;
i,j:byte;
begin
clrscr;
randomize;
for i:=1 to 8 do begin
for j:=1 to 6 do begin
loto[i,j]:=random(49)+1;
write(loto[i,j]:5);
end;
writeln;
end;
readln;
end.
Computer Programming - 1
Alper VAHAPLAR
29
2-d Arrays
Construct a two dimensional array for a matrix and compute sum of
the matrix elements
uses crt;
var
for i:=1 to 3 do
m:array[1..3 ,1..4]of integer;
for j:=1 to 4 do begin
i,j:smallint;
t:=t+m[i,j];
t:integer;
end;
begin
write('Sum of the matrix elements=',t);
clrscr;
readln;
t:=0;
end.
for i:=1 to 3 do
for j:=1 to 4 do begin
write('Write (',i,',',j,'). element=');
readln(m[i,j]);
end;
Computer Programming - 1
Alper VAHAPLAR
30
15
Ex- Write a pascal program to
compute the sum of each rows
uses crt;
var
m:array[1..3,1..4]of integer;
srows:array[1..3] of integer;
i,j:smallint;
begin
… {read or generate the matrix m}
for i:=1 to 3 do begin
for j:=1 to 4 do srows[i]:=srows[i]+m[i,j];
Writeln('Sum of the ',i,'.row=',srows[i]);
end;
readln;
end.
Computer Programming - 1
Alper VAHAPLAR
31
Exercises
 Write a pascal program to compute the sum of each
columns
 Write a pascal program to compute the mean and
std.deviation value of the each columns.
 Build a 2 dimensional array for 5 students containing
their midterm1, midterm2, final and average scores.
The user will input the exam scores of each student.
 Calculate the average score for each student according to
the following formula:
AvSc = Midterm1*25% + Midterm2*25% + Final*50%
 Find the maximum score of each exam.
Computer Programming - 1
Alper VAHAPLAR
32
16