teaching machines

CS 145 Lecture 30 – Higher Dimensions

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

Agenda

TODO

Note

Last time we looked at managing “growable” arrays with the ArrayList class:

  1: /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 `
' ArrayList<Person> people = new ArrayList<Person>();   2: Random g = new Random();   3: people.add(g.nextPerson());   4: people.add(g.nextPerson());   5: people.add(g.nextPerson());   6: people.add(g.nextPerson());   7: people.add(g.nextPerson());   8: System.out.println(people.size());   9: System.out.println(people.get(1).getName());  10: people.get(4).jump(3);  11: people.get(0).highFive(people.get(3));  12: people.add(people.get(2));  13: people.remove(2);  14:  15: for (int i = 1; i < people.size(); i += 2) {  16: people.get(i).sit();  17: }  18: System.out.println(sum);  19:  20: int sum = 0;  21: for (int i = 0; i < people.size(); ++i) {  22: sum += people.get(i).getCourses(Semester.FALL, 2015).size();  23: }  24: System.out.println(sum);  25:  26: Person winner = people.get(0);  27: for (int i = 1; i < people.size(); ++i) {  28: if (people.get(i).getDateOfBirth().isBefore(winner.getDateOfBirth())) {  29: winner = people.get(i);  30: }  31: }  32: System.out.println(winner.getName());  33:  34: Person tmp = people.get(2);  35: people.set(2, people.get(1));  36: people.set(1, tmp);  37:  38: for (int i = 0; i < people.size(); ++i) {  39: for (int j = 0; j < people.size(); ++j) {  40: if (i != j) {  41: people.get(i).shakeHands(people.get(j));  42: }  43: }  44: }  45:  46: people.clear();[/precode]

Now, we missed a blackbox the other day that grows our idea of arrays into higher dimensions. Let’s have look:

To solve this we need to make arrays in higher dimensions. We saw the general pattern for making an array:

type[] id = new type[n];

The type can be anything, even another array. Suppose we want an array of int arrays? Then we substitute int[] in for type:

int[][] id = new int[outerN][innerN];

We’ll look at two-dimensional arrays through the lens of the birthday problem and generating all prefixes of an array.

Code

Prefices.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 `
'

Bepeats.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

Country, state, city
Street, house number, apartment
2D is nothing