Tuesday, May 3, 2011

Contoh Program Array

Contoh Program  :

Program Contoh_Array_Input;
Uses Crt;
Var
           Bilangan : array[1..50] of Integer;
              Begin
               ClrScr;
                   Bilangan[1]:=3;
                   Bilangan[2]:=29;
                   Bilangan[3]:=30;
                   Bilangan[4]:=31;
                   Bilangan[5]:=23;
                 Writeln('nilai varibel bilangan ke 3 =',Bilangan[3]);
                Readln;
               End.

Array juga dapat dideklarasikan bersama dengan tipe yang beragam seperti contoh dibawah
ini :

Program Contoh_Deklarasi_Array_Beragam;
Uses Crt;
Var
           NPM      : array[1..20] of string[10];
           Nama      : array[1..20] of string[25];
           Nilai        : array[1..20] of real;
           Umur      : array[1..20] of byte;
           banyak,i : integer;
Begin
        ClrScr;
           Write('Isi berapa data array yang diperlukan :');Readln(banyak);
            For i := 1 to banyak Do
             Begin
             Write('NPM  =');Readln(NPM[i]);
             Write('Nama =');readln(Nama[i]);
             Write('Nilai=');readln(Nilai[i]);
             Write('Umur =');readln(Umur[i]);
             End;
            {cetak varibel array}
               Writeln('NPM             NAMA            NILAI        UMUR ');
            For i:= 1 to banyak Do
                Begin
                     Writeln(Npm[i]:10,Nama[i]:25,Nilai[i]:3:2,' ',Umur[i]:3);
                End;
             Readln;
  End.

0 comments:

Post a Comment