1 / 19

Arrays

Arrays. Real-world Scenario. PLU code is missing for a product you are buying at the grocery counter. Associate may use a cheat sheet showing PLU code for each product – it works like an array. Historical snippets …. BASIC & FORTRAN : array index started at 1 COBOL?

jael
Download Presentation

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. Real-world Scenario • PLU code is missing for a product you are buying at the grocery counter. • Associate may use a cheat sheet showing PLU code for each product – it works like an array.

  3. Historical snippets … • BASIC & FORTRAN : array index started at 1 • COBOL? • Modern languages use index starting at 0 • How about Visual Basic?

  4. Array index start with 0 or 1? "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration." -- Stan Kelly-Bootle

  5. Array index start with 0 or 1?

  6. Scenario: Max price • Let us say we want to read list of prices from the user and find the max price. • Do we need an array?

  7. Find max price: pseudocode item_number = 0 DOWHILE not eof item_number = item_number + 1 read item_price[item_number] ENDDO max_price_item = 0 max_price = 0.00 FOR i = 1 to item_number do IF max_price < item_price[i ] THEN max_price_item = i; max_price = item_price[i ]; ENDIF ENDFOR print max_price_item, max_price

  8. Find max price: no arrays item_number = 0 max_price_item = 0 max_price = 0.00 DOWHILE not eof item_number = item_number + 1 read item_price IF max_price < item_price THEN max_price_item = i; max_price = item_price; ENDIF ENDDO print max_price_item, max_price

  9. When arrays are needed? • Only if we are going to use all those individual input values again.

  10. Do we need arrays? • Mortgage amortization table expansion – show the output for user-specified year

  11. Do we need arrays? • Compute total ticket sales/donations for the day

  12. Do we need arrays? • Find max, min, average air-fare paid by customers

  13. Do we need arrays? • Find the median air-fare paid by customers

  14. Do we need arrays? • Output all the airfare amounts in sorted order

  15. Do we need arrays? • Keeping track of your score at Golf course

  16. Arrays • Easy access • Fixed size • When bigger size is needed, we need to allocate new array and copy the current contents over. • Use ArrayList for varying array sizes (not covered in this course)

  17. Multi-dimensional arrays • Students’ scores in one assignment intscores[student_index]

  18. Multi-dimensional arrays • Students’ scores in all assignments int scores[assignment_index][student_index]

  19. Multi-dimensional arrays • Students’ scores in all assignments for several sessions of that course int scores[session_index][assignment_index][student_index]

More Related