Arrays - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Arrays

Description:

UNC NCAA championships. 1957, 1982, 1993, 2005. Names of seven dwarfs ... Square brackets [] indicates list of array items. Items separated by comma. Fixed array ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 43
Provided by: scie2
Category:
Tags: arrays | brackets | ncaa

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • Dorian Miller

2
Announcements
  • Homework 3
  • Due Thursday Sept 27, 1159pm EST
  • Program 2
  • Due Tuesday Oct 2, 1159pm EST

3
Program 1 graded
  • Program 1 results available
  • See My Grades on BlackBoard
  • Program 1 solution posted
  • Purpose of program 1
  • Simple JavaScript program
  • Variables and computation
  • HTML forms
  • Practical debugging experience to fix program

4
Range of scores
  • Minor issues
  • points 90-101
  • Program needs improvement
  • points 55-90
  • Major problems with program
  • less than 50 points

5
Minor issues (points 90-101)
  • Constants
  • All numbers must be constants
  • 200 common magic number
  • Use of comments, missing or too few
  • Missing, incorrect HTML tags

6
Program needs improvement (points 55-90)
  • Good strategy for partial credit
  • Program runs (works with tester)
  • Incorrect computation
  • Output was not in form must be in form, not alert

7
Major problems with program (less than 50 points)
  • Program does not run
  • Program produces no output
  • Graded on HTML and Javascript (25 points)
  • Late submission 0 points
  • Edit web page after deadline
  • Tester shows date and time of last file save

8
Resources
  • Office hours (or by appointment)
  • Dorian MW 330-430
  • Will Tu, Th 2-3pm
  • Email questions to Will and Dorian
  • Reading
  • Online reading for each lecture listed on course
    schedule (next to slides)
  • Lecture slides

9
Collaboration
  • Allowed to discuss
  • lecture material
  • recitation material
  • Program specification
  • Homeworks and programs completed independently
  • Rule of thumb for discussions
  • Discuss ideas
  • Take a 1 hour break
  • Independently implement code (shows significant
    understanding)

10
Important Please stay after class
  • Review honor code

Rule of thumb for discussions 1. Discuss ideas 2.
Take a 1 hour break 3. Independently implement
code (shows significant understanding)
11
Practice tracing a program
  • Trace a program (part of homework 3)
  • Process by hand and paper
  • Walk through program and predict output
  • Useful for understanding and debugging program
  • Typical exam question

