Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class ProjectsProjects 2 dimensional arrays2 dimensional arrays Higher dimensionsHigher.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class ProjectsProjects 2 dimensional arrays2 dimensional arrays Higher dimensionsHigher."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class ProjectsProjects 2 dimensional arrays2 dimensional arrays Higher dimensionsHigher dimensions

2 March 2005 2R. Smith - University of St Thomas - Minnesota Class Projects Something I can ‘run’Something I can ‘run’ Does something visibleDoes something visible –Graphics and/or file processing Class definitions in multiple files (2 or more)Class definitions in multiple files (2 or more) May be solo, or teams of 2 or 3May be solo, or teams of 2 or 3 –One separate file/class per team member Project ideas?Project ideas? –Text-based game –Tax return: maybe just a simple 1040 form –A “complete” payroll implementation Correct taxes, multiple employeesCorrect taxes, multiple employees –House design/costing (extension of the rug problem)

3 March 2005 3R. Smith - University of St Thomas - Minnesota Two dimensional array Two brackets instead of oneTwo brackets instead of one –Rows and columns –Array of 1 dimensional arrays CreatingCreating –New with 2 dimensions –Initializers with nested rows/columns int twod[][] = new int[3][3];int twod[][] = new int[3][3]; –First subscript identifies rows, second columns

4 March 2005 4R. Smith - University of St Thomas - Minnesota Ragged Arrays A 2D array is an array of 1D arraysA 2D array is an array of 1D arrays “Rows” may be filled in separately:“Rows” may be filled in separately: int twod[][] = new int[3][]; We can now create separate rows:We can now create separate rows: twod[0] = new int[6]; twod[1] = new int[5]; twod[2] = new int[4];

5 March 2005 5R. Smith - University of St Thomas - Minnesota Array Length and Raggedness What does twod.length return?What does twod.length return? –It returns the number of rows! –You have to check individual rows to see how long they are There is no general ‘array size’ checkThere is no general ‘array size’ check

6 March 2005 6R. Smith - University of St Thomas - Minnesota Working with 2D arrays Initialize a 2D arrayInitialize a 2D array Write nested FOR loops to print them outWrite nested FOR loops to print them out –What if we swap around the indices? Do a sum of all elementDo a sum of all element

7 March 2005 7R. Smith - University of St Thomas - Minnesota Multiple Dimensions It doesn’t stop at 2 – we can add more dimensionsIt doesn’t stop at 2 – we can add more dimensions Video store example – 3D arrayVideo store example – 3D array –Storage cases –Storage shelves –Index on shelf

8 March 2005 8R. Smith - University of St Thomas - Minnesota Review of Common Errors Chapter 5, page 274Chapter 5, page 274

9 March 2005 9R. Smith - University of St Thomas - Minnesota Sorting – Selection Sort I know a lot of algorithmsI know a lot of algorithms –I never remember array sorting algorithms –The problem with arrays – everything is in a fixed location Selection SortSelection Sort –Start at one end Find the lowest value in the arrayFind the lowest value in the array Swap it with the first elementSwap it with the first element –Repeat the process for the array minus the first element

10 March 2005 10R. Smith - University of St Thomas - Minnesota Selection Sort Example 5 1 8 1212 6 4 8 3

11 March 2005 11R. Smith - University of St Thomas - Minnesota Coding the selection sort Two loopsTwo loops Outer loop looks at subsets of the arrayOuter loop looks at subsets of the array Inner loop does “min” on remaining arrayInner loop does “min” on remaining array

12 March 2005 12R. Smith - University of St Thomas - Minnesota Binary Search Algorithm Searching for a phone number in a bookSearching for a phone number in a book –Option: read every page till you get there What do we really do?What do we really do? How do people search a phone book?How do people search a phone book?

13 March 2005 13R. Smith - University of St Thomas - Minnesota Binary Search Look at element halfway from startLook at element halfway from start –Is the target greater or less than that? –Pick upper or lower half accordingly Repeat on that subset of the arrayRepeat on that subset of the array EfficiencyEfficiency –Related to log of 2

14 March 2005 14R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class ProjectsProjects 2 dimensional arrays2 dimensional arrays Higher dimensionsHigher."

Similar presentations


Ads by Google