Matakuliah Tahun Versi : D0524 / Algoritma dan Pemrograman Komputer : 2005 : Pertemuan 11 Array 1 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Menerapkan penggunaan array dalam aplikasi 2 Outline Materi • Introduction to Array • Declaring an Array • Using Arrays 3 Introduction to Array • Up to now, we have dealt with single value data only: e.g. -47.29, True, “Smith” • For each data item we have used a single variable name: e.g. sum, endOfInput, aName • Suppose we need to store the names of everyone in this group. • We could use a unique name for each variable, e.g. name1, name2, name3 … • This would be time-consuming, tedious and unworkable with even larger sets of numbers 4 List Structures • We can treat this kind of data as a group of related items, rather than separate ones • We can reserve a storage area large enough for all the values then assign a name to the area • This data structure may be called a single-level table, a list, a vector or a one-dimensional array 5 Array Subscripts • The group (array or list) is given a name, but the individual items in the group are referred to by their position in the array – using a subscript in brackets • So with a group called students, students(3) is the 4th value (as the numbering starts at 0). • It is similar to a street where all the houses have the same street name, but are distinguished by their house number. 6 Why Use Arrays • Being able to group data into lists or arrays is an advantage – mirrors the real world way of working • much data in business and science is already grouped – allows computer memory to be used most effectively • efficient allocation of storage to variables • faster data access to array elements – allows the writing of efficient program code • write the code to process one value in the array • then make it repeat for all the other values 7 Common Array Processing Task • Array initialisation • Input of data to an array (from keyboard or external data file) • Output of the contents of an array (to screen or external data file) • Searching an array for a specific value • Counting or summing values in the array 8 Array • Imagine we need to store the marks of a group of 10 students studying VB. We decide to store the marks in an array called VBmarks. • If we could visualise the array, it might look like this. VBmarks(0) VBmarks(9) 9 Declaring an Array • To declare an array, we need to: – give the array an identifier – state how many data elements the array should hold – specify the type of data to be held in that array • E.g. – Dim vbMarks (0 To 9) as Integer 10 Initialize the Array • It is good practice to initialise (set to a known value) variables before use. • So we will initialise all the array elements to zero: FOR posisi = 0 TO 9 VBmarks(posisi) = 0 NEXT posisi • You might now visualise the array as this: 0 0 0 0 0 0 0 0 0 0 11 Exercise 1 – Input / Output Data • Now we might input marks from the keyboard into the array FOR posisi = 0 TO 9 keyInput VBmarks(posisi) NEXT posisi 55 22 68 49 81 49 35 56 55 49 and then display them on the screen FOR posisi = 0 TO 9 conOutputput VBmarks(posisi) NEXT posisi 12 Exercise 2 – Find Highest Value in the List • How can we set about finding that out? • Our human eyes can easily pick out the highest value but the computer cannot do that. • It must inspect each value in turn. • It can keep a record of the value that is ‘highest found so far’ and compare that to the next value • if the next value is higher, it becomes ‘highest found so far’ otherwise, the old value is kept • After inspecting each value, it will know the highest value 55 22 68 49 81 49 35 56 55 49 13 Exercise 2 – Solution tertinggi = 0 FOR posisi = 0 TO 9 IF VBmarks(posisi) > tertinggi THEN tertinggi = VBmarks(posisi) ENDIF NEXT posisi conOutput tertinggi Exercise: 1. Amend the design so that the array position of the highest mark is output, not the mark itself 2. Amend the design so that the lowest mark in the set is output. 14 Exercise 3 - Calculate the Average Mark • We often want to know the average (mean) mark. What is the average of this set of marks? • Write the program design to solve this problem 55 22 68 49 81 49 35 56 55 49 15 Exercise 4: Counting array elements Find out the number of students who passed the module (by gaining 40 or more marks). To do this we have to initialise a counter and process through the array, counting the number of marks that are equal to or greater than 40. Write the program design to solve this problem. 55 22 68 49 81 49 35 56 55 49 16 Exercise 5: Using two arrays • Sometimes, information from one array is processed and the results sent to another array or to a data file. • In our example, if a student passes the VB module, by gaining 40 or more marks, then they have earned 20 more credits to their degree credit score. • The students’ associated degree credit scores are held in an array called ‘credits’ in matching positions • When a student passes a module, the value at the same subscript position in ‘credits’ must be incremented by 20 17 Exercise 5 cont’d Develop the pseudocode solution to: set up the values as shown in the credits array (by assignment not by input from the keyboard), input a set of student marks into VBmarks from the keyboard and process through VBmarks, adding 20 to the existing value in the credits array if the value in VBmarks at the same subscript position is >= 40 (student has passed VB) VBmarks 55 22 68 49 81 49 35 56 55 49 credits 40 50 40 30 30 60 70 50 40 30 18