12
Trace the while loop
  • What does this program do?
  • var i 0
  • var sum 0
  • while (i
  • sum sum i
  • alert("i " i " sum " sum)
  • i i5
  • Final value of i and sum?
  • i ???
  • sum ???
  • Values at alert
  • list values to trace
  • Each line, 1 pass through loop
  • Variable value changes at assignment
  • i sum
  • ____ ____
  • ____ ____
  • ____ ____
  • ____ ____
  • ____ ____

13
Trace the while loop
  • What does this program do?
  • var i 0
  • var sum 0
  • while (i
  • sum sum i
  • alert("i " i " sum " sum)
  • i i5
  • Final value of i and sum?
  • i25 (i increased and condition false)
  • sum 50 (did not change)
  • Values at alert
  • list values to trace
  • Each line, 1 pass through loop
  • i sum
  • 0 0
  • 5 5
  • 10 15
  • 15 30
  • 20 50

14
Trace the for loop
  • What does this program do?
  • var i 0
  • var sum 0
  • var i
  • var sum 0
  • for (i1 i
  • sum sum i
  • alert("i " i " sum " sum)
  • Final value of i and sum?
  • i ???
  • sum ???
  • Values at alert
  • list values to trace
  • Each line, 1 pass through loop
  • Variable value changes at assignment
  • i sum
  • ____ ____
  • ____ ____
  • ____ ____
  • ____ ____
  • ____ ____

15
Trace the for loop
  • What does this program do?
  • var i
  • var sum 0
  • for (i1 i
  • sum sum i
  • alert("i " i " sum " sum)
  • Final value of i and sum?
  • i 32 (changed but condition is false)
  • sum 31 (Stays the same)
  • Values at alert
  • list values to trace
  • Each line, 1 pass through loop
  • Variable value changes at assignment
  • i sum
  • 1 1
  • 2 3
  • 4 7
  • 8 15
  • 16 31

16
Big Picture
  • Control structures
  • If statements
  • Loops
  • This week
  • Array data structures

17
Simple program analogyControl structures
  • Program Instructions
  • While (need more)
  • Take bread
  • Get scoop of peanut butter
  • Spread scoop on bread
  • If (need strawberry)
  • Get scoop of strawberry
  • else
  • Get scoop of grape
  • Spread scoop on bread
  • Done
  • Control structure
  • Selection
  • Loops
  • Related to program instructions

18
Simple program analogyMaking a peanut butter and
Jelly sandwich
  • Data until now
  • Variables
  • Single data elements
  • Examples of single data
  • Number of Scoops
  • Slices of bread
  • Sandwich as text
  • Program Data
  • PB scoops 5
  • Jelly scoops 5
  • Bread slices 10
  • Sandwich ???

19
Examples of list of data
  • List of multiple data items
  • UNC NCAA championships
  • 1957, 1982, 1993, 2005
  • Names of seven dwarfs
  • Grumpy, Sneezy, Doc, Happy, Dopey, Bashful,
    Sleepy

20
Wrong way
  • Variables are not good solution to store
    championships
  • Example Wrong way to store list
  • var year1 1957
  • var year2 1982
  • var year3 1993
  • var year4 2005
  • Many items difficult to manage as individual
    variables
  • How about 100 or 1000 data items?

21
Arrays
  • An array is a list of values that can be
    represented by one variable
  • Example
  • var wins 1957, 1982, 1993, 2005
  • Syntax
  • wins is a variable
  • Square brackets indicates list of array items
  • Items separated by comma

22
Fixed array
  • var wins 1957, 1982, 1993, 2005
  • Fixed
  • Values not intended to change in program
  • Example Years of championship are fixed
  • Set at design time when program is created

23
Array index
  • Array index position of data in list
  • First index 0
  • Called zero offset
  • Last index number of items -1
  • Example
  • First item, index 0 has value 1957
  • Last item, index 3 has value 2005
  • 0 1 2 3
  • var wins 1957, 1982, 1993, 2005

24
Array access
  • Variable wins refers to entire list
  • var wins 1957, 1982, 1993, 2005
  • Access one array item
  • Get the value stored in the list at the index
    position
  • Square brackets indicate position of list item
  • winsindex
  • Example of array access
  • wins0 is 1957
  • wins1 is 1982
  • wins2 is 1993
  • wins3 is 2005

25
Array access example
  • Read the value of championship
  • var wins 1957, 1982, 1993, 2005
  • Store the final win value in variable last
  • var last wins3
  • Display value of first win
  • document.write(First win wins0)

26
Array length
  • Array could be any length
  • var wins 1957, 1982, 1993, 2005
  • How many array elements? 4
  • wins.length
  • Syntax
  • Period operator
  • Length property, number of items in array
  • Example
  • var numberOfItems wins.length

27
Display elements in array
  • Display elements in array
  • var wins 1957, 1982, 1993, 2005
  • document.write(wins0
    )
  • document.write(wins1
    )
  • document.write(wins2
    )
  • document.write(wins3
    )
  • Write a for loop to display elements
  • First index is 0
  • Last index is wins.length -1

28
Solution
  • Details
  • Index offset by one
  • Start index 0
  • Last index i (wins.length-1)
  • var wins 1957, 1982, 1993, 2005
  • var i
  • for(i0 i
  • document.write(winsi "
    ")

29
Errors
  • Out of bounds access
  • Access array index outside valid range
  • Valid range 0 to array.length-1
  • Results undefined
  • Example undefined values
  • var wins 1957, 1982, 1993, 2005
  • wins-1 is undefined
  • wins4 is undefined

30
Review concept of memory
  • Review Memory
  • Cell stores value
  • Each cell has address
  • Memory Stores data

address
9278 9279 9280 9281 9282 9283
cell
31
Declaration
var jellyScoops
  • Declaration
  • Creates variable in program
  • Reserves memory cell
  • Value starts undefined
  • Why address 9279?

9278 9279 9280 9281 9282 9283
jellyScoops
16
undefined
32
Array in memory
  • var wins 1957, 1982, 1993, 2005
  • Array elements use consecutive memory locations

wins
9278 9279 9280 9281 9282 9283
1957
1982
1993
2005
33
Array in memory
  • Win is a reference variable
  • Reference variable stores the memory location
  • winsindex refers to memory location wins
    index

wins
9278 9279 9280 9281 9282 9283
1957
1982
1993
2005
34
Dynamic Array
  • Array values not known at design time
  • Example compute average grade
  • List of assignment scores
  • User determines number of scores
  • User enters scores

35
Create a new array
  • Create a new array with 5 elements
  • var scores new Array(5)
  • Syntax
  • new operator creates an array object
  • Array is the array object
  • (5) the number of elements in array
  • new and Array are keyword words

36
New Array
  • Initial array values
  • null value that stands for nothing
  • null is keyword
  • Check initial value
  • var scores new Array(5)
  • if (scores0 null)
  • alert(Yes, the value is null)

37
Write array value
  • Write one array value
  • Assign value like any variable
  • Store integer 99 in array index 0
  • scores0 99

38
Set Initial array values
  • What is a reasonable initial value for scores?
  • Initial value ???
  • var scores new Array(5)
  • Write a for loop to initialize score array
  • Start index ???
  • End index ???

39
Solution
  • Set initial value
  • 5 array elements
  • Start index 0
  • Last index 4
  • var scores new Array(5)
  • var i
  • for(i0 i
  • scoresi0

40
Exercise
  • Say these scores are entered into the array
  • scores 33, 74, 20, 51, 60
  • The average is
  • Sum 33 74 20 51 60
  • Average sum / 5
  • Write a for loop for compute the average

41
Solution Compute average
  • var scores 33, 74, 20, 51, 60
  • var i
  • var sum0
  • for(i0 i
  • sum sum scoresi
  • document.write("sum " sum "
    ")
  • document.write("average " sum/scores.length)

42
Important Please stay after class
  • Review honor code
Write a Comment
User Comments (0)
About PowerShow.com