ICS4U0 Unit4 Package
ICS4U0 Unit4 Package
1 T
Name: _________________________
Angad
0. Look at the following array and answer these questions about it.
(b) What is the value of m.length? ....................
int m[]={1, 8, 3, 4, 20};
(c) What is the value of m.length()? ....................
int t = 3; (d) What is in m[2]? ....................
(e) What is in m[t]? ....................
//Print backwards (f) What is in m[t+1]? ....................
String rev = ""; (g) What is in m[t-1]? ....................
for(int i=m.length-1; i>=0; i--)
rev+=m[i]; (h) What is in element 0? ....................
result.setText(rev); (i) What is the index of 8? ....................
(j) What is the index of 20? ....................
(a) Fill in the array memory diagram for ‘m’: (k) What is in m[m.length]? ....................
[0] [1] [2] [3] [4] (l) What is in m[m.length-1]? ....................
2. Suppose that you have a sorted array and wish to find all the repeated values:
(a) What are the
int sort[]={1,2,3,5,5,6,7,8,9,11,13,13,17,20,20,21};
repeated values
String repeat = ""; in the array?
(b) What is the
for(int i=0; i<sort.length-1; i++){
array type?
if(sort[i]==sort[i+1]) (c) What is the
array name?
repeat+= sort[i]+" ";
(d) What does the
result.setText(repeat); loop start at?
(e) What does the
}
loop end at?
(f) Write code to print the minimum of the sorted array in a TextView named min. (handle inflation first)
(g) Write code to print the maximum of the sorted array in a TextView named max. (handle inflation first)
3. What is an algorithm? (Circle the most correct answer).
(a) Computer code that sorts things. (c) A flowchart diagram.
(b) A series of steps to complete a task. (d) Computer code.
(c) Assume that after the array is sorted, a user randomly selects a number and replaces it with 0.
int randomPlace=(int)(Math.random()*100);
hundred[randomPlace]=0;
Write the code to find the missing number WITHOUT using randomPlace. Save it in an int named answer.
Strong hint: The sum of the numbers from 1 to 100 is 5050.
Android Arrays of Objects 3.2 C
Name: _________________________
1. On a gridLayout, views are added row by row, from left to right. Label the order these widgets should be put
into the gridLayout so that they are added correctly.
2. This game is called Scrubby Dubby. Fill in the grid information with the appropriate picture code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" The picture information:
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">
</LinearLayout>
</LinearLayout>
4. For each picture, you will have a number representation in an integer grid. This tracks the picture value.
(a) Why choose an int type array? (3 reasons, no sentences)
..................................................................
..................................................................
..................................................................
(b) What is a problem with choosing an int type array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5. Explain why a setPicMethod is useful. (2 points, sentences)
.......................................................................................................
.......................................................................................................
.......................................................................................................
.......................................................................................................
Grid Array Algorithms 3.3 K
Name: _________________________
1D to 2D Array
0. ImageView arrays are only 1D so their IDs are unique. The 1D array for the grid below is like this:
However, if you look at the screen, it is laid out in a grid, or 2D array. We use a 2D int array to track these
positions.
For the 1D ImageView array: The references are from the 2D int
(a) How many elements? tracking array. Write the
corresponding ID of the 1D
......... ImageView array.
(b) What is the ID of the
first orange oval? (e) [0][0] .........
......... (f) [2][1] .........
int candy[][] = {{__, __, __, __}, {__, __, __, __}, {__, __, __, __}};
int row = ___;
int col = ___;
ImageView pics[] = new ImageView[row * col];
Level 1 Level 2
int lvl1[][]= {{___, ___, ___, ___, ___, ___}, int lvl2[][]= {{___, ___, ___, ___, ___, ___},
{___, ___, ___, ___, ___, ___}, {___, ___, ___, ___, ___, ___},
{___, ___, ___, ___, ___, ___}}; {___, ___, ___, ___, ___, ___}};
(b) This method redraws the screen with whatever is currently in the game array.
public void redraw() {
int m = 0;
for (int i = 0; i < __________; i++) {
for (int j = 0; j < ____________; j++) {
if (____________[i][j] == ______)
pics[m].setImageResource(R.drawable._____________);
else if (_____________[i][j] == ______)
pics[m].setImageResource(R.drawable._____________);
else if (_____________[i][j] == ______)
pics[m].setImageResource(R.drawable._____________);
else
pics[m].setImageResource(R.drawable._____________);
m++;
}
}
}
(c) Create a method to copy over the array named b into the array named a.
public void copyOver(int a[][], int b[][]) {
for (int i = 0; i < ___________; i++) {
for (int j = 0; j < __________; j++) {
a[i][j] = ______[___][___];
}
}
}
(d) Create an onClick for a button named next that copies the lvl2 array and puts it into the game array. Then it
redraws the array on the screen.
public ____________ _______________(____________ ___________) {
copyOver(__________, __________);
redraw();
}
Searching Algorithms 3.4 C
Name: _________________________
0. Do a linear search on this array for 4.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
-10 -5 4 7 9 10 16 22 28 34 37 40 55 345 900 1002
Found in position: _____
1. Do a linear search on this array for ‘m’.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
a n w e q j k d f g h j
Found in position: _____
2. Do a linear search on this array for “Fish”.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
Cat Dog Tree Frog Fish Zoo Ape Kit Ham Two City Red Yam Bob
Found in position: _____
3. Circle the most correct answer.
T F a) Sorting is faster than searching.
T F b) Sorting is finding an element and searching is putting in order.
T F c) Linear Search has one for loop so it is an “n” speed algorithm.
T F d) Linear Search is slower than Selection Sort.
T F e) Linear Search is faster than Quick Sort.
T F f) Linear Search would work on unsorted data.
T F g) Linear Search would work well when looking for the name “ZZtop” in the
phone book.
T F h) Linear Search would work well when looking through a pile of unsorted test
papers to find a particular student’s work.
T F i) The Google Search engine uses linear search because it is really efficient.
if(foundit)
result.setText("It is in position "+mid);
else
result.setText("It is not in the array");
6. Trace the search of this array to find ‘12’ using binary search. Draw the search on the array too.
7. Trace the search of this array to find ‘W’ using binary search. Draw the search on the array too.
8. Trace the search of this array to find ‘nap’ using binary search. Draw the search on the array too.
.................................................................................................
Selection Sort 3.5 K
Name: _________________________
1. Find the maximum element in each data set.
(a) 2, 5, 67, 8, 7, 98, 1
(b) a, g, r, t, y, u, p, q
(c) zebra, cheetah, elephant, hippo, rhino
(d) red, orange, yellow, green, blue, indigo, violet
2. In each of these three data sets, circle the first two elements that swap.
(a)
(b) (c)
...................................................................................................................
...................................................................................................................
5. Put these algorithm speeds in order. 6. What speeds are these array algorithms?
(1 is fastest, 7 is slowest) (a) Linear search _________
___ O(2n) (b) Finding array length _________
___ O(n!) (c) Selection Sort _________
___ O(log n) (d) Finding the average _________
___ Constant time (e) Mergesort _________
___ O(n2) (f) Binary search _________
___ O(n) (g) Redraw _________
___ O(n log n) (h) Copy one array into another _________
7. Trace selection sort for these arrays.
(a) (b) (c)
8 9 4 2 3 1 V T Q U R S Big Ape Add Cap App
} }
(c) The array type is String (2 changes) (d) You want to go from largest to smallest (only 1
code change is needed, then 5 to rename max)
for (int left = a.length - 1 ; left > 0 ; left--) { for (int left = a.length - 1 ; left > 0 ; left--) {
} }
Bubble Sort 3.6 K
Name: _________________________
0. Which tracing shows bubble sort and which show selection sort?
9 8 7 6 5 9 8 7 6 5
8 9 7 6 5 5 8 7 6 9 q a m b c
8 7 9 6 5 5 6 7 8 9 a q m b c
8 7 6 9 5 a m q b c
8 7 6 5 9 a m b q c
7 8 6 5 9 q a m b c a m b c q
7 6 8 5 9 c a m b q a b m c q
7 6 5 8 9 c a b m q a b c m q
6 7 5 8 9 b a c m q
6 5 7 8 9 a b c m q
5 6 7 8 9
2. One of these pictures represents bubble sort and one selection. Which is which?
Selection Sort
8 6 4 1 3
int a [] = {5, 62, 81, 9, 30, 42, 0}; 4. Answer the following questions about
the adjacent code.
public void Bubble(View view) { (a) What is in the array to start?
clear(); 0 1 2 3 4 5 6
unsort();
bubbleSort(a);
redraw();
}
(b) What type is the array? ..........
public void bubbleSort(int a[]) { (c) What is the name of the array? . . . . . . .
int n = a.length;
for (int i = 0; i < n - 1; i++) { (d) What is a.length’s value? .........
for (int j = 0; j < n - 1 - i; j++) {
if (a[j + 1] < a[j]) { (e) What is the onClick of the button?
int temp = a[j];
a[j] = a[j + 1]; ...................
a[j + 1] = temp;
addNewRow();
(f) What is in memory like after the array
} is sorted?
} 0 1 2 3 4 5 6
}
}
..........................................................................................................
..........................................................................................................
..........................................................................................................
..........................................................................................................
6. Which one is selection sort? (circle) 7. In 2013, the Waterloo Beaver Computing Challenge asked the
following question. You are arranging people in order based on the
numbers on their shirts. The order to start is:
7 3 2 9 8 5 1 4 6
You will arrange individuals using the following technique:
• Look at two consecutive people at a time, starting from the
left.
• If the person on the left has a number which is larger than
that of the person on the right, switch the positions of those
two people; otherwise, leave them in the order they are in.
• Move to the right one position, so that you are comparing
one new person with one of the people just compared, and
repeat the above comparison and potential swap.
Once you have compared the right-most two people in the list, we
call this one “pass” over the list.
How many passes over the list are required until the list is in the
order:
1 2 3 4 5 6 7 8 9?
Name: _________________________
Conceptually, a merge sort works as follows:
Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will
be the sorted list.
1. Fill in the speeds of the algorithms: 2. Given the following array of numbers:
________ Bubble sort, best case. {21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40}
which answer illustrates the array to be sorted after 3
________ Bubble sort, average case.
recursive calls to mergesort? (circle the correct answer)
________ Delete to a list.
________ Add to a list. (A) {16, 49, 39, 27, 43, 34, 46, 40}
(B) {21,1}
________ Mergesort average case. (C) {21, 1, 26, 45}
________ Selection sort, average case. (D) {21}
________ Print out array.
________ Find the maximum
Name: _________________________
void quicksort(int[] array, int startIndex, int endIndex) The steps are:
{
if (startIndex >= endIndex) { Pick an element, called a pivot, from the array.
return; Partitioning: reorder the array so that all elements less
than the pivot come before the it, and all elements
} else {
int pivotIndex = partition(array, startIndex, endIndex);
greater come after. After partitioning, the pivot is in its
quicksort(array, startIndex, pivotIndex - 1); final position.
quicksort(array, pivotIndex + 1, endIndex); Recursively apply the above steps to the sub-array of
} elements before the pivot AND again to those after it.
}
0. Which of the following pictures show quickSort and which show mergeSort?
.............................................................................................................
3. Why are quicksort swaps so much better than bubblesort swaps?
.............................................................................................................
.............................................................................................................
.............................................................................................................
4. Trace quicksort on the following arrays.
4 2 9 7 8 3 5 1 6 8 7 6 9 0 1 3 4 5
.............................
b) You have no extra memory and the array randomly ordered.
.............................
c) You have extra memory and the array is in random order.
.............................
d) The array is almost sorted and you are eating an apple.
.............................
e) An integer array, storing ages, is in almost reverse order.
.............................
f) The String array is in almost reverse order, there are 5
million elements and you are sorting on a school computer.
.............................
g) The char array is not well randomized and you have lots of
extra memory.
.............................
The Secret Lives of Modern Algorithms 3.9 C
Name: _________________________
1. From the movie, the Secret Lives of Algorithms, fill in the first column. (The last 5 are from class)
Word Description
A series of steps to complete a task.
Phones use this to quickly find human faces in real time on the screen.
A game strategy algorithm. If your opponent takes n items, you take (4-n).
The oldest recorded algorithm. Finds the greatest common divisor for 2 #s.
A sorting algorithm that repeatedly swaps out of order elements into place.
John Von Neumann’s sorting algorithm. Data is divided then merged.
A billion dollar algorithm that puts searches in an order useful to the user.
A gaming system that taught itself how to understand human movement.
A streaming service that uses algorithms to make future recommendations.
Robots that use algorithms to put together customer orders for mailing.
A compromise. You give up something to get something else.
Looks at each item in the list until the desired item is found.
The worst sorting algorithm. It might never finish.
Repeatedly discarding half of a sorted list until the item is found.
A sorting method where you repeatedly swap the max into place.
The fastest in-place algorithm in the general case.
Across
7. A searching algorithm that always works, but it is a bit slower.
9. A sorting algorithm invented by John Von Neumann. Data is divided and merged in order.
10. The worst sorting algorithm.
11. The fastest in-place sorting algorithm for regular randomized data.
13. The oldest recorded algorithm. Finds the greatest common divisor for 2 large
numbers.
15. A streaming service that uses algorithms to make recommendations based on your
past movie choices.
16. The fastest searching algorithm.
Down
1. Google’s billion-dollar algorithm; puts web searches in an order useful to the user.
2. A sorting method where you repeatedly find the max and swap it into place.
3. Robots that use algorithms to efficiently put together customer orders for mailing.
4. Phones use this algorithm to quickly find human faces in real time on the screen.
5. A sorting algorithm that repeatedly swaps out of order elements into place. Named
after pop bubbles.
6. A series of steps to complete a task.
8. A game strategy algorithm. If your opponent takes n items, you take (4-n) items.
12. A compromise made when you make a choice. You give up something to get
something else.
14. A gaming system that taught itself how to understand human movement.
PDLC 3.10 C
Name: _________________________
1. What does the acronym PDLC stand for?
Analysis Design
1. Create a storyline 1. Character Design
2. Hold Focus Groups 2. Artwork
3. Design levels 3. Level Design
A......... B......... 4. Pitch Concept 4. Watch Netflix
5. Write while loops 5. Make Prototypes
6. Eat Lunch 6. Create storyline
7. Define the Problem 7. Alpha testing
8. Build a snowman 8. Draw Flow charts,
D. . . . . . . . . C.........
9. Analyze sales figures structure charts
10. Write a list of 9. Write and record music
specifications 10. Define the problem
Across
5. Reviews the sales in the
reflection phase. Thinks about a
sequel.
7. Would pitch the overall idea
and decide the direction of the
software.
8. A type of testing done inside the
company.
Down
1. Draws diagrams and lays out
the details of the program
2. Supports users who are having
difficulties with the software.
3. Designs the screen layout,
colours, character design and
backgrounds.
4. Composes and records music
for the software.
6. creates advertisements for the
software in the reflection phase