Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSIS 110 Lecture 28: Multi-dimensional Arrays and Nested For Loops - Prof. Brian F. Hanks, Study notes of Javascript programming

In this lecture, students are introduced to multi-dimensional arrays and nested for loops in java. The lecture covers the concept of 2-dimensional arrays, their organization in memory, and how to access their elements using two indices. Students are also encouraged to think of real-life applications of 2-dimensional arrays, such as gameboards, spreadsheets, and maps. The lecture includes examples, exercises, and coding activities to help students understand the concepts.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-8vo-1
koofers-user-8vo-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 28
Reminders
Program due Friday. Any questions about the current programming assignment?
Quiz: Wednesday, March 29
More Nested For Loops
Let's do one more triangle that is a bit more complicated - Try to write method that prints
triangles like this:
*
***
*****
*******
Multi-dimensional arrays
We have looked at one-dimensional arrays. Java also has multidimensional arrays.
int[] quizScores = new int[20];
ChessPiece[][] chessboard = new ChessPiece[8][8];
double [][][] temperature = new double[500][500][100];
We're going to look at 2-dimensional arrays. They are the most commonly used type of
multi-dimensional array.
A typical way of thinking about 2D arrays is as a set of rows and columns. The
chessboard example has 8 rows and 8 columns. Other examples
String[][] words = new String[10][4];
double[][] measurements = new double[3][5];
First dimension is the number of rows, second dimension is number of columns.
Can anyone think of other 2-dimensional items that we could model using a 2D array?
- gameboards
- spreadsheet
- map coordinates
- matrices
- computer displays
- photographs/images
Accessing the elements
pf3
pf4

Partial preview of the text

Download CSIS 110 Lecture 28: Multi-dimensional Arrays and Nested For Loops - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 28

Reminders Program due Friday. Any questions about the current programming assignment? Quiz: Wednesday, March 29 More Nested For Loops Let's do one more triangle that is a bit more complicated - Try to write method that prints triangles like this:




Multi-dimensional arrays We have looked at one-dimensional arrays. Java also has multidimensional arrays. int[] quizScores = new int[20]; ChessPiece[][] chessboard = new ChessPiece[8][8]; double [][][] temperature = new double[500][500][100]; We're going to look at 2-dimensional arrays. They are the most commonly used type of multi-dimensional array. A typical way of thinking about 2D arrays is as a set of rows and columns. The chessboard example has 8 rows and 8 columns. Other examples String[][] words = new String[10][4]; double[][] measurements = new double[3][5]; First dimension is the number of rows, second dimension is number of columns. Can anyone think of other 2-dimensional items that we could model using a 2D array?

  • gameboards
  • spreadsheet
  • map coordinates
  • matrices
  • computer displays
  • photographs/images Accessing the elements

To access an element of a 2D array, you need to use two indices. words[1][3] measurements[0][2] Let's look at this array: int[][] anArray = new int[3][4]; Draw picture of array elements in rows and columns. Identify each element by its indices. Stress that this is not how the array is actually laid out in memory. So, we can access or assign values to any element using its indices anArray[2][2] = 7; Array Organization The row and column model is a nice way for us to think about arrays – however, they are not stored in memory that way. Let's look at what is really going on. Let's look at our earlier declaration: int[][] anArray = new int[3][4]; What is anArray? It's a reference [Draw pic]. But what is it a reference to? Well, it is an array type. How many elements does it have? What is the type of each element? [Draw pic] anArray is a reference to an array with 3 elements. Each element is in turn an array of int. That is, each element in turn references an array with 4 elements that are each ints. [Draw indices in the pic] So, when you reference an array element such as anArray[1][2], you are really referencing anArray[1] and then element 2 of that array. Using loops to access the array elements In a 1D array, we use the array's length in a for loop to access each element of an array. [Give an example] What do you think anArray.length means? What is the value of anArray.length? [3] Let's say that we wanted to print the contents of a 2D array. How would we do it?

Lab Exercise Modify the TicTacToe game

  1. In class TicTacToe, modify the method set() so that it returns false if i or j is < 0 or > 2.
  2. Modify the method getWinner in class TicTacToe. This method should return 'x', 'o', or ' ' depending on whether X, O, or no one is the winner. Remember, a player wins if they have three-in-a-row in a row, a column, or a diagonal.
  3. Modify the TicTacToeGame class a. Change the while loop so that it stops iterating when either X or O is a winner. b. Print a message (after the while loop) that says "x is the winner", "o is the winner", or "no one wins"