Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Similar documents
Two Dimensional Arrays

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

15. Multidimensional Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays

Arrays. Eng. Mohammed Abdualal

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

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

Chapter 7 Multidimensional Arrays

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Chapter 12 Two-Dimensional Arrays

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

Lecture 13: Two- Dimensional Arrays

Lab #10 Multi-dimensional Arrays

Introduction to Object-Oriented Programming

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

LAB 12: ARRAYS (ONE DIMINSION)

Declaring Array Variable

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

Declaring and ini,alizing 2D arrays

Chapter 8 Multi-Dimensional Arrays

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.

Instructor: Eng.Omar Al-Nahal

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

Chapter 4: Control structures. Repetition

Chapter Nine Arrays. Sixth Edition

Lecture Multidimensional Arrays and the ArrayList Class. Dr. Martin O Connor CA166

Supplement: Case Study: Sudoku. For Introduction to Java Programming By Y. Daniel Liang

I101/B100 Problem Solving with Computers

Chapter 4: Control structures

Spring 2010 Java Programming

ARRAYS and ARRAYLISTS

Declaring a 2D Array

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

How to declare an array in C?

AP Computer Science Lists The Array type

More non-primitive types Lesson 06

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Array. Prepared By - Rifat Shahriyar

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

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

9/19/2018 Programming Data Structures. Polymorphism And Abstract

Chapter 8 Multidimensional Arrays

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Principles of Programming. Chapter 6: Arrays

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

Computer Science II (20082) Week 1: Review and Inheritance

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

BITG 1113: Array (Part 1) LECTURE 8

Two-Dimensional Arrays

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

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

Exercises Software Development I. 06 Arrays. Declaration, Initialization, Usage // Multi-dimensional Arrays. November 14, 2012

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 10 INTRODUCTION TO ARRAYS

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

ENGR 1181 MATLAB 09: For Loops 2

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

Arrays in Java Multi-dimensional Arrays

Introduction to Java & Fundamental Data Types

Module 6: Array in C

Chapter 6 Arrays, Part D 2d Arrays and the Arrays Class

Exercises with switch

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

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

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Object Oriented Programming. Java-Lecture 1

CMSC131. Arrays: The Concept

Give one example where you might wish to use a three dimensional array

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

CMSC131. Seating Chart

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

C library = Header files + Reserved words + main method

APCS Semester #1 Final Exam Practice Problems

UNIT-I Fundamental Notations

Chapter 7 Array. Array. C++, How to Program

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

Chapter 9 Introduction to Arrays. Fundamentals of Java

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

CS115 Principles of Computer Science

AP Computer Science Homework Set 5 2D Arrays

Computer Programming: C++

Homework # 4. Example: Age in years. Answer: Discrete, quantitative, ratio. a) Year that an event happened, e.g., 1917, 1950, 2000.

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

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

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

Control Structures in Java if-else and switch

2.8. Decision Making: Equality and Relational Operators

Lab Activity #7 2D Arrays and File Streams

Arrays and ArrayLists. Ananda Gunawardena

Chapter 6. Data Types

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

CO Java SE 8: Fundamentals

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Transcription:

Arrays Chapter 7 Part 3 Multi-Dimensional Arrays Chapter 7: Arrays Slide # 1

Agenda Array Dimensions Multi-dimensional arrays Basics Printing elements Chapter 7: Arrays Slide # 2

First Some Video Tutorials Matthew Guarnotta: 2D arrays in Java (9:36) CompSci Studio: Java Beginner Programming Tutorial 34 2D Arrays (6:43) Java Beginner Programming Tutorial 35 2D Array Grid (7:46) in28minutes: Java 2d array tutorial (9:37) MargretPosch Java Basics 2D Arrays (14:39) 2D + Jagged arrays + Arrays class + tostring to print elements Jose Vidal: Java Tic Tac Toe Board Program: 2D Array: Tutorial (8:38) Advanced topics: Uses tostring() method, for each loops, constructors, & enum Chapter 7: Arrays Slide # 3

Array Dimensions The arrays we discussed so far are called one-dimensional arrays Syntax review: 1. Declaring and then creating memory datatype[ ] arrayname; arrayname = new datatype[arraysize]; 2. Declaring and creating memory in one step datatype[ ] = new datatype[arraysize]; Chapter 7: Arrays Slide # 4

Array Dimensions An array that has more than one index is called multi-dimensional arrays An array that has two indexes is called twodimensional arrays (2D-Arrays or matrix) A 2D-array can be displayed as a twodimensional table 2D arrays are commonly used in financial and scientific applications Chapter 7: Arrays Slide # 5

