1 CS1110. 17 Nov 2011 Ragged arrays Reading for today: sec. 9.3. Reading for next time: Interfaces: Sec. 12.1 and corresponding ProgramLive material. Also,

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

COMP 170 L2 Page 1 L03: Binomial Coefficients l Purpose n Properties of binomial coefficients n Related issues: the Binomial Theorem and labeling.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
COMP 170 L2 Page 1 L03: Binomial Coefficients l Purpose n Properties of binomial coefficients n Related issues: the Binomial Theorem and labeling.
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
The Binomial Theorem.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Notes 9.2 – The Binomial Theorem. I. Alternate Notation A.) Permutations – None B.) Combinations -
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Copyright © 2007 Pearson Education, Inc. Slide 8-1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
5-7: The Binomial Theorem
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
Lesson 6.8A: The Binomial Theorem OBJECTIVES:  To evaluate a binomial coefficient  To expand a binomial raised to a power.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Copyright © Cengage Learning. All rights reserved. 8.4 The Binomial Theorem.
Binomial Theorem & Binomial Expansion
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
The Binomial Theorem. (x + y) 0 Find the patterns: 1 (x + y) 1 x + y (x + y) 2 (x + y) 3 x 3 + 3x 2 y + 3xy 2 + y 3 (x + y) 4 (x + y) 0 (x + y) 1 (x +
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
1 CS Nov 2009 Ragged arrays Reading for today: sec Reading for next time: chapter 16, applications and applets If you have a conflict with.
2-6 Binomial Theorem Factorials
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Section 6.4. Powers of Binomial Expressions Definition: A binomial expression is the sum of two terms, such as x + y. (More generally, these terms can.
Binomial Coefficients and Identities
1 CS April 2010 Ragged arrays Reading for today: sec Reading for next time: chapter 16, applications and applets Prelim 3: mean 69.3, median.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
The Binomial Theorem Section 9.2a!!!. Powers of Binomials Let’s expand (a + b) for n = 0, 1, 2, 3, 4, and 5: n Do you see a pattern with the binomial.
The Binomial Theorem.
The binomial expansions
Use the Binomial Theorem
Use the Binomial Theorem
Binomial Theorem Pascal’s Triangle
Use the Binomial Theorem
CS November 2010 Ragged arrays, Applets
11.6 Binomial Theorem & Binomial Expansion
CS2011 Introduction to Programming I Multidimensional Arrays
CS November 2010 Developing array algorithms. Reading:
CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9
Presentation transcript:

1 CS Nov 2011 Ragged arrays Reading for today: sec Reading for next time: Interfaces: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7). About recursion: Did you Hear about the programmer they found dead in the shower? He was clutching an empty bottle of shampoo which read, "Lather, rinse, repeat."

1. Slow to reveal! /** Extract and return … */ public String reveal() { … int p= 4; String result= ""; // inv: All hidden chars before // pixel p are in result[0..k-1] for (int k= 0; k < len; k= k+1) { result= result + (char) (getHidden(p)); p= p+1; } return result; } 2 /** Extract and return … */ public String reveal() { … int p= 4; char[] result= new char[len]; // inv: All hidden chars before // pixel p are in result[0..k-1] for (int k= 0; k < len; k= k+1) { result[k]= (char) (getHidden(p)); p= p+1; } return new String(result); } gives n 2 algorithm (n is message length) linear algorithm

3 Some mysteries: an odd asymmetry, and strange toString output (see demo). Type of d is int[][] (“int array array”/ “an array of int arrays”) To declare variable d: int d[][]; To create a new array and assign it to d: d= new int[5][4]; or, using an array initializer, d= new int[][]{ {5,4,7,3}, {4,8,9,7}, {5,1,2,3}, {4,1,2,9}, {6,7,8,0} }; d Review of two-dimensional arrays Number of rows of d: d.length Number of columns in row r of d: d[r].length

4 How multi-dimensional arrays are stored: arrays of arrays int b[][]= new int[][]{ {9, 6, 4}, {5, 7, 7} }; b a0 r r r0 r b holds the name of a one-dimensional array object with b.length elements; its elements are the names of 1D arrays. b[i] holds the name of a 1D array of ints of length b[i].length java.util.Arrays.deepToString recursively creates an appropriate String

5 Ragged arrays: rows have different lengths b a0 r r r0 r int[][] b; Declare variable b of type int[][] b= new int[2][] Create a 1-D array of length 2 and store its name in b. Its elements have type int[] (and start as null). b[0]= new int[] {17, 13, 19}; Create int array, store its name in b[0]. b[1]= new int[] {28, 95}; Create int array, store its name in b[1]

6 Pascal’s Triangle The first and last entries on each row are 1. Each other entry is the sum of the two entries above it row r has r+1 values …012345…

7 Pascal’s Triangle Entry p[i][j] is the number of ways i elements can be chosen from a set of size j ! p[i][j] = “i choose j” = …012345… ( ) ijij recursive formula: for 0 < i < j, p[i][j] = p[i–1][j–1] + p[i–1][j]

8 Pascal’s Triangle Binomial theorem: Row r gives the coefficients of (x + y) r (x + y) 2 = 1x 2 + 2xy + 1y 2 (x + y) 3 = 1x 3 + 3x 2 y + 3xy 2 + 1y 3 (x + y) r = ∑ (k choose r) x k y r-k 0 ≤ k ≤ r

9 Function to compute first r rows of Pascal’s Triangle in a ragged array /** = ragged array of first n rows of Pascal’s triangle. Precondition: 0 ≤ n */ public static int[][] pascalTriangle(int n) { int[][] b= new int[n][]; // First n rows of Pascal's triangle // invariant: rows 0..i-1 have been created for (int i= 0; i != b.length; i= i+1) { // Create row i of Pascal's triangle b[i]= new int[i+1]; // Calculate row i of Pascal's triangle b[i][0]= 1; // invariant b[i][0..j-1] have been created for (int j= 1; j < i; j= j+1) { b[i][j]= b[i-1][j-1] + b[i-1][j]; } b[i][i]= 1; } return b; }

10 Adding up elements of an Integer or int array no matter what its dimension!! /** = Sum of elements of b. Precondition: b is an Integer or an array with base type Integer. */ public static int sum(Object b) { if (b instanceof Object[]) { Object[] bb= (Object[]) b; int sum= 0; //inv: sum = sum of b[0..k-1]*/ for (int k= 0; k < bb.length; k= k+1) { sum= sum + sum(bb[k]); } return sum; } // { b has type Integer } return 0 + (Integer) b; }