Two Dimensional Arrays

Similar documents
Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Two-Dimensional Arrays

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

final int CHICAGO = 0; final int BOSTON = 1; final int MIAMI = 4;

Declaring and ini,alizing 2D arrays

I101/B100 Problem Solving with Computers

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

AP Computer Science Lists The Array type

Lab Activity #7 2D Arrays and File Streams

Computer Programming: C++

Haskell Refresher Informatics 2D

(9-1) Arrays IV Parallel and H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (March 5, 2018) Washington State University

Chapter 7 Multidimensional Arrays

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Chapter 9 Introduction to Arrays. Fundamentals of Java

Tic Tac Toe Game! Day 8

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

Arrays in Java Multi-dimensional Arrays

CMSC131. Seating Chart

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Creating Two-dimensional Arrays

Lesson 35..Two-Dimensional Arrays

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Spring 2010 Java Programming

Declaring a 2D Array

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Chapter 6 Single-dimensional Arrays

BOREDGAMES Language for Board Games

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

Lecture 13: Two- Dimensional Arrays

CS159 Midterm #1 Review

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Introduction to VBA for Excel-Tutorial 7. The syntax to declare an array starts by using the Dim statement, such that:


Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Object Oriented Programming. Java-Lecture 6 - Arrays

Computer Programming for Engineering Applica4ons. Intro to Programming 10/22/13 1 ECE 175. Mul4- dimensional Arrays

Instructor: Eng.Omar Al-Nahal

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Introduction to Object-Oriented Programming

CSci 1113 Lab Exercise 6 (Week 7): Arrays & Strings

Last Class. More on loops break continue A bit on arrays

Arrays. Eng. Mohammed Abdualal

Module 6: Array in C

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

CS453 Arrays in Java in general

Lab #10 Multi-dimensional Arrays

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

Array Basics: Outline

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

GridLang: Grid Based Game Development Language. Programming Language and Translators - Spring 2017 Prof. Stephen Edwards

2D Arrays. Lecture 25

CS159. Nathan Sprague. September 11, 2015

Computer Science & Engineering 150A Problem Solving Using Computers

CSC 108. Prof. Yue-Ling Wong. Lab 6: Tic-tac-toe Game

Teaching guide: Data structures (Twodimensional

1 Undirected Vertex Geography UVG

Mini-Lecture 16. Nested Lists

Functionally Modular. Self-Review Questions

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Lecture 10: for, do, and switch

Lecture 21. Chapter 12 More Python Containers

15. Multidimensional Arrays

Java Programming: Program Design Including Data Structures. Chapter Objectives

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

What are the differences between the Set interface and the List interface?

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Questions, Projects, and Labs

Projects for Advanced Functional Programming 2015

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

13. Section 9 Exercises

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Collections

How to declare an array in C?

COMP1917: 09 Arrays and Strings

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Transcription:

+ Two Dimensional Arrays

+ Two Dimensional Arrays So far we have studied how to store linear collections of data using a single dimensional array. However, the data associated with certain systems (a digital image, a board game, etc.) lives in two dimensions. To represent this data in our programs, we need a multi-dimensional data structure, that is, a multidimensional array.

+ Two Dimensional Arrays Chicago Boston New York Chicago 0 983 787 Boston 983 0 214 New York 787 214 0

+ Declaring Two Dimensional Arrays You can declare a two dimensions array using almost the same syntax you would use to declare a single dimensional array. For example: int[][] mylist = new int[5][5]; This would create a 5 x 5 matrix of integers, all defaulted to a zero value upon creation. We generally think of the first number as the number of rows in your array and the second as the number of columns

+ Declaring Two Dimensional Arrays

+ Declaring Two Dimensional Arrays You can also create a two dimensional array with pre-specified values by using almost the same syntax as a single dimensional array. For example: int[][] list = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; See Creating2DArrays.java

+ Accessing Two Dimensional Arrays You can access the two dimensional array values using the same syntax as you would with a single dimensional array. For example, the first element in the first row is at position: mylist[0][0]

+ Accessing Two Dimensional Arrays

+ Accessing Two Dimensional Arrays

+ Accessing Two Dimensional Arrays

+ Accessing Two Dimensional Arrays See OffsetsIn2DArrays.java

+ Arrays of Arrays Two dimensional arrays are really just one dimensional arrays that have been chained together. For example: int[][] list = new int[5][5]; Is a list of 5 elements. Each of those elements, however, references another single dimensional array, each of which is 5 elements long as well. Moreover, a two-dimensional array is really nothing more than an array of arrays.

+ Arrays of Arrays

+ Arrays of Arrays

+ Arrays of Arrays

+ Arrays of Arrays

+ Arrays of Arrays

+ Arrays of Arrays

+ Getting the Dimension Lengths The length of your main array in a 2 dimensional array can be obtained by referencing the following. We usually refer to this as the number of rows in the array list.length The length of each sub-array in a two dimensional array can be obtained by referencing the following. We usually refer to this number as the columns in the array. list[element].length Note that each row in a two dimensional array could have a different number of columns. We sometimes refer to these kinds of arrays as ragged arrays

+ Looping Through 2D Arrays In order to iterate over all elements in a two dimensional array you will need to maintain two indexes one for the row and one for the column This is usually done by setting up a nested for loop like this: for (int row = 0; row < list.length; row++) { for (int col = 0; col < list[row].length; col++) { // statements // list[row][col] = } }

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays

+ Looping Through 2D Arrays See LoopingThrough2DArrays.java

+ Printing Two Dimensional Arrays Just as with single dimensional arrays, you cannot directly print a two dimensional array by using its variable name. int[][] list = { {1,2,3}, {4,5,6} }; System.out.println(list); // prints gobbledygook, useful to no one Two dimensional arrays are reference types, so printing out the variable name associated with an array will only print out some type information followed but the hash code Therefore you will need to set up a nested for loop in order to access and print each element in your array. See Printing2DArrays.java

+ Summing Two Dimensional Arrays You can sum up all elements in an array by establishing a sum accumulator variable outside of the array and then accessing that variable as you visit each element. For example: // set up a running sum int sum = 0; // iterate over the array for (int row = 0; row < list.length; row++) { for (int col = 0; col < list[row].length; col++) { sum += list[row][col]; } } See Summing2DArrays.java

+ Programming Exercise Write the game Tic Tac Toe Players begin with an empty board of 9 cells Players can elect to place their token (X or O) in a cell. If the cell is not taken the token is assigned to that cell. Players take turns until either (a) all cells are occupied or (b) one player s token is found to occupy all three cells along any row, column or diagonal