Multidimensional Arrays

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

Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Building Java Programs Chapter 7.5
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
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.
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.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
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.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
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.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Request Dispatching for Cheap Energy Prices in Cloud Data Centers
SpringerLink Training Kit
Luminosity measurements at Hadron Colliders
HF NOISE FILTERS PERFORMANCE
Parameterization of Tabulated BRDFs Ian Mallett (me), Cem Yuksel
Bayesian Confidence Limits and Intervals
انتقال حرارت 2 خانم خسرویار.
yaSpMV: Yet Another SpMV Framework on GPUs
Pointers and Classes.
Test 2 Review Outline.
Two-Dimensional Arrays
Arrays in 60 seconds err.. minutes
Pointers & Arrays.
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
Multiplication table. x
Dynamic Array Multidimensional Array Matric Operation with Array
Lecture-5 Arrays.
ECE Application Programming
The University of Texas – Pan American
CSC 253 Lecture 8.
CS Week 8 Jim Williams, PhD.
S. Kiran, PGT (CS) KV, Malleswaram
Arrays An Array is an ordered collection of variables
Engineering Problem Solving with C++, Etter/Ingber
CSC 253 Lecture 8.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 13 Vector of Vectors (2D Arrays)
Chapter 8 Multi-Dimensional Arrays
Arrays November 8, 2017.
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
CS 180 Assignment 6 Arrays.
Multidimensional array
Arrays Week 2.
CS2011 Introduction to Programming I Multidimensional Arrays
Chapter 6 Data Types.
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Pointers & Arrays.
CHAPTER 2 Arrays and Vectors.
CS150 Introduction to Computer Science 1
Arrays.
CS150 Introduction to Computer Science 1
Programming Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

Multidimensional Arrays

Multidimensional Arrays Arrays are good for storing lists of data What about storing a table? 14 8 9 7 78 90 89 70 09 79 87 97 07 08 77 67 56 65 45 30 91 39 37 38 20 27 24 28 80 46 72

Multidimensional Arrays If arrays can store any object, why not have them store arrays? An array of arrays is a list of lists, or a table!

Multidimensional Arrays 2D array declaration: <data type>[ ][ ] <name>; int[ ][ ] grades; String[ ][ ] seatingChart; 2D array definition: grades = new int[9][15]; seatingChart = new String[5][9]; Remember, each reference in seatingChart must be initialized!

Multidimensional Arrays Accessing elements: grades[0][0] = 90; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 90

Multidimensional Arrays Accessing elements: grades[0][0] = 90; Grades[8][5] = 65; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 90 65

Multidimensional Arrays for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 90 34 65 87 78 57 98 77 88 99 43 76 45 44 67 89 82 92 17 95

Multidimensional Arrays How are 2D arrays really stored? 0 1 2 1 2 3 90 34 65 78 57 98 87

Multidimensional Arrays How are 2D arrays really stored? 2D arrays are arrays of arrays 0 1 2 1 2 3 90 34 65 78 57 98 87

Multidimensional Arrays How are 2D arrays really stored? 2D arrays are arrays of arrays 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays grades[0][1] = 34 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 2 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 1 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 2 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87

Multidimensional Arrays Although we can think of it in table format for accessing, it is not necessarily a table! int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[10] = new int [10][ ]; threeD[10][5] = new int[7]; threeD[10][5][4] = 6; What is the Memory Diagram?

Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9

Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 6

Multidimensional Arrays Exercise: Write a method that takes an int n, and returns the multiplication table (2D array) for all numbers from 1 to n

Multidimensional Arrays public static int[ ][ ] multTable(int n){ int[ ][ ] table = new int[n][n]; for(int i=1; i <= n; i++) for(int j=1; j<=n; j++) table[i][j] = i*j; return table; }