Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

ARRAY RUBY. PENDAHULUAN Ruby's arrays are untyped and mutable. The elements of an array need not all be of the same class, and they can be changed at.

Presentasi serupa


Presentasi berjudul: "ARRAY RUBY. PENDAHULUAN Ruby's arrays are untyped and mutable. The elements of an array need not all be of the same class, and they can be changed at."— Transcript presentasi:

1 ARRAY RUBY

2 PENDAHULUAN Ruby's arrays are untyped and mutable. The elements of an array need not all be of the same class, and they can be changed at any time. Furthermore, arrays are dynamically resizeable; you can append elements to them and they grow as needed. If you assign a value to an element beyond the end of the array, the array is automatically extended with nil elements. (It is an error, however, to assign a value to an element before the beginning of an array.)

3 DEKLARASI nama_variable=[nil]; atau nama_variabel=Array.new(ukuran, obj); Contoh: A=[nil]; B=Array.new(10,nil);

4 ARRAY LITERAL # An array that holds three Fixnum objects [1, 2, 3] # An array of two ranges; trailing commas are allowed [-10...0, 0..10,] # An array of nested arrays [[1,2],[3,4],[5]] # Array elements can be arbitrary expressions [x+y, x-y, x*y] # The empty array has size 0 []

5 SPECIAL SYNTAKS words = %w[this is a test] # Same as: ['this', 'is', 'a', 'test'] open = %w| ( [ { < | # Same as: ['(', '[', '{', '<'] white = %w(\s \t \r \n) # Same as: ["\s", "\t", "\r", "\n"]

6 MENGAKSES ISI ARRAY a = [0, 1, 4, 9, 16] # Array holds the squares of the indexes a[0] # First element is 0 a[-1] # Last element is 16 a[-2] # Second to last element is 9 a[a.size-1] # Another way to query the last element a[-a.size] # Another way to query the first element a[8] # Querying beyond the end returns nil a[-8] # Querying before the start returns nil, too

7 CONTOH MENGISI ARRAY 1 DIMENSI YANG MEMILIKI 5 ELEMEN: A=[nil], temp=0, i=0; for i in 1..5 # loop untuk mengisi array printf ("Isi elemen array A ke-"+i.to_s+" : "); A[i]=gets.to_i; #mengubah semua input menjadi nilai integer end for i in 1..5 #loop untuk menampilkan isi array printf ("Isi elemen array A ke-"+i.to_s+" : "+A[i].to_s+"\n"); end

8 ARRAY 2D Deklarasi: nama_variabel=Array.new(ukuran, obj); Contoh: Angka=Array.new(3,Array.new(5,nil)); kolom 0kolom 1kolom 2 Kolom 3 kolom 4 baris 0 [0][0][0][1][0][2][0][3][0][4] baris 1 [1][0][1][1][1][2][1][3][1][4] baris 2 [2][0][2][1][2][2][2][3][2][4]

9 ARRAY ND # membentuk struktur data 3 dimensi. Sumbu x, y, z Ruang=Array.new(5,Array.New(5,Array.new(5,nil)); # membentuk struktur data 6 dimensi. # Sulit sekali memvisualisasikannya frase= Array.new(3,Array.New(3,Array.new(3, Array.new(5,Array.New(5,Array.new(7,nil))))

10 LATIHAN Cari nilai statistik dari data array number Max Min Buat program untuk mengisi dan menampilkan array 2D (gunakan perulangan)

11 TUGAS ARRAY+FUNGSI MOD 6 1.Buatlah program untuk menghitung fungsi statistik (input berupa array yg sudah terinisialisasi bisa pada saat design time maupun otomatis) yang mempunyai fungsi untuk: 1.Rata-rata 2.Menghitung modus (nilai yang paling sering muncul).

12 TUGAS 2. Buat programuntuk operasi matriks 2D yang berisi angka. 1.penjumlahan 2.pengurangan 3.perkalian titik 4.perkalian silang


Download ppt "ARRAY RUBY. PENDAHULUAN Ruby's arrays are untyped and mutable. The elements of an array need not all be of the same class, and they can be changed at."

Presentasi serupa


Iklan oleh Google