Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Arrays.
1 Various Methods of Populating Arrays Randomly generated integers.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Arrays Chapter 6 Chapter 6.
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.
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.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Jan Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
CSC – Java Programming II Lecture 9 January 30, 2002.
Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
1.4 Arrays "... By the way, we rank 10 th among the industrialized world in broadband technology and its availability. That’s not good enough for America.
Solving Recurrence Relations T(n)= 2T(n/2)+n = 2*(2T(n/4)+n/2)+n = 4*T(n/4) +2*n = 8*T(n/8) + 3*n = 2 (log n) * T(1) + (log n) * n = n * 1 + n log n O(n.
Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
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.
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.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CompSci 100E 3.1 Random Walks “A drunk man wil l find his way home, but a drunk bird may get lost forever”  – Shizuo Kakutani Suppose you proceed randomly.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Warm Up Perform the indicated operations. If the matrix does not exist, write impossible
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
CompSci 100E 4.1 Google’s PageRank web site xxx web site yyyy web site a b c d e f g web site pdq pdq.. web site yyyy web site a b c d e f g web site xxx.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
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.
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
ARRAYS Multidimensional realities Image courtesy of
4-3 Matrix Multiplication Objective: To multiply a matrix by a scalar multiple.
CS 112 Introduction to Programming Array Reference Semantics; 2D Arrays Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
1 A Case Study: Percolation Percolation. Pour liquid on top of some porous material. Will liquid reach the bottom? Applications. [ chemistry, materials.
2.4 A Case Study: Percolation Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
Review Multi-Dimensional Array Searching and Sorting in arrays Inheritance Graphics.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Properties and Applications of Matrices
12-1 Organizing Data Using Matrices
Cache Memories CSE 238/2038/2138: Systems Programming
Multidimensional Arrays
Multidimensional Arrays
Exam 2 Review 1.
Multidimensional Arrays
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/6/11 12:33.
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Arrays and Matrices Prof. Abdul Hameed.
2.4 A Case Study: Percolation
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Multidimensional Arrays

2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each experiment and outcome. n Table of grayscale values for each pixel in a 2D image. Mathematical abstraction. Matrix. Java abstraction. 2D array.

3 Two Dimensional Arrays in Java Array access. Use a[i][j] to access element in row i and column j. Zero-based indexing. Row and column indices start at 0. int M = 10; int N = 3; double[][] a = new double[M][N]; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { a[i][j] = 0.0; }

4 Setting 2D Array Values at Compile Time Initialize 2D array by listing values. double[][] p = { {.02,.92,.02,.02,.02 }, {.02,.02,.32,.32,.32 }, {.02,.02,.02,.92,.02 }, {.92,.02,.02,.02,.02 }, {.47,.02,.47,.02,.02 }, };

5 Matrix Addition Matrix addition. Given two N-by-N matrices a and b, define c to be the N-by-N matrix where c[i][j] is the sum a[i][j] + b[i][j]. double[][] c = new double[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) c[i][j] = a[i][j] + b[i][j];

6 Matrix Multiplication Matrix multiplication. Given two N-by-N matrices a and b, define c to be the N-by-N matrix where c[i][j] is the dot product of the i th row of a and the j th column of b. double[][] c = new double[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j]; all values initialized to 0

7 Array Challenge 2 Q. How many scalar multiplications multiply two N-by-N matrices? A. NB. N 2 C. N 3 D. N 4 double[][] c = new double[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j];

Self-Avoiding Walk

9 Model. n N-by-N lattice. n Start in the middle. n Randomly move to a neighboring intersection, avoiding all previous intersections. Applications. Polymers, statistical mechanics, etc. Q. What fraction of time will you escape in an 5-by-5 lattice? Q. In an N-by-N lattice? Q. In an N-by-N-by-N lattice?

10 Self-Avoiding Walk: Implementation public class SelfAvoidingWalk { public static void main(String[] args) { int N = Integer.parseInt(args[0]); // lattice size int T = Integer.parseInt(args[1]); // number of trials int deadEnds = 0; // trials resulting in dead end for (int t = 0; t < T; t++) { boolean[][] a = new boolean[N][N]; // intersections visited int x = N/2, y = N/2; // current position while (x > 0 && x 0 && y < N-1) { if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1]) { deadEnds++; break; } a[x][y] = true; // mark as visited double r = Math.random(); if (r < 0.25) { if (!a[x+1][y]) x++; } else if (r < 0.50) { if (!a[x-1][y]) x--; } else if (r < 0.75) { if (!a[x][y+1]) y++; } else if (r < 1.00) { if (!a[x][y-1]) y--; } } System.out.println(100*deadEnds/T + "% dead ends"); } take a random unvisited step dead end

11 Self-Avoiding Walks

12 Summary Arrays. n Organized way to store huge quantities of data. n Almost as easy to use as primitive types. n Can directly access an element given its index. Ahead. Reading in large quantities of data from a file into an array. 1.5

1.4 Arrays: Extra Slides

14 Off by One "You’re always off by 1 in this business." - J. Morris

15 Memory Representation Memory representation. Maps directly to physical hardware. Consequences. n Arrays have fixed size. n Accessing an element by its index is fast. n Arrays are pointers. 2D array. Array of arrays. Consequences. Arrays can be ragged.

Sieve of Eratosthenes

17 Sieve of Eratosthenes Prime. An integer > 1 whose only positive factors are 1 and itself. Ex. 2, 3, 5, 7, 11, 13, 17, 23, … Prime counting function.  (N) = # primes  N. Ex.  (17) = 7. Sieve of Eratosthenes. Maintain an array isPrime[] to record which integers are prime. Repeat for i=2 to  N – if i is not still marked as prime i is is not prime since we previously found a factor – if i is marked as prime i is prime since it has no smaller factors mark all multiples of i to be non-prime

18 Sieve of Eratosthenes Prime. An integer > 1 whose only positive factors are 1 and itself. Ex. 2, 3, 5, 7, 11, 13, 17, 23, … Prime counting function.  (N) = # primes  N. Ex.  (17) = 7.

19 Sieve of Eratosthenes: Implementation public class PrimeSieve { public static void main(String[] args) { int N = Integer.parseInt(args[0]); // initially assume all integers are prime boolean[] isPrime = new boolean[N+1]; for (int i = 2; i <= N; i++) isPrime[i] = true; // mark non-primes <= N using Sieve of Eratosthenes for (int i = 2; i*i <= N; i++) { if (isPrime[i]) { for (int j = i; i*j <= N; j++) isPrime[i*j] = false; } // count primes int primes = 0; for (int i = 2; i <= N; i++) if (isPrime[i]) primes++; StdOut.println("The number of primes <= " + N + " is " + primes); } if i is prime, mark multiples of i as nonprime