Multidimensional Arrays

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
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.
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
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.
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)
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
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.
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.
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.
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.
ARRAYS Multidimensional realities Image courtesy of
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.
2.4 A Case Study: Percolation Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
CSE 504 Discrete Mathematics & Foundations of Computer Science
CSNB 143 Discrete Mathematical Structures
Properties and Applications of Matrices
MATRICES.
Two-Dimensional Arrays
Discrete Structures – CNS2300
Cache Memories CSE 238/2038/2138: Systems Programming
MODIFIED SIEVE OF ERATOSTHENES
Dr. Ameria Eldosoky Discrete mathematics
CS 213: Data Structures and Algorithms
Rosen 5th ed., §2.7 ~18 slides, ~1 lecture
Sequences and Summations
Arrays An Array is an ordered collection of variables
Yong Choi School of Business CSU, Bakersfield
Multidimensional Arrays
Arrays Continued.
Exam 2 Review 1.
Lecture 10 Arrays.
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
In this class, we will cover:
JavaScript Arrays.
Section 2.4 Matrices.
CS100: Discrete structures
Multidimensional array
Arrays Week 2.
Properties of Relations
In this class, we will cover:
Multi-Dimensional 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.
Introduction to Computer Science I.
CIS 110: Introduction to Computer Programming
Chapter 8 Multidimensional Arrays
Discrete Mathematics and its Applications
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.
Rosen 5th ed., §2.7 ~18 slides, ~1 lecture
C++ Array 1.
Arrays and Matrices Prof. Abdul Hameed.
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
2.4 A Case Study: Percolation
Applied Discrete Mathematics Week 4: Functions
Algorithms and data structures: basic definitions
Presentation transcript:

Multidimensional Arrays

Two Dimensional Arrays Table of data for each experiment and outcome. Table of grades for each student and assignments. Table of grayscale values for each pixel in a 2D image. Mathematical abstraction. Matrix. Java abstraction. 2D array. "Microarrays allow us to see if a particular gene is on or off in a particular tissue. The colors on this picture correspond to whether or not the gene is expressed. So each row is a gene, and each column is a particular experiment, for example a particular type of tissue. If you see a red spot for some gene for breast tumors, that means this gene is expressed in breast cancer. But that same gene is not expressed in the brain, for example."

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; }

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 }, };

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];

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 ith row of a and the jth column of b. all values initialized to 0 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];

Array Challenge 2 Q. How many scalar multiplications multiply two N-by-N matrices? A. N B. N2 C. N3 D. N4 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]; Answer: C (triply nested loop)

Self-Avoiding Walk

Self-Avoiding Walk Model. N-by-N lattice. Start in the middle. 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? A1. Not hard to convince yourself that you will always escape in a 5-by-5 grid

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 < N-1 && y > 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"); dead end take a random unvisited step

Self-Avoiding Walks

Summary Arrays. Organized way to store huge quantities of data. Almost as easy to use as primitive types. 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

Off by One "You’re always off by 1 in this business." - J. Morris Bush: We're number 0 " . . . By the way, we rank 10th among the industrialized world in broadband technology and its availability. That’s not good enough for America. Tenth is 10 spots too low as far as I’m concerned. " - George W. Bush http://cbs.marketwatch.com/news/story.asp?guid=%7B69B94AFA-721D-48CC-92E6-D27984BD9C0C%7D&siteid=google&dist=google http://ruixue.cogsci.uiuc.edu/mt/snowtime/archives/000432.html President George W. said the following in his speech promoting broadband internet access: "Now the use of broadband has tripled since 2000 from 7 million subscriber lines to 24 million," Bush said "That's good. But that's way short of the goal for 2007. And so - by the way, we rank 10th amongst the industrialized world in broadband technology and its availability. That's not good enough for America. Tenth is 10 spots too low as far as I'm concerned. http://imgs.xkcd.com/comics/donald_knuth.png

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

Sieve of Eratosthenes

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

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: 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; if (isPrime[i]) primes++; StdOut.println("The number of primes <= " + N + " is " + primes); if i is prime, mark multiples of i as nonprime