Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
L.O. Today you will learn how to play chess. How to Play Chess.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
Prepared by : Walaa Maqdasawi Razan Jararah Supervised by: Dr. Aladdin Masri.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Chess By Kezia Farley.
Chess By Kyle Fischer. What is chess? Chess is a game that you try to get the other person’s king in a checkmate.
A game based off of the esteemed classic By: Tadziu Kosiara.
Each piece is represented by a symbol. The pieces all stand in the same position at the start of the game the pieces are the Rook, the Knight, the Bishop,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Arrays.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Some counting questions with the Rules of Chess. Chessboard and Chess pieces The game of chess is played on an 8-by-8 grid of alternately colored squares.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Lecture 8: 2D Arrays and Nested Loops
Sit-In Lab 1 Ob-CHESS-ion
Week 9 - Monday CS 121.
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
Multidimensional Arrays
Rules to play chess. Chess is a played with 16 pieces: 8 pawns, 2 towers, 2 knights, 2 bishops, 1 queen and 1 king. Movements: Pawns: They only can move.
Presentation transcript:

Chapter 7 Multidimensional Arrays

Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate For example: int[][] chessboard; To allocate space for the array, use new: chessboard = new int[8][8]; Or int[][] chessboard = new int[8][8];

Accessing elements int king = 10; chessboard[0][4] = king; int piece = chessboard[7][3]; Each dimension must use a pair of brackets around the index [] Do not try chessboard[7, 3] notation

Initializing multiple dimensions

What’s an array? A two-dimensional array is actually a one- dimensional array where each element is a one-dimensiona array An “array of arrays” is an important concept! Each array has its own length property Array lengths can be different (ragged array)

The length property in action

Rectangular versus ragged arrays A rectangular array has the same number of elements in each array for a given dimension int[][] myArray = new int[5][7]; myArray has 5 x 7 = 35 elements, with 7 elements in each of the 5 rows A ragged array has varying numbers of elements in a given dimension, as illustrated on the next slide:

Ragged array definition

The contents of a ragged array

Another array initialization example

Passing a two-dimensional array as an argument to a method

Multiple Dimensional Arrays An array of arrays of arrays of…. Each dimension can consist of rectangular or ragged arrays Each dimension is enclosed with a pair of brackets Maximum number of dimensions is nominally 255 for the VM*

Three-dimensional ragged array

Lab 2 Write a program to create a chessboard stored as a two-dimensional character array Chess is played on an 8 x 8 square board with a total of 64 squares Empty squares can be represented by a space Abbreviate pieces as ‘R’ = rook, ‘N’ = knight, ‘B’ = Bishop, ‘Q’ = queen, ‘K’ = king, ‘P’ = pawn Initialize the array according to the diagram

What the array dimensions mean The columns will be the first array dimension Label columns ‘a’ – ‘h’ Column a is the 0 index, h is the 7 index The rows will be the second array dimension Number rows 1-8 Row 1 is the 0 index, 8 is the 7 index

What to do and turn in Write a method that creates a two- dimensional character array, initializes it with the starting chessboard as shown in the diagram, and returns the array Write a method that prints the chess board contents and the row/column labels Turn in a screen shot of the chess board Turn in a print-out of your program

What the chessboard looks like:

Extra Credit (10%) Write a user interface that makes a move The notation is the letter/number of the start location and the letter/number of the end location (e.g. “d1 g4” would move the white queen diagonally 3 squares) Allow any move (don’t check legality) Print an updated view of the board showing the pieces after the move