Nested Loop Review and Two-Dimensional Arrays

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Advertisements

CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Arrays part 3 Multidimensional arrays, odds & ends.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
More Arrays Length, constants, and arrays of arrays By Greg Butler.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Java Unit 9: Arrays Declaring and Processing Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Chapter 9 Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Lecture 18: Nested Loops and Two-Dimensional Arrays
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Two-Dimensional Arrays
Chapter 8 – Arrays and Array Lists
Topic Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
EKT120 : Computer Programming
CSC 142 Computer Science II
Arrays Declare the Array of 100 elements Notes
CSS161: Fundamentals of Computing
Yong Choi School of Business CSU, Bakersfield
Engineering Problem Solving with C++, Etter/Ingber
The for-loop and Nested loops
Two-Dimensional Arrays
EKT150 : Computer Programming
int [] scores = new int [10];
Review of Arrays and Pointers
EKT120: Computer Programming
Multidimensional Arrays
More 2 D Array.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Data Structures (CS212D) Week # 2: Arrays.
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
Arrays Week 2.
1D Arrays and Lots of Brackets
INC 161 , CPE 100 Computer Programming
Arrays in Java.
Suggested self-checks:
Chapter 8 Multidimensional Arrays
Outline Declaring and Using Arrays Arrays of Objects
C++ Array 1.
Arrays Introduction to Arrays Reading for this Lecture:
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Nested Loop Review and Two-Dimensional Arrays

Learning Objectives Review nested loops Introduce two-dimensional arrays Describe the use of two-dimensional arrays to represent grids of information

Nested for Loops Nested loops frequently used to process two-dimensional arrays Often body of inner loop is where the main computation is done Example: for (i = 0; i < m; I++) { before inner loop for (j = 0; j < n; j++) body of inner loop after inner loop };

Dependent for Loops Sometimes the extent of the inner nested loop will depend on the index value of the outer loop Dry Run the following: for (i = 0; i < 3; i++) { System.out.print(“i= “; + i + “: j = “); for (j = 0; j <= i; j++) System.out.print(“ “ + j); }

Nested Loop Contained in Other Statements //Dry run #2 for (int i = 1; i <= 10; i++) { if (i % 2 == 0) // i even for (int j = 1; j <= i/2; j++) out.print(“*”); else // i odd for (int k = 1; k <= 5 – i/2; k++) out.print(“#”); out.println(); }

Two-Dimensional Arrays Declaration similar to one dimensional arrays Need to specify both the number of rows and columns during allocation Example: final int COLS = 6, ROWS = 5; //Constants double[][]energyTable = new double[ROWS][COLS];

Computing Row Totals double [] yearTotals = new double[ROWS]; for (int year = 0; year < ROWS; year++) { // compute total for the row year yearTotals[year] = 0.0; for (int column =0; column < COLS; column++) yearTotals[year] = yearTotals[year] + energyTotal[year][column]; }

Populating energyTable int y, s; // reads 30 numbers needed to fill // energyTable one row at a time for (y = 0; y < ROWS; y++) for (s = 0; s < COLS; s++) { System.out.println(“Enter the next value”); energyTable[y][s] = input.nextDouble(); }

Initializing Two-Dimensional Arrays double[][] energyTable = { {18.9, 19.4, 34.2, 3.9, 5.7, 0.3}, {19.1, 19.3, 33.6, 3.0, 6.2, 0.2}, {18.8, 19.6, 32.9, 3.1, 6.6, 0.2}, {18.9, 20.3, 33.5, 2.8, 6.7, 0.2}, {19.6, 20.8, 33.8, 3.1, 6.5, 0.2} };

Arrays of Arrays When we write This is shorthand for energyTable = new double[ROWS][COLS]; This is shorthand for energyTable = new double[ROWS][]; for (int i = 0; i < ROWS; i++) energyTable[i] = new double[COLS];

2-D Arrays: Dimensions In Java, a 2-D array is a 1-D array of 1-D arrays, its rows. Each row is stored in a separate block of consecutive memory locations. If m is a 2-D array, then m[k] is a 1-D array, the k-th row. m.length is the number of rows. m[k].length is the length of the k-th row. Nothing unusual: an array of rows contains objects; each object happens to be an array.

public static void main(String [] args) { final char BLANK = ' public static void main(String [] args) { final char BLANK = '?'; // location empty final char X = 'x'; final char OH = 'o'; final int size = 3; char board[][] = new char[size][size]; for (int i=0; i < size; i++) for (int j=0; j < size; j++) board[i][j] = BLANK; for(int i=0; i< size; i++) board[i][i] = X; for (int j = 0; j< size; j++) board[j][size-j-1] = OH; System.out.print(board[i][j]+" "); System.out.println(); } Dry Run

More Two D Internally, Java stores 2 dimensional arrays as an array of arrays: int [][] nums = new int[5][4]; The above is really equivalent to a 3-step process: // create the single reference nums (yellow square) int [][] nums; // create the array of references (blue squares) nums = new int[5][]; // this create the second level of arrays (red squares) for (int i=0; i < 5 ; i++) nums[i] = new int[4]; // create arrays of integers

Even More 2-D Note: when you initially declare a 2D array: you must always specify the first dimension nums = new int[][]; // ILLEGAL - NEEDS 1ST DIMENSION you do not need to specify the second dimension nums = new int[5][]; // OK nums = new int[5][4]; // OK Elements of the Array: if nums is a 2D array as shown above, nums[i][j] represents a single integer in that array nums[i] represents a 1D array (a single row in the 2D array)

Adding Another Dimension You can create arrays of higher dimension than 2. For example, if we were measuring the temperature in a rectangular volume. int temperature[][][] = new int[10][20][30]; This creates an array of 10x20x30=6000 integers. temperature is an array of array of arrays SubArrays: temperature is a 3D array of size 10x20x30. There is one of these. temperature[i] is a 2D array of size 20x30. There are 10 of these. temperature[i][j] is a 1D array of size 30. There are 200 of these. All but the last dimension must be initially specified: int temperature[][][] = new int[10][10][]; // OK int temperature[][][] = new int[10][][]; // NOT OK

2D Array Magic Square Program A Magic Square is a two-dimensional array of positive integers such that the sum of each row, column and diagonal is the same. Write a program that takes 9 integers as inputs. You decide how the order of input matches the square. The program should determine whether or not the square is a magic square and display the results. Example: Push: Modify your program to check a 4x4 Square. Push: 5x5 Push: N x N Push: Write a program that will have the computer randomly generate a magic square 2 7 6 9 5 1 4 3 8