Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

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.
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
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.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
©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.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle.
Multiple-Subscripted Array
Building Java Programs Chapter 7.5
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
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.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Java Programming: From Problem Analysis to Program Design, 4e
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Two –Dimensional Arrays Mrs. C. Furman September 18, 2008.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved Arrays of Greater Dimension One-dimensional arrays are linear containers. Multi-dimensional Arrays.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Arrays. Arrays are objects that help us organize large amounts of information.
CS 201 Tarik Booker California State University, Los Angeles.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Chapter 8 Multidimensional Arrays
Two-Dimensional Arrays
Chapter 7 Multidimensional Arrays
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Case Study 2 – Marking a Multiple-choice Test
Chapter 7 Multidimensional Arrays
The Matrix A b a d e a a a a a a a A b a d e a a a a a a a
Lecture 13: Two-Dimensional Arrays
INC 161 , CPE 100 Computer Programming
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Presentation transcript:

Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int i = 1; i <= values.length; i++) { if(i < values.length && values[i] == values[i-1]) { runLength++; } else { if(runLength > maxRunLength) { maxRunStart = runStart; maxRunLength = runLength; } runStart = i; runLength = 1; } } return maxRunStart; }

2D Arrays Two-Dimensional Arrays A two-dimensional array is a table/matrix -- with rows and columns -- where each row is an array, and there are several rows in a table. e.g. a two-dimensional array of 3 rows and 4 columns

Create 2D Array To create a two-dimensional array, you specify the number of rows (first) and columns (second). int[][] iMat = new int[3][4]; // integer matrix of 3 rows, 4 columns double[][] dMat = new double[5][3]; // double matrix of 5 rows, 3 columns char[][] cMat = new char[3][3]; // char matrix -- 3 rows, 3 columns

Direct Initialization To give initial values for the elements in a two- dimensional array, you can of course set up a loop. Another way is to provide the initial values directly in declaration. int[][] iMat = { {12, 48, 69, 7}, {5, 16, 27, 30}, {51, 3, 72, 9} };

Access an element in 2D Array Each row is an individual array Column index starts at 0 To access an element in a two- dimensional array, you specify the row index (first) and column index (second). System.out.println(iMat[0][2]); // prints an element at row 0, column 2 (69 in the example above) iMat[2][2] = 5; // assign 5 to row 2, column 1

Length 2D array Lengths of a Two-dimensional Array. Arrays can contain different number of column elements at each row | 12 | 48 | 69 | 7 | | 5 | 16 | 27 | | | 51 | 3 | | LENGTH OF ROW AND COLUMN The Row length (number of rows in the array) is found by using length. int numRow = mat.length; Array to the side has 3 rows The Column Length is different for each row so you must access the row first then the column length. int columnLength = mat[0].length // equal 4 int columnLength = mat[1].length // equal 3

Traversing Over a 2D Array with for loop Then we can utilize those two lengths to traverse over a two-dimensional array -- also by using a nested loop. Two for loops: First specify the row to loop through and then the column. Use Variables row and col to represent the row and column. int row, col; for (row = 0; row < matrix.length; row++) { for (col = 0; col < matrix[row].length; col++) System.out.print[matrix[row][col] + “ “ ); } System.out.println(); } int[][] iMat = { {12, 48, 69, 7}, {5, 16, 27, 30}, {51, 3, 72, 9} };

For Wednesday Finish the 2 worksheets on arrays Look over notes Write the algorithms for the program