More Dimensions
> Click or hit Control-Enter to run the code above
Next Few Weeks
-
MP2 comes out today, due a week from today.
-
Next week is our first midterm on imperative programming and concepts.
-
We will continue homework during the midterm as we begin to talk about object-oriented programming.
-
However, we will wait to release MP3 until the midterm is over.
Q1 Review: Scope
int first = 5;
if (first > 4) {
int second = 4;
}
second++;
What is wrong with this code?
Q1 Review: Assignment v. Equality
if (i = 5) {
int j = 0;
j = 5;
}
What is wrong with this code?
Q1 Review: Conventions
Why is the numeric value of the ASCII character 'k' 107?
Arrays Can Have Multiple Dimensions
// This is a single-dimensional array of ints of size 4
int[] samples = new int[4];
// This is a two-dimensional array of of chars of
// size 4 in the first dimension and 8 in the second
char[][] board = new char[4][8];
// This is a three-dimensional array of of doubles of
// size 6 in the first dimension, 8 in the second,
// and 10 in the third
double[][][] temperature = new double[6][8][10];
Understanding Multi-Dimensional Arrays
// This is a two-dimensional array
int[][] samples = new int[4][8];
// This is a one-dimensional array
int[] samplesSlice = samples[0];
> Click or hit Control-Enter to run the code above
Multi-Dimensional Array Initialization
We can also initialize multi-dimensional arrays:
int[][] measurements = {
{ 1, 2 },
{ 3, 4 }
};
// This is equivalent to
int measurements = new int[2][2];
measurements[0][0] = 1;
measurements[0][1] = 2;
measurements[1][0] = 3;
measurements[1][1] = 4;
(The ordering can be confusing, but you won’t see this often.)
> Click or hit Control-Enter to run the code above
Forget About Rows and Columns
(These aren’t spreadsheets—you’re not in business school.)
You’re In Charge
The first, second, third, or Nth index of an array mean whatever you want.
This is particularly important when representing certain kinds of data using multi-dimensional arrays.
Multi-Dimensional Data
What kind of data would work well in a multi-dimensional array?
Multi-Dimensional Data
What kind of data would work well in a multi-dimensional array?
-
Pictures and images: each pixel is stored in a 2-dimensional grid
-
Higher-dimensional data: our world is 3D—4D, actually—so our data should be as well.
-
Sound: even data that might seem one dimensional often turns out to be more complicated than we expected
Multi-Level Loops
A frequent use of nested for
loops is to iterate over multi-dimensional
arrays:
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
> Click or hit Control-Enter to run the code above
String Rotation Master Class
Let’s look at some ways to do HW13.
-
Note that all of these passed our test suite.
-
But that doesn’t mean that they are all correct.
-
I’m not going to post these as part of the slide deck to avoid disseminating potentially bad solutions
-
Is this your code? Maybe. Maybe not though. There are a lot of students in the course.
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
Announcements
-
MP1 is due today at 5PM. Office hours all day today—please come if you need help!
-
MP2 will be out tonight and due a week from today.
-
Then the first midterm, then MP3.