8
Arrays

Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Embed Size (px)

Citation preview

Page 1: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Arrays

Page 2: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Arrays: Introduction

• So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple lines in DO TOGETHER structure.

• What if we have lots of objects? What if we want to do a group dance?

• With current approach, program will become too lengthy and unmanageable!

Page 3: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Arrays: Introduction

• Commercial programs use lot of data! We cannot afford to declare individual variables to keep each piece of data in memory!

• Let us look into a simple example.

Page 4: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Texas Lotto

• Texas Lottery involves selecting 6 unique numbers from the range of 1 to 50. We can put 50 balls with 50 numbers in a bowl, mix them and take 6 of them – they are guaranteed to be 6 different numbers!

• What if we want to computerize? Assuming that we know how to generate random number between 1 and 50, we can repeat it 6 times.

Page 5: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Texas Lotto …

• It works well, but occasionally we may get a selection like 10, 18, 43, 18, 23, 34. Is there a problem?

• Yes, we cannot have repeating numbers in the selection: 10, 18, 43, 18, 23, 34. It is invalid!

• Then, how to ensure that the numbers are unique?

Page 6: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

Ideas?

What should we do after generating each number?• Check against the numbers selected so far• If current number is a repetition, throw it away

and reselect.

Selection can be considered as a collection or array of 6 numbers.

Page 7: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

General view of an array

• Typically, array has a name – need to use index # to access a specific item.

• Stored items can be anything. For example, we can define array of objects too!

10

18

43

18

index 0

index 1

index 2

index 3

index 4

index 5

Page 8: Arrays. Arrays: Introduction So far, we handled only one object in each line of code. When we wanted multiple objects to do something, we wrote multiple

ok, enough of serious discussion, let us proceed to dance …