Neal Stublen
Computer Memory (Simplified) Remember, all programming decisions came down to a true or false evaluation Consider a true decision to have a value of 1 and a false decision to have a value of 0 Now we can say all programming decisions come down to a 1 or 0 evaluation
Computer Memory (Simplified) Computer Memory (4GB) 04,294,967,295 4 GB 4,096 MB = 4 * 1024 MB 4,194,304 KB = 4 * 1024 * 1024 KB 4,294,967,296 bytes = 4 * 1024 * 1024 * 1024 bytes Each byte can store a value from 0 to 255
Computer Memory (Simplified) Operating SystemDisplayApp Object Code Constant Data (Static) Available Memory intint intint intint App string 04,294,967,295 intint ? ?
What’s an array? Every variable represents a value stored somewhere in the computer’s memory We may want to store a group of very similar values An array represents a collection of values stored side-by-side in the computer’s memory Each value is accessed according to its offset from the first value in the array
What is an array? int string int string int string int “Elements” in the array We can declare variables for individual values. We can also declare an “array” of values.
Possible Use of an Array? int Track one high scores in a game… int Track top five high scores in a game…
Declaring an Array num highScore1 = 0 num highScore2 = 0 num highScore3 = 0 num highScore4 = 0 num highScore5 = 0
Declaring an Array num highScore1 = 0 num highScore2 = 0 num highScore3 = 0 num highScore4 = 0 num highScore5 = 0 num highScores[5] = 0, 0, 0, 0, 0
Using Arrays The size of the array is the number of elements in the array Each element in the array exists at a specific index (sometimes referred to as a subscript of the array) Index values typically start at zero highScores[0] = 5400 Assign the first element in the array a value of 5400
Initializing an Array How can we reset or initialize an array so all the elements have the same value? num highScores[5] for index = 0; index < 5; index++ highScores[index] = 0 endfor
Benefits of an Array? We begin tracking 5 high scores… num highScores[5] for index = 0; index < 5; index++ highScores[index] = 0 endfor
Benefits of an Array? num NUM_HIGHSCORES = 5 num highScores[NUM_HIGHSCORES] for index = 0; index < NUM_HIGHSCORES; index++ highScores[index] = 0 endfor If we want to track more high scores, we just change the value of NUM_HIGHSCORES instead of adding highScore6, etc.
Manage High Scores What logic is necessary to track a set of high scores? Start with a set of zero scores Enter a new score and display the list of updated high scores
Mapping Index Values string months[12] = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" num month_num = 10 // October string abbrev = months[month_num – 1]
Searching an Array for index = 0; index < array_size; index++ if array[index] == search_value then // found a match endif endfor Requires anywhere from 1 to array_size iterations to find a match.
Binary Search If the data in the array is sorted, we can reduce the number of searches Like guessing the number between 1 and 100. Start in the middle and keep dividing the array in half
Common Problems Array bounds num values[5] values[18] = -6// Out of bounds!! for index = 0; index <= 5; index += 1 output values[index] // Out of bounds!! endfor
Summary Storing data in arrays Using constants with arrays Using loops to iterate over array elements Searching an array
Heart Evaluation What logic is necessary to recommend an appropriate exercising heart rate? years = bpm years = bpm years = bpm years = bpm years = bpm Over 70 years = bpm A user can enter an age and see the recommended range.