Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Stotts Computer Science Department UNC Chapel Hill.

Similar presentations


Presentation on theme: "David Stotts Computer Science Department UNC Chapel Hill."— Presentation transcript:

1 David Stotts Computer Science Department UNC Chapel Hill

2 0. data (types, simple information) 1. data storage (variables, assignment) 2. data retrieval (expressions, evaluation) 3. repetition (loops) 4. decision making (conditionals) 5. procedure abstraction (functions) 6. data abstraction (arrays) 7. objects: all-the-above, wrapped up

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18 “Musical chairs”

19

20

21 Arrays  Sometimes we need to access a collection of data values as a group, or systematically  We create a name for the entire collection (this is the abstraction)  We access items in the collection by number (called an index ) along with the name  Call this collection an Array

22 4 SeasonsBora Hut #2 “The Smiths want some papaya juice” “Go to Hut #2 and toss in some papaya juice”

23 Think of an array as a contiguous set of memory slots with one name for the whole var bbHuts = new Array( );...... bbHuts 1 0 4 2 3 n Each slot can hold a data value Each slot sort of like a variable Smith

24 var bbHuts = new Array( ); var k = 3 ; bbHuts[0] = “Jackson” ; bbHuts[1] = “Miller” ; bbHuts[2] = “Smith” ; bbHuts[ k ] = “Olafdottir” ; k++ ; // increments k bbHuts[ k ] = “Evans” ; k++ ; bbHuts[ k++ ] = “Xerxes” ; bbHuts[ k ] = “Wilson” ;...... bbHuts 1 0 4 2 3 5 Jackson Smith Miller Evans Olafdottir 7 6 Xerxes Wilson k 34 5 6

25 alert( bbHuts[ k ] ) ; // prints “Wilson” bbHuts[ k+1 ] = bbHuts[ 2 ] + “-Jones” ; // does not change k // k+1 is NOT k++ if (bbHuts[ k-1 ] > bbHuts[ k ] ) { // a swap var tmp = bbHuts[ k ] ; bbHuts[ k ] = bbHuts[ k-1 ] ; bbHuts[ k-1 ] = tmp ; }...... bbHuts 1 0 4 2 3 5 Jackson Smith Miller Evans Olafdottir 7 6 Xerxes Wilson k 34 5 6 Smith-Jones tmp Wilson Xerxes Wilson

26 var x = 4; // arrays can be initialized similarly var colors = [ “red”, ”blue”, ”green”, ”yellow”, “orange”, “purple”, “rose”, “umber”, “mauve”, “chartreuse” ]; alert(colors[1]); // prints blue alert(colors.length); // prints 10, indexes go 0 to 9 alert(colors.indexOf(“rose”); // prints 6

27 var words = [ ] ; // common alternative to new Array( ); var songs = new Array(n) ; // alternative also, says make songs have n slots // doesn’t do much, but other languages do this // JS arrays grow longer as needed words[143] = “splendiferous” ; alert(words[0]) ; // prints undefined alert(words[142]) ; // prints undefined alert(words[-1]) ; // prints undefined alert(words.length) ; // prints 144

28  Can store data related to some common concept in one array, and then get at the component values systematically  for loop is commonly used to get to all array elements one-at-a-time  Example: keep all student names in one array, so the array represents the entire course  Example: keep all student grades in an array, so the array represents the entire course

29 names 1 0 4 2 3 5 Jackson Smith Miller Evans Olafdottir 7 6 Xerxes Wilson Smith-Jones grades 0 1 2 3 4 5 6 7 91.7 56.9 86.3 74.1 83.7 71.2 89.8 99.1 k = 2; // grades[k] and names[k] // select all the information // about person k 2 2 a “student”, student 2

30 function myProg ( ) { size = Number(prompt(“how many students?”)); var students = new Array( size ); for (var sn=0; sn<size; sn++) { students[ sn ] = prompt(“student name?”); } var grades = new Array(size); for (var i=0; i<size; i++) { grades[i] = getGoodGrade( ); // data validation in the function } for (var k=0; k<size; k++) { alert(student[ k ] + “ has grade “ + grades[ k ]); } }

31 function getGoodGrade ( ) { // get a grade and return it after validation var num; num = Number(prompt(“what is the grade?”)); // now validate the input while ( num 100) { alert(“grade must be between 0 and 100”); num = Number(prompt(“what is the grade?”)); } return num ; }

32  Now compute the average grade from the stored array of grades  Then go through the array of grades and find the largest grade  Also print the name of the student who had the highest grade


Download ppt "David Stotts Computer Science Department UNC Chapel Hill."

Similar presentations


Ads by Google