1 Gentle Introduction to Programming Session 4: Arrays.
-
date post
22-Dec-2015 -
Category
Documents
-
view
218 -
download
0
Transcript of 1 Gentle Introduction to Programming Session 4: Arrays.
![Page 1: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/1.jpg)
1
Gentle Introduction to Programming
Session 4: Arrays
![Page 2: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/2.jpg)
2
Review• Review on Functions
• Higher order functions• Recursion
• Solving big instances using the solution to smaller instances• Solving directly the base cases
• Tower of Hanoi• Recursion and efficiency (Fibonacci)• Importance of correct base-cases (Odd-Even)
![Page 3: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/3.jpg)
3
Today• Home work review • Recursive vs. Iterative • Arrays
• Arrays in memory• Initialization and usage• foreach, filter • Arrays as functions arguments• Multi-dimensional arrays• References to array
• Sorting, searching and time-complexity analysis• Binary search• Bubble sort, Merge sort
• Home work
![Page 4: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/4.jpg)
4
Exercise 1
• Write a program that receives two non-negative integers and computes their product recursively
• Hint: Notice that the product a*b is actually a+a+…+a (b times). How does this help us define the problem recursively?
![Page 5: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/5.jpg)
5
SolutionProduct.scala
![Page 6: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/6.jpg)
6
Exercise 2
• Given the following iterative version of sum-of-digits calculation
Write the recursive definition
![Page 7: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/7.jpg)
7
SolutionSumDigits.scala
![Page 8: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/8.jpg)
8
Exercise 3
• Write a function that simulates print on positive integers
• The function should print one digit at time• Example:
• printRec(1234) 1234
![Page 9: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/9.jpg)
9
SolutionPrint.scala
![Page 10: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/10.jpg)
10
Today• Home work review • Recursive vs. Iterative • Arrays
• Arrays in memory• Initialization and usage• foreach, filter • Arrays as functions arguments• Multi-dimensional arrays• References to array
• Sorting, searching and time-complexity analysis• Binary search• Bubble sort, Merge sort
• Home work
![Page 11: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/11.jpg)
1111
Compute ab Iteratively
• Operationally:
• Halting condition:
product product * a
counter counter - 1
counter = 0
ab = a2 *a*…*a = a3 *…*a• Which is:
ab = a * a * a*…*a
b
![Page 12: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/12.jpg)
12
Compute ab Iteratively
![Page 13: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/13.jpg)
13
Compute ab (Recursive Approach)
• Recursive call: ab = a * a(b-1)
• Base case: a0 = 1
![Page 14: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/14.jpg)
14
Compute ab (Iterative Approach)
How then, do the two procedures differ?
![Page 15: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/15.jpg)
15
Recursive Process
powRec(3,4)
3 * powRec(3,3)
3 * (3 * powRec(3,2))
3 * (3 * (3 * powRec(3,1)))
3 * (3 * (3 * (3 * powRec(3,0))))
3 * (3 * (3 * (3 * 1)))
3 * (3 * (3 * 3))
3 * (3 * 9)
a * 27
81
![Page 16: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/16.jpg)
16
Iterative Process
powTailRec(3,4)
pow1(3,4,1)
pow1(3,3,3)
pow1(3,2,9)
pow1(3,1,27)
pow1(3,0,81)
81
![Page 17: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/17.jpg)
17
powRec(3,4)
3 * powRec(3,3)
3 * (3 * powRec(3,2))
3 * (3 * (3 * powRec(3,1)))
3 * (3 * (3 * (3 * powRec(3,0))))
3 * (3 * (3 * (3 * 1)))
3 * (3 * (3 * 3))
3 * (3 * 9)
a * 27
81
The DifferenceGrowing amount of space
Constant amount of space
powTailRec(3,4)
pow1(3,4,1)
pow1(3,3,3)
pow1(3,2,9)
pow1(3,1,27)
pow1(3,0,81)
81
![Page 18: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/18.jpg)
18
Why More Space?
Operation pending
No pending operations
![Page 19: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/19.jpg)
19
Summary
• Recursive process num of deferred operations “grows proportional to b”
• Iterative process num of deferred operations stays “constant” (actually it’s zero)
Can we better quantify these observations?
![Page 20: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/20.jpg)
20
Order of Growth: Recursive ProcesspowRec(3,5)
3 * powRec(3,4)
3 * (3 * powRec(3,3))
3 * (3 * (3 * powRec(3,2)))
3 * (3 * (3 * (3 * powRec(3,1))))
3 * (3 * (3 * (3 * (3 * powRec(3,0)))))
3 * (3 * (3 * (3 * (3 * 1))))
3 * (3 * (3 * (3 * 3)))
3 * (3 * (3 * 9))
3 * (3 * 27)
a * 81
243
powRec(3,4)
3 * powRec(3,3)
3 * (3 * powRec(3,2))
3 * (3 * (3 * powRec(3,1)))
3 * (3 * (3 * (3 * powRec(3,0))))
3 * (3 * (3 * (3 * 1)))
3 * (3 * (3 * 3))
3 * (3 * 9)
a * 27
815
4
Dependent on b
![Page 21: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/21.jpg)
21
Order of Growth: Iterative Process
powTailRec(3,5)
pow1(3,5,1)
pow1(3,4,3)
pow1(3,3,9)
pow1(3,2,27)
pow1(3,1,81)
pow1(3,0,243)
243
powTailRec(3,4)
pow1(3,4,1)
pow1(3,3,3)
pow1(3,2,9)
pow1(3,1,27)
pow1(3,0,81)
81
Same constant, independent of b
![Page 22: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/22.jpg)
22
“Tail” Recursion in Scala
• Scala compiler translate tail-recursion to iterative execution
• Thus, the functions-stack is not growing
![Page 23: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/23.jpg)
23
Today• Home work review • Recursive vs. Iterative • Arrays
• Arrays in memory• Initialization and usage• foreach, filter • Arrays as functions arguments• Multi-dimensional arrays• References to array
• Sorting, searching and time-complexity analysis• Binary search• Bubble sort, Merge sort
• Home work
![Page 24: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/24.jpg)
24
Arrays
• Array: sequential block of memory that holds variables of the same type
• Array can be declared for any type• Example: new Array[Int](3)
• Examples:• list of students’ marks• series of numbers entered by user• vectors• matrices
![Page 25: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/25.jpg)
25
Arrays in Memory
• Sequence of variables of specified type• The array variable itself holds the address in
memory of beginning of sequence• Example: val s = new Array[Double](10)
• The k-th element of array A is specified by A[k-1] (0 based)
0 1 2 3 4 5 6 7 8 9
s
……
![Page 26: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/26.jpg)
26
Example - Initialization
![Page 27: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/27.jpg)
27
Arrays in Memory
• Access array’s content
• Change array’s content
![Page 28: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/28.jpg)
28
Example
Array.scala
![Page 29: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/29.jpg)
29
foreach, filter
• Iterates over arrays (and other containers)
• foreach – for each element apply a given function
• filter – create a new container containing only elements that pass the given Boolean-function
![Page 30: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/30.jpg)
30
Example – Print Arrays PrintArray.scala
![Page 31: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/31.jpg)
31
Example – filter
![Page 32: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/32.jpg)
32
Example – Find MinimumFindMin.scala
![Page 33: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/33.jpg)
33
Arrays as Function Arguments
• Functions can accept arrays as arguments
• Example:
• Within the function, arr is accessed in the usual way
• Changes to the array in the function change the original array! (why?)
![Page 34: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/34.jpg)
34
ExampleArraySum.scala
![Page 35: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/35.jpg)
35
ExampleMultAll.scala
![Page 36: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/36.jpg)
36
Efficient Factorial
• Write a program that repeatedly receives a natural number as its input and returns its factorial
• The maximal number that will be given is known to be 1000
• The program should be efficient:• Do not calculate 1*2*…*n for every n from the
beginning on every input• Do not calculate values that were not requested
![Page 37: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/37.jpg)
37
Solution: Main Idea
• We can keep in an array the results
• When given a new n• If calculated before – return it• Otherwise, get a better start
![Page 38: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/38.jpg)
38
SolutionEfficientFact.scala
![Page 39: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/39.jpg)
39
Solution (main)
EfficientFact.scala
![Page 40: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/40.jpg)
40
Multi-Dimensional Arrays
• Array of arrays:val arr = Array[Array[Int]](3,2)
• Means an array of 3 integer arrays, each of length 2• Access: jth element of the ith array is a[i][j]
![Page 41: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/41.jpg)
41
References to Arrays
What is going on here?
![Page 42: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/42.jpg)
42
In Memory
321x1
x2
100
![Page 43: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/43.jpg)
43
Today• Home work review • Recursive vs. Iterative • Arrays
• Arrays in memory• Initialization and usage• foreach, filter • Arrays as functions arguments• Multi-dimensional arrays• References to array
• Sorting, searching and time-complexity analysis• Binary search• Bubble sort, Merge sort
• Home work
![Page 44: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/44.jpg)
44
Sort
• We would like to sort the elements in an array in an ascending order
7 2 8 5 4 2 4 5 7 8sort
![Page 45: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/45.jpg)
45
What is Sorting Good For?
• Remember exercise 4 (find number)?• Now we have a large array (of length n) and have
multiple queries on whether a given number exists in the array (and what is its position in it)
• Naive solution: given a number, traverse the array and search for it• Not efficient ~ n/2 steps for each search operation
• Can we do better?• Sort the array as a preliminary step. Now search can be
performed much faster!
![Page 46: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/46.jpg)
46
Today• Home work review • Recursive vs. Iterative • Arrays
• Arrays in memory• Initialization and usage• foreach, filter • Arrays as functions arguments• Multi-dimensional arrays• References to array
• Sorting, searching and time-complexity analysis• Binary search• Bubble sort, Merge sort (rest on Thursaday)
• Home work
![Page 47: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/47.jpg)
47
Exercise 1
Write a program that gets 10 numbers from the user.It then accepts another number and checks to see ifthat number was one of the previous ones.
Example 1:Please enter 10 numbers:1 2 3 4 5 6 7 8 9 10Please enter a number to search for: 8Found it!
Example 2:Please enter 10 numbers:1 2 3 4 5 6 7 8 9 10Please enter a number to search for: 30Sorry, it’s not there
![Page 48: 1 Gentle Introduction to Programming Session 4: Arrays.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d805503460f94a64fb2/html5/thumbnails/48.jpg)
48
Exercise 2
• Implement a function that accepts two integer arrays and returns true if they are equal, false otherwise. The arrays are of the same size
• Write a program that accepts two arrays of integers from the user and checks for equality