Download presentation
Presentation is loading. Please wait.
Published byErick Long Modified over 9 years ago
1
Arrays
2
2 An array is a variable that holds a collection of related data values. Each of the values in an array is called an element. Each element in the array is identified by an integer value called its index, which indicates the position of the element in the array.
3
3 Array Multiple instances of the same type of item Instances in the array are called elements Each element has a subscript or index Bill Jane Joe Sally NameNames() 01230123
4
4 Arrays Most modern programming languages implement zero-based arrays, meaning that array index values begin with 0 The array length is the number of elements in the array. The upper bound of an array is the index of the last element.
5
5 Accessing Individual Elements of an Array To access individual array elements, you specify the array name and follow it with an index expression enclosed in parentheses. If you attempt to reference an array with an index value greater than the upper bound of the array, the result will be an out-of- bounds error.
6
6 Array Reference Scores(0) Scores(1) Scores(2) Scores(3) Scores(4) Scores(5) Scores(6) Scores(7) Scores(8)
7
7 When to Use Arrays Arrays are useful… – when you are storing or processing large amounts of related data –when information must be stored and processed twice –when you want to store multiple entries of related information
8
8 File Input Rather than enter a large number of values (or even a small number) every time the program is ran, you can get the values from a data file. Supply the full path to the file.
9
9 Above Average Problem (Mutual Funds) The Problem –Jim is a financial analyst with many large investment clients. Each year Jim identifies ten different mutual funds that he shares with his clients for investing. At the end of each year he keeps the funds that performed better than the ten-fund average and replaces the others with new funds.
10
10 Funds Problem Statement Input –10 fund performance values Process –Sum the 10 fund performance values –Divide the Sum by 10 to get the average –For each fund determine if is above the average Output –Average of the 10 funds –Funds that are above the average Funds 11.13 21.21 30.98 41.37 50.95 61.01 70.87 81.21 91.06 100.99 1.078
11
11 Parallel Arrays (Girl Scout Cookies) The Problem –Every spring you look forward to buying a box of Caramel deLites Girl Scout cookies from your niece, Belinda. Her troop gives an award to the girl who sells the most boxes of cookies each year. They are looking to develop a software solution to assist in determining the annual award winner.
12
12 Analysis and Design Parallel arrays are two or more arrays whose elements are related by their position in the arrays.
13
13 Analysis and Design (cont.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.