Presentation is loading. Please wait.

Presentation is loading. Please wait.

Siti Nurbaya Ismail Senior Lecturer

Similar presentations


Presentation on theme: "Siti Nurbaya Ismail Senior Lecturer"— Presentation transcript:

1 Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): (b):

2 Array Differentiate Between Simple Variable And Array
Understand How Computer Executes Algorithm Contains Array Construct Algorithm Using Array (Maximum, Minimum, Count, Average, Summation) Apply Sorting And Searching Problem

3 Introduction So far, we solve problems using simple variables.
A simple variable can only store one value at one time. When we assign a new value to the variable, the existing value will be replaced by the new value. Start variable = 10 Display “ initial value = “ , variable variable = variable + 5 Display “ new value = “ , variable End output

4 variable num will store the last number entered by the user (50)
variable num will store the last number entered by the user (50). The first four numbers entered are gone. If we want to keep the previous data (the first four entered data), we have to use five variables, instead of only one variable. Simple variable  One variable stores one data. Sometimes, you need to deal with a whole bundle of data, all at once.  Introduction input Start sum  0 Display “ Enter 5 numbers : “ Set counter to 1 While counter ≤ 5 Read num sum  sum + num Add 1 to counter endWhile average  sum / 5 Display “ the average = “ , average Display newline Display “The numbers entered are : “ Display num , “ “ End 10, 23, 7, 10, 50 tracing table num sum average 10 23 33 7 40 50 100 output

5 What is Array? An array lets you manage a whole train of data
Sometimes, you want to refer to a whole bunch of data all at once. For that, you need a new type of variable: the array. Think of an array as a data train. Each car in the train is called an array element and can store a single piece of data. If you want to store a number in one element and a string in another, you can.

6 What is Array? You might think that as you are storing all of that data in an array, you still might need variables for each of the items it stores. But this is not the case. An array is itself just another variable, and you can give it its own variable name:

7 What is Array? Even though an array contains a whole bunch of data items, the array itself is a single variable, which just so happens to contain a collection of data. Once your data is in an array, you can treat the array just like any other variable.

8 Array array is one of a complex data
array is a variable that can store more than one data or value at one time because it is allocated with many memory locations as indicated by the size of the array the data type for each value must be the same the number of data can be stored in an array is fix Array size is 10

9 When to use array? We store the data in the array when:
There are many data with the same data type to be used repeatedly in an algorithm at different places. There are many data with the same data type that must be retained until the end of the algorithm.

10 Manipulate An Array Two types of data;
primitive data  data cannot be broken further into smaller data complex data  data can be broken further.  an array is one example of a complex data  an array stores a set of data with the same data type that can be broken into a few data from a set of data  example:  declare a string as coursecode = “love”  initialize the string coursecode with values love  the string coursecode can be broken into four single characters as l, o, v and e coursecode = l o v e

11 Manipulate An Array When an array stores 10 data or value, the array can be broken into 10 different data or values. element (at index 8) first index 1 2 3 4 5 6 7 8 9 cat 10 index element array size is 10 Element Index / Subscript First element Second element 1 Third element 2 Fifth element 3

12 Manipulate An Array index element array size is N
1 2 3 4 5 N-1 6 N index element array size is N Note : When the size of array is N, the indexes are 0, 1, 2, …… N-1. We manipulate an array just like we manipulate a variable. We can perform input, process and output to an array.

13 Declare An Array declare the array examples the name of the array
the size of the array examples name-of-array [size-of-array] num[5] mark[10] salary[30] name[30]

14 Declare and Initialize The Array
We can declare and initialize the array at the same time as in the following ways: name-of-array [size-of-array] = {values} or name-of-array [ ] = {values}

15 Declare and Initialize The Array
the size of the array grade is 7 even though the size of the array were not mentioned in the square bracket [ ].

16 Declare and Initialize The Array
the size of the array grade is 7 even though the size of the array were not mentioned in the square bracket [ ].

17 INCORRECT to declare an array with a size less than the number of initialized values.
When the size of an array is greater than the number of initialized values, the system will store all the initialized values in the array and the rest of the array will be filled with zero(0) when the type of the array is numeric OR null character (■) when the type of the array is character.

