Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.

Similar presentations


Presentation on theme: "2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet."— Presentation transcript:

1 2 dimensional arrays Steven Wood ©2005

2 Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet

3 3D example Can have more than 2 dimensions!

4 How to think of a 2d array A 2-D array is an array whose components are themselves arrays

5 Creating an 2d array A two-dimensional array is an array of arrays. In this example: The first index is the 12 months, indexed from 0 to 11. Each month array is an array of 31 days, indexed from 0 to 30 double rainfall[][] = new double[12][31]; Month index Day index

6 Using initializer lists int myTable[][] = { {23, 45, 65, 34, 21, 67, 78}, {46, 14, 18, 46, 98, 63, 88}, {98, 81, 64, 90, 21, 14, 23}, {54, 43, 55, 76, 22, 43, 33} };

7 Setting each value individually int myTable[][]; myTable[0,0] = 23; myTable[0,1] = 45; … myTable[3,5] = 43; myTable[3,6] = 33;

8 Example Track daily temperature for 4 weeks double[][] weektemp = new double[4][7];

9 Finding # of rows (weeks) to determine the number of rows in a 2D array, use length int numWeeks = weektemp.length; //4

10 Finding # of columns (days) to determine the number of columns in a 2D array, find length of first row int numDays = weektemp[0].length; // 7 Side note: a 2d array does not have to be rectangular, but we won’t look at this situation

11 Our example Create a computized mark book for a teacher Assume only 3 students and 3 marks for each Assume all marks have same weighting (e.g. all percents) MarkBook - String theClass[][] - String name +MarkBook(String name, int students, int marks) +initClass() +displayClass()

12 A model of the information 01234 0 1Thomas438084 2Robert727666 3Katie759080 4 Store student average in these Class average Name + 3 marks + Average  5 columns blank +3 students+ average  5 columns

13 Sample runs…

14 Do this #1… Download the program MarkBook.java Find the constructor Find the line that declares & allocates space for the array It needs to be completed How big should the array be (look at the previous slide) Make sure that you use variables to set the size!

15 Do this #2… Find the initClass() method Use the picture of the array contents from 2 slides previous Remove the comment marks and complete the assignment statements that fill the array with values. E.g. //theClass[2][0] = theClass[2][0] = “Robert”; Etc.

16 Example of using a loops int[][] array = new int[10][5]; // print array in rectangular form for (int r=0; r<array.length; r++) { //# of rows // print columns for each row for (int c=0; c<array[r].length; c++) { System.out.print(" " + array[r][c]); } System.out.println(""); }

17 Do this #3… In MarkBook.java find the displayClass method Use the information on the last slide to complete the method so that it displays the contents of the array Update the main method so that you use the displayClass method to print out the array!

18 Finding a student’s average Which student? (Which row?) set a variable Add up all the marks (total) All the columns (use a loop!) Hint: the inside loop for printing… Find the average (total / # of marks) Put in proper array spot (Row? Column?) use variables not “1, 4”

19 Do this #4… Complete the method that will find the average for a student (to 1 decimal) Place the average in the proper spot in the array Make sure you update the main method to use this method!

20 Finding a test average Which test? (Which column?) set a variable Add up all the marks (total) All the rows (use a loop!) Hint: the outside loop for printing… Find the average (total / # of marks) Put in proper array spot (Row? Column?) use variables not “4, 1”

21 Do this #5… Complete the method that will find the average for a test (to 1 decimal) Place the average in the proper spot in the array Make sure that you update the main method to use this method

22 Do this #6… Add a method that will find the class average It may depend on the student averages being calculated already Place it in the array

23 Do this #7… Improve the initClass method so that the values can be entered in from the keyboard Maybe the entire class doesn’t have to be entered at one time  more realistic!

24 Do this #8… Add a method that will allow you to change/update a student’s information You maybe should then recalculate the averages (test, student, class) Add a method to delete a student Add a method to delete a test

25 Sources Text http://www.leepoint.net/notes-java/data/arrays/30arrays.html http://livedocs.macromedia.com/coldfusion/7/htmldocs/images/array2_3.jpg www.cs.brandeis.edu/.../cs11a/arrays/multi.html http://s91589888.onlinehome.us/sgc/g05.gif


Download ppt "2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet."

Similar presentations


Ads by Google