1 / 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 lines in DO TOGETHER structure. What if we have lots of objects? What if we want to do a group dance?

adonis
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays

  2. 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!

  3. 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.

  4. 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.

  5. 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?

  6. 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.

  7. General view of an array 10 • 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! index 0 18 index 1 43 index 2 18 index 3 index 4 index 5

  8. ok, enough of serious discussion, let us proceed to dance

More Related