# teaching machines

## CS 145 Lecture 28 – Arrays * 4

November 14, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

With arrays, we are treating the computer as a factory worker, performing some automated task on every item rolling through an assembly line. It’s these mindlessly repetitive tasks that make computers so compelling. While they do the grunt work, we can move on to a more intellectually-challenging exercise, like getting the machine running in the first place and keeping it running.

We will continue working with arrays for a couple of days. On Wednesday we will extend them to multiple dimensions. On Friday we will introduce a cousin to them called an ArrayList. The one remaining big topic for the course is designing our own objects, which we will start after break.

Back to today. Let’s play some array bingo! Here are the rules:

1. Draft a board: an array of 10 ints, with each element in [0, 9].
2. The instructor calls out a series of boolean expressions about elements of a 10-int array. The leftmost element identified in the expression is the target. If the expression is true for the target element of your array, cross it off.
3. Occasionally the subscript of the target element will be expressed in terms of i. On such occasions, i can take on any value in [0, 9], and multiple target elements may be crossed off.
4. The first player(s) to cross off all 10 elements wins.

 0: board[0] == 3
1: board[4] % 2 == 1
2: board[9] > 5
3: board[i] == board[i - 1]
4: board[3] < 2 * board[5]
5: board[7] == board[6] + 1
6: board[i] == 0
7: board[2] < 7
8: board[board[3]] < 6
9: board[6] > board[1] + board[2]
10: board[5] >= board[0] && board[5] <= board[1]
11: board[i] = i
12: board[8] % 3 == 2
13: board[1] < 10
14: board[i] == board[9 - i]
15: board[4] > board[9 / 2]
16: board[7] == board[0] || board[7] == board[1] || board[7] == board[2] || board[7] == board[3]
17: board[generator.nextInt(6) + 1] < d12 roll
18: board[i / 2] <= 4
19: board[i % 3] >= 7
20: board[5] > 100 % 3
21: board[6] < board[8] - board[7]
22: board[i] > board[i + 1] + 1
23: board[i % 4 + 3] >= 2
24: board[7] < board[5] - board[9]
25: board[4] != board[1]
26: board[gen.nextInt(6) + 1] > gen.nextInt(6) + 1

For the remainder of our time, let’s crack open some blackboxes!

Here’s your TODO list to complete before we meet again:

• Lab 9 is posted. Feel free to start early!

See you next class!

Sincerely,

P.S. Here’s the code we wrote together…

#### Blackboxes.java

package lecture1114;

import java.util.Arrays;

public class Blackboxes {
public static void main(String[] args) {
int[] numbers = { 30, 31, 32, 33, 34 };

System.out.println(Arrays.toString(freezeOrNot(numbers)));

System.out.println(Arrays.toString(generatePresquares(10)));
}

public static int[] add1(int[] src) {
int[] dst = new int[src.length];
for (int i = 0; i < dst.length; ++i) {
dst[i] = src[i] + 1;
}
return dst;
}

public static boolean[] freezeOrNot(int[] src) {
boolean[] dst = new boolean[src.length];
for (int i = 0; i < dst.length; ++i) {
dst[i] = src[i] > 32;
}
return dst;
}

public static int[] generatePresquares(int n) {
int[] numbers = new int[n];
//    for (int i = 0; i < n; ++i) {
//      numbers[i] = (i + 1) * (i + 1) - 1;
//    }

for (int i = 1; i <= n; ++i) {
numbers[i - 1] = i * i - 1;
}
return numbers;
}

public static int nZeroOrAbove(int[] src) {
int n = 0;
for (int i = 0; i < src.length; ++i) {
if (src[i] >= 0) {
++n;
}
}
return n;
}

public static int[] fillDown(int start,
int more,
int times) {
int[] values = new int[times];
for (int i = 0; i < times; ++i) {
values[i] = start + more * i;
}
return values;
}
}