2D Array Declaration Syntax : 1. Declaring and then creating memory datatype[][] arrayname; arrayname = new datatype[rowsize][columnsize]; 2. Declaring and creating memory in one step datatype[][] arrayname = new datatype[rowsize][columnsize]; Chapter 7: Arrays Slide # 6

2D Arrays Example Assume you want to keep the medal count for a specific Olympics How would you create a 2D array that resembles this? Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Chapter 7: Arrays Slide # 7

2D Arrays Example Array declaration int[][] medals = new int[5][3]; or Array initialization during declaration int[][] medals = { {1,1,0}, {1,0,1}, Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 } {1,2,0}, {2,1,1}, {3,2,1} Chapter 7: Arrays Slide # 8

2D Arrays Example 0 1 2 Gold Silver Bronze 0 1 2 3 4 Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Accessing array elements How many silver medals did Germany get? System.out.printf( Germany s medals: %d, medals[2][1]); Change the number of gold medals for Canada to be 2 medals [0][0] = 2; Chapter 7: Arrays Slide # 9

2D Arrays Example 0 1 2 Gold Silver Bronze 0 1 2 3 4 Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 To get the number of rows & columns, use the length instance variable To return the number of rows System.out.print(medals.length); To return the number of columns System.out.print(medals[anyRow].length); Chapter 7: Arrays Slide # 10

2D Arrays Example Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Chapter 7: Arrays Slide # 11

2D Arrays Exercise Modify the code as follows: Add two, one-dimensional arrays to save the countries and medal names Display the countries to the left and the medal names at the top of the array contents The output should look like the figure Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Chapter 7: Arrays Slide # 12

2D Arrays Exercise How will this array be populated? int my2darray [][] = new int [5][3]; for (int row = 0; row < my2darray.length; ++row) for (int col = 0; col < my2darray[row].length; ++col) my2darray[row][col] = row + col; Chapter 7: Arrays Slide # 13

2D Arrays Exercise How will this array be populated? int my2darray [][] = new int [5][3]; for (int row = 0; row < my2darray.length; ++row) for (int col = 0; col < my2darray[row].length; ++col) my2darray[row][col] = row + col; 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 14

Printing a 2D Arrays for (int row = 0; row < my2darray.length; ++row) { for (int col = 0; col < my2darray[row].length; ++col) System.out.print(my2DArray[row][col]+ \t ); System.out.println(); } 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 15

Printing a 2D Arrays Exercise: What is printed if we switch the two for statements? What must be changed in the code? 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 16

Printing a 2D Arrays Exercise: What is printed if we switch the two for statements? What must be changed in the code? 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 17

Printing a 2D Arrays Exercise: What is printed if we switch the two for statements? for (int col = 0; col < my2darray[1].length; ++col) { for (int row = 0; row < my2darray.length; ++row) System.out.print(my2DArray[row][col] + "\t"); System.out.println(); } 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 18

Printing a 2D Arrays Example: Add rows contents for (int row = 0; row < my2darray.length; ++row) { int sum = 0; for (int col = 0; col < my2darray[row].length; ++col) sum += my2darray[row][col]; System.out.println( Row + row + : + sum); } 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 19

More Array Dimensions You can define three-dimensional arrays or n- dimensional array (can be any number). Syntax: datatype[]...[] arrayname = new datatype[idx 1 ]...[idx n ]; Chapter 7: Arrays Slide # 20

More Array Dimensions Example: An array of grades, where there are 40 students, all taking the same 3 courses, and each course has 10 quizzes int [][][] grades = new int[4][3][2]; Total # element = 4 x 3 x 2 = 24 Read and fill the array of grades What could be a better names for the indices? int [][][] grades = new int[4][3][2]; // Read from the user for (int i = 0; i < 4; ++i) for (int j = 0; j < 3; ++j) for (int k = 0; k < 2; ++k) grades[i][j][k] = input.nextint(); Chapter 7: Arrays Slide # 21

3-D Array Exercise Create a program that will enter the values and displays the contents of the 3-D array. Sample Output: Chapter 7: Arrays Slide # 22

Review: Multidimensional-Array Basics Consider this table of values Chapter 7: Arrays Slide # 23

Review: Multidimensional-Array Basics We specify the row and column indices for the array to access a specific element table[3][2] has a value of 1262 Chapter 7: Arrays Slide # 24

Review: Multidimensional-Array Basics We can access elements of the table with a nested for loop Example: Chapter 7: Arrays Slide # 25

Multidimensional- Array Example Sample screen output Chapter 7: Arrays Slide # 26

Summary Multidimensional arrays are implemented as an array of arrays Treat two-dimensional array as a table with rows and columns Chapter 7: Arrays Slide # 27