Presentation is loading. Please wait.

Presentation is loading. Please wait.

Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.

Similar presentations


Presentation on theme: "Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider."— Presentation transcript:

1 Neal Stublen nstublen@jccc.edu

2

3 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

4 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

5 Computer Memory (Simplified) Operating SystemDisplayApp Object Code Constant Data (Static) Available Memory intint intint intint App string 04,294,967,295 intint ? ?

6 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

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

8 Possible Use of an Array? int Track one high scores in a game… int Track top five high scores in a game…

9 Declaring an Array num highScore1 = 0 num highScore2 = 0 num highScore3 = 0 num highScore4 = 0 num highScore5 = 0

10 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

11 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

12 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

13 Benefits of an Array? We begin tracking 5 high scores… num highScores[5] for index = 0; index < 5; index++ highScores[index] = 0 endfor

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

15 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

16 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]

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

18 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

19 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

20 Summary  Storing data in arrays  Using constants with arrays  Using loops to iterate over array elements  Searching an array

21 Heart Evaluation  What logic is necessary to recommend an appropriate exercising heart rate? 20-29 years = 120-160 bpm 30-39 years = 114-152 bpm 40-49 years = 108-144 bpm 50-59 years = 102-136 bpm 60-69 years = 96-128 bpm Over 70 years = 90-120 bpm  A user can enter an age and see the recommended range.


Download ppt "Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider."

Similar presentations


Ads by Google