Lecture 8: 2D Arrays and Nested Loops

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Arrays.
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
CS305j Introduction to Computing Two Dimensional Arrays 1 Topic 22 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
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.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Two dimensional arrays.
Two-Dimensional Arrays
multi-dimensional arrays
Two Dimensional Arrays
Two-dimensional arrays
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
EGR 115 Introduction to Computing for Engineers
MULTI-DIMENSIONAL ARRAY
ECE Application Programming
Two-Dimensional Arrays
Engineering Problem Solving with C++, Etter/Ingber
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Review of 2 dimension arrays.
Nested Loop Review and Two-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Topic 26 Two Dimensional Arrays
Introduction to Matrices
Engr 0012 (04-1) LecNotes
Introduction To Programming Information Technology , 1’st Semester
JavaScript Arrays.
Multidimensional Arrays
Announcements & review
Chapter 7 Part 2 Edited by JJ Shepherd
2.2 Introduction to Matrices
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional array
Multidimensional Arrays
CS2011 Introduction to Programming I Multidimensional Arrays
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
Loops and Arrays in JavaScript
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
Multi-Dimensional Arrays
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Multidimensional Arrays Section 6.4
C++ Array 1.
Arrays and Matrices Prof. Abdul Hameed.
Dr. Khizar Hayat Associate Prof. of Computer Science
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

Lecture 8: 2D Arrays and Nested Loops Yoni Fridman 7/10/01

Outline Two-dimensional arrays Creating 2D arrays Using 2D arrays How 2D arrays are stored Examples

Two-dimensional Arrays Arrays in Java can have any number of dimensions. Yesterday we looked at one-dimensional arrays. Today we’ll look at two-dimensional arrays. 7 2 8 9 5 4 3 1 6

Creating 2D Arrays 2D arrays are declared just like 1D arrays, but with two pairs of square brackets: int[][] myGrid; Space is allocated for 2D arrays just like for 1D arrays, but again with two pairs of square brackets: myGrid = new int[rows][cols]; So this declares and allocates a 4 x 9 array, like shown in the last slide, but full of zeros. int[][] myGrid; myGrid = new int[4][9]; OR int[][] myGrid = new int[4][9];

Using 2D Arrays To access the highlighted array element, for example, we write myGrid[2][6]. What will this example do? columns 1 2 3 4 5 6 7 8 rows 1 2 3 7 2 8 9 5 4 3 1 6 3 myGrid[2][1] = 5; System.out.println(myGrid[2][0] * myGrid[2][1]);

How 2D Arrays Are Stored It’s intuitive to imagine a 2D array as a table, but Java actually stores it as an array of arrays. So myGrid.length == 4. And myGrid[0].length == myGrid[1].length == … == 9. myGrid 1 2 3 7 2 8 9 5 4 3 1 6 7 2 8 9 5 4 3 1 8 6 2 5 3 9 5 6 8 1 2 6 9 5 4

Examples Nested loop example: Displaying a pyramid. 2D array example: Summing a table.

Homework Read: 1.7, 2.3 (middle of p. 34 to middle of p. 36), 2.11, and on-line handout. Remember: HW3 due tomorrow at the beginning of class.