teaching machines

CS 145 Lecture 27 – Array Exercises

November 6, 2015 by . Filed under cs145, fall 2015, lectures.

Agenda

TODO

Note

In case you haven’t noticed yet, we don’t just jump from new topic to new topic in this class. We savor each one by exploring problems relevant to the current topic. Today, we see more of arrays in two ways. First we play some array bingo. Then we generate some music.

Bingo

The rules:

  1. Draft a board: an array of 10 ints. Each element is 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.
  1: board[0] == 3
  2: board[4] % 2 == 1
  3: board[9] > 5
  4: board[i] == board[i - 1]
  5: board[3] < 2 * board[5]
  6: board[7] == board[6] + 1
  7: board[i] == 0
  8: board[2] < 7
  9: board[board[3]] < 6
 10: board[6] > board[1] + board[2]
 11: board[5] >= board[0] && board[5] <= board[1]
 12: board[board[i]] = i
 13: board[8] % 3 == 2
 14: board[1] < 10
 15: board[i] == board[9 - i]
 16: board[4] > board[9 / 2]
 17: board[7] == board[0] || board[7] == board[1] || board[7] == board[2] || board[7] == board[3]
 18: board[d6 roll] < d12 roll
 19: board[i / 2] <= 4
 20: board[i % 3] >= 7
 21: board[5] > 100 % 3
 22: board[6] < board[8] - board[7]
 23: board[i] > board[i + 1] + 1
 24: board[i % 4 + 3] >= 2
 25: board[7] < board[5] - board[9]
 26: board[4] != board[1]
 27: board[d6 roll] > d6 roll

Music

Next we generate audio clips by sampling sine waves of various frequencies. The pressure at time t of a sound wave of a given frequency on our ear is sin(2 * pi * frequency * t). We’ll walk along regular intervals of time, store the pressure in an array, write the array out to disk, and play it through the sox utility with something like this:

play --endian big -c 1 -e float -r 22050 -b 32 samples.raw

To write out data, we’ll use a DataOutputStream. This class can write binary data. Instead of writing the float 1.23456 as seven chars, it writes the raw 4-byte representation. This keeps files smaller, but makes them not human readable.

Code

Sounder.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

Haiku

Dear Santa lastname
Please bring lots of boxed numbers
i has been real good