18 Set Data Into Array We set data or store value(s) into the array using input statement or assignment statement. We can assign data into a certain element or give values to an entire array or a portion of the array.

19 Set Data Into An Element of Array
If we want to set a data or a value into an element, use the following way: name-of-array [which-element] = data // using assignment statement read name-of-array [which-element] // using input statement

20 Set Data for Entire Array or A Portion of the Array
When we want to set the entire array or a portion of an array, we have to set element by element using a loop control structure.

21 Set Data for Entire Array or A Portion of the Array
set element by element using a loop control structure.

22 Set Data for Entire Array or A Portion of the Array
set element by element using a loop control structure.

23 Set Data for Entire Array or A Portion of the Array
set element by element using a loop control structure.

24 Display An Array Display an element of an array as follows: i. Display name-of-array [ which element ] Example: display num[4] // the content of the fifth element will be displayed OR // the content of element at index 4 will be displayed ii. Display the entire of array elements one by one using a loop control structure.

25 Basic Algorithms Using Array
Calculate the total of data in the array

26 Basic Algorithms Using Array
Calculate the average of data in the array

27 Basic Algorithms Using Array
Find the maximum value in the array

28 Basic Algorithms Using Array
Find the minimum value in the array

29 Basic Algorithms Using Array
Counting ( to count how many certain data exists in the array )

30 Basic Algorithms Using Array
Determine how many students in the class KCS1101C pass in the second test. Assume that there are 30 students in the class. Passing mark is 50. List all the pass marks.

31 Basic Algorithms Using Array
Searching ( to find whether a certain data is in the array or not ) Example to find whether 88 is in the array or not. Solution 1: Use count algorithm Use break

32 Start arraySize = 10 Num[arraySize] //segment to input data Set ind with 0 While ind < arraySize read Num[ind] increase ind by 1 endWhile //count how many 88 in the array count88 = 0 Set index with 0 While index < arraySize if ( Num[index] == 88 ) count88 = count88 + 1 EndIF increase index with 1 if ( count88 == 0 ) Display “ 88 is NOT in the array “ Else Display “88 is in the array “ End Solution 1 Start arraySize = 10 Num[arraySize] //segment to input data Set ind with 0 While ind < arraySize read Num[ind] increase ind by 1 endWhile //determine whether 88 is in the array or not found = 0 set ind with 0 while ( found is 0 AND ind < arraySize ) if ( Num[ind] == 88 ) found = 1 break EndIF if ( found == 1 ) Display “88 in the array “ Else Display data “ 88 not in the array “ End Solution 2 When we compare these two solutions, Solution 1 and Solution 2, in terms of the number of iteration, Solution 1 does iteration for arraySize times. Meanwhile Solution 2 stops the iteration when the data was found. It means that the maximum number of iteration is arraySize times. BUT when the data was found, the number of iteration is lesser. Note: statement break is to jump out from the loop when the condition ( num[ind] == 88 ) is true

33 Sort Data Sorting is a process that takes in an unordered collection and makes it an ordered one, either in ascending order or descending order. Many methods can be implemented to sort data such as bubble sort, heap sort, binary sort, etc

34 Sort Data: Bubble Sort: Ascending & Descending
Start arraySize = 10 Num [arraySize ]  //segment to input data ::  //sort data in ascending order Set outer with 0 While outer < arraySize Set inner with outer + 1 While inner < arraySize - 1 if ( Num[ inner ] > Num [outer ] ) temporary = Num[ inner ] Num[ inner ] = Num[ outer ] Num[ outer ] = temporary EndIF increase inner by 1 endWhile add 1 to outer  //display sorted data Set index with 0 While index < arraySize Display Num[index] Increase index by 1 End Sort Data: Bubble Sort: Ascending & Descending Ascending order Descending order if ( Num[ inner ] > Num [outer ] ) if ( Num[ inner ] < Num [outer ] )

35 Execution of Algorithm Uses Array
Refer Tutorial

36 The End


Download ppt "Siti Nurbaya Ismail Senior Lecturer"

Similar presentations


Ads by Google