Pemodelan untuk Komputasi : Multidimensional Array dan Contoh Soal
Introduction
It often happens that we want to store collections of values that have multidimensional layout. When it happens, we need a storage which can provide more than just predecessor-successor relatioship between its data.
Case Study
A teacher wants to calculate the average score of the homeworks that he has given to his students. There are 10 students and there are 5 homeworks.
You will need to store 5 x 10 data.
Multi-dimensional Array
Multidimensional arrays with two dimensions are often used to represent tables of values with data arranged in rows and columns. To identify a particular table element, you specify two indices. By convention, the first identifies the element’s row and the second its column.
Declaration
Declaration of two dimentional array is similar to declaration of one dimentional array. You need to mention the array type, the length of row, and the length of column.
Example:
- daftarNilai yaitu sebuah array dua dimensi dengan ukuran 10 x 5
Accessing element
Similar to one-dimensional array, each element in multi-dimensional array is also accessed using an index.
Each elements of a multi-dimensional array can be processed as a single variable. Example: daftarNilai[4][3] -> score of students number 4 for homework number 3
Sequential Access
A nested loop is used to perform sequential access to all elements in a multi-dimensional array.
Exercise 1
Di dalam komputer, gambar disimpan dalam matriks 2 dimensi. Tiap elemen matriks menyatakan nilai dari piksel pada baris ke-i dan kolom ke-j. Gambar hitam putih, hanya akan mempunyai nilai 0 atau 1 saja di tiap baris dan kolom matriks. 0 untuk piksel hitam dan 1 untuk piksel putih. Buatlah algoritma untuk meninvers gambar hitam putih ( piksel hitam menjadi putih, dan sebaliknya )
Exercise 2
Terdapat m x n lampu LED yang disusun menjadi m baris dan n buah kolom. Buatlah algoritma untuk:
- Menyalakan semua lampu
- Mematikan semua lampu
- Menyalakan hanya lampu di diagonal utama saja
- Menyalakan hanya lampu di kolom genap saja
- Menyalakan lampu di segitiga bawah saja
Exercise 3
Sebuah gambar hitam putih pada kertas transparan sanggup ditumpuk dengan gambar lainnya. Buatlah algoritma untuk memilih hasil penumpukan dua buah gambar hitam putih yang ukurannya sama !
No comments:
Post a Comment