0% found this document useful (0 votes)
30 views16 pages

15 Record

1. The document discusses one-dimensional and two-dimensional arrays in Pascal. It provides the syntax for declaring arrays, examples of one-dimensional array programs to output values, and an example of a two-dimensional array program to store and output values. 2. Examples show how to declare array types, initialize values, use for loops to iterate over arrays, and print outputs. Formulas for calculating array indices and accessing elements are demonstrated. 3. Two-dimensional arrays are explained as having two indices to access elements, similar to a table with rows and columns. An example program initializes and prints values from a 2D integer array.

Uploaded by

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

15 Record

1. The document discusses one-dimensional and two-dimensional arrays in Pascal. It provides the syntax for declaring arrays, examples of one-dimensional array programs to output values, and an example of a two-dimensional array program to store and output values. 2. Examples show how to declare array types, initialize values, use for loops to iterate over arrays, and print outputs. Formulas for calculating array indices and accessing elements are demonstrated. 3. Two-dimensional arrays are explained as having two indices to access elements, similar to a table with rows and columns. An example program initializes and prints values from a 2D integer array.

Uploaded by

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

Pertemuan 4

Array
(Array Dimensi 1)

1. Larik Dimensi Satu


Sintak:

peubah := array[sub-range] of tipe-data


pengubah : suatu pengubah yang disebut pengubah kendali, dapat
berupa pengenal dari tipe integer, char, boolean atau
tipe terbilang.
Sub-range : nilai dengan rentang: 1..10, 1..100, a.. c, dan
sebagainya.
Tipe-data : tipe data, misalnya: integer, real, word, dsb

Program:
Program larik1;
Uses wincrt;
var
x: array[1..10] of integer;
i:integer;
Begin
for i:=1 to 5 do
begin
x[i]:=i;
writeln(x[i]:8:2);
end;
end.

Output:
####1.00
####2.00
####3.00
####4.00
####5.00

Program larik2;
Uses wincrt;
Const
maks = 10;
var
x,y: array[1..maks] of integer;
i:integer;
Begin
for i:=1 to 5 do
begin
x[i]:=i;
y[i]:=i*i;
writeln(x[i]:8:2,y[i]);
end;
end.

1.00
2.00
3.00
4.00
5.00

1.00
4.00
9.00
16.00
25.00

Const
maks = 10;
Type
larik = array[1..maks] of
Var
x,y : larik;

Program larik2;
Uses wincrt;
Const
maks = 10;
var
x,y: array[1..maks] of integer;
i:integer;
Begin
for i:=1 to 5 do
begin
x[i]:=i;
y[i]:=i*i;
z[i]:=y[i]-x[i];
writeln(x[i]:8:2,y[i]:8:2,z[i]:8:2);
end;
end.

1.00
2.00
3.00
4.00
5.00

1.00
4.00
9.00
16.00
25.00

0.00
2.00
6.00
12.00
20.00

Program pengulangan1;
Uses wincrt;
type
larik = array['a'..'z'] of char;
var
x: larik;
i: char;
Begin
for i:='a' to e' do
begin
x[i]:=i;
writeln(x[i]:8);
end;
end.

a
b
c
d
e

Program jumlah_data;
Uses wincrt;
const
maks =100;
type
larik = array[1..maks] of real;
var
x
: larik;
i
:integer;
sum :real;
Begin
x[1]:=3;x[2]:=4;x[3]:=6;x[4]:=-2;x[5]:=0;
sum:=0;
for i:=1 to 5 do
begin
sum:=sum+x[i];
writeln(x[i]:8:2);
end;
writeln('jumlah semua data = ',sum:8:2);
end.
2.00
4.00
6.00
-2.00
0.00
Jumlah semua data =

11.00

Diketahui data: 3, 4, 5, 8, 6, 8, 7, 7, 6 dan 9, jika rataan xbar = 7, buatlah variansinya.


Program variansi;
Uses wincrt;
const
maks =100;
type
larik = array[1..maks] of real;
var
x,y
: larik;
i,n
: integer;
sum,xbar,variansi : real;
Begin
x[1]:=3;x[2]:=4;x[3]:=5;x[4]:=8;x[5]:=6;
x[6]:=8;x[7]:=7;x[8]:=7;x[9]:=7;x[10]:=9;
n:=10;
xbar:=7;
sum:=0;
for i:=1 to n do
begin
y[i]:=sqr(x[i]-xbar);
sum:=sum + y[i];
writeln(x[i]:8:2,y[i]:8:2);
end;
variansi := sum/n; writeln;
writeln('variansi = ',variansi:8:2);
end.

3.00
4.00
5.00
8.00
6.00
8.00
7.00
7.00
6.00
9.00
variansi =

16.00
9.00
4.00
1.00
1.00
1.00
0.00
0.00
1.00
4.00
3.60

2. Larik Dimensi Dua


Sintak:

peubah := array[sub-range] of tipe-data


pengubah : suatu pengubah yang disebut pengubah kendali, dapat
berupa pengenal dari tipe integer, char, boolean atau
tipe terbilang.
Sub-range : nilai dengan rentang: 1..10, 1..100, a.. g, dan
sebagainya.
Tipe-data : tipe data, misalnya: integer, real, word, dsb
1

x[i,j]

1
2
3

baris (i)

Kolom (j)

Program larik2D;
Uses wincrt;
var
x: array[1..10,1..10] of integer;
i,j:integer;

begin
begin
for j:=1 to 2 do
write(x[i,j]:8);
end;
writeln;
end;

Begin
x[1,1]:=1;x[1,2]:=2;x[2,1]:=3;x[2,2]:=4;
for i:=1 to 2 do
begin
for j:=1 to 2 do
writeln(x[i,j]:8);
end;

1
3

end.
write(x[i,j]:8)
1
2
3
4

2
4

You might also like