Multidimensional Arrays CIT 336. The Basics Explaining Multidimensional Arrays.

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Arrays.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Pradeep Velugoti Lakshman Tallam.  Type in the month name “January” in any cell say A1.  Now drag the fill handle to the right to select the range (Do.
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
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.
©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.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 9 Introduction to Arrays
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Multiple Choice Solutions True/False a c b e d   T F.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Unit 12 JavaScript Arrays Instructor: Brent Presley.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
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.
ME 142 Engineering Computation I More Loops & Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
CS 201 Tarik Booker California State University, Los Angeles.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Chapter 6: Using Arrays.
C# Arrays.
Two Dimensional Arrays
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
2-D Lists Taken from notes by Dr. Neil Moore
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Repeating Instructions And Advance collection
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Multidimensional Arrays Vectors of Vectors
Web Systems Development (CSC-215)
Chapter 7 Part 2 Edited by JJ Shepherd
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Programming Control Structures with JavaScript Part 2
Loops and Arrays in JavaScript
Multidimensional Arrays Section 6.4
2-D Lists Taken from notes by Dr. Neil Moore
Presentation transcript:

Multidimensional Arrays CIT 336

The Basics Explaining Multidimensional Arrays

Arrays An array is a serial collection of data. In most programming languages, including PHP, individual array elements are accessed using the following syntax: variableName[index] Ex: $boolArr[2] = true; In the example above, the 3 rd item in the $boolArr array is being accessed (indexes are 0-based, so the first item is accessed at index: 0, the second item at index: 1, and so on.) $boolArr [0]: true [1]: false [2]: true [3]: true

2D Arrays A ‘normal’ array is one dimensional, meaning it has elements ranging from index:0 to index:n. However, arrays do not necessarily need to be limited to one dimension. We could represent a 2D array by using two indexers like this: variableName[index1][index2] Ex: $intArr2D[1][2] = 2; Notice, there are now two indexers, giving our array two dimensionality. The next slide demonstrates this using a table to illustrate. This concept can be expanded to any number of dimensions, for example, a three dimensional array would be accessed like this: $doubleArr3D[0][9][6] = 3.4f; $intArr2D [0][0]: 0 [0][1]: 0 [0][2]: 0 [1][0]: 0 [1][1]: 1 [1][2]: 2 [2][0]: 0 [2][1]: 2 [2][2]: 4

2D Array - Rectangular $intArr2D[0][?][1][?][2][?] [?][0][0][0]: 0[1][0]: 0[2][0]: 0 [?][1][0][1]: 0[1][1]: 1[2][1]: 2 [?][2][0][2]: 0[1][2]: 2[2][2]: 4 This is an example of a “rectangular” array – an array of arrays where the nested arrays are all the same size (in this case, each nested array contains 3 elements).

Jagged Arrays Another type of multidimensional array is a Jagged Array. A jagged array is also an array of arrays, but in this case, the nested arrays may each be of different lengths. Consider the example to the right – $jagged[0] contains 3 elements, but $jagged[1] only contains 2 elements, while $jagged[2] contains 5 elements. This variation in nested element length is why the this sort of array is called a Jagged Array. $jagged[0] [0]: ' a ' [1]: ' b ' [2]: ' c ' $jagged[1] [0]: ' 1 ' [1]: ' 2 ' $jagged[2] [0]: '. ' [1]: ', ' [2]: ' ; ' [3]: ' ! ' [4]: ' ? '

2D Array - Jagged $jagged[0][?][1][?][2][?] [0][0]: 'a'[1][0]: '1'[2][0]: '.' [0][1]: 'b'[1][1]: '2'[2][1]: ',' [0][2]: 'c'[2][2]: ';' [2][3]: '!' [2][4]: '?' This is an example of a “jagged” array – an array of arrays where the nested arrays can be of varying sizes.

Creating & Accessing Multidimensional Arrays The process for creating a multidimensional array is very similar to the process for creating a single dimensional array. Essentially, a “normal” array is first created, and then arrays are created and assigned as elements of that array. For example: $myArray = array(); $myArray[0] = array(); $myArray[1] = array();... $myArray[99] = array(); This demonstrates manually assigning each nested array, but a for loop could certainly be used. Elements of multidimensional arrays are accessed just like one dimensional arrays, only additional indexers are used. $board[0][0] = false; $cell[15][6] = 16.4; $names[3]['first'] = 'Don';

Application Using Multidimensional Arrays

When do we use them…? There are a number of cases where multidimensional arrays are the natural choice for a data structure. For instance, in the case of modeling a chess board, a two dimensional array makes a lot of sense; a chess board is really a 2D array of squares and representing it with a 2D array of variables is pretty straightforward. Tabular data may also be a good candidate for using a 2D array. Tables also make easy transitions to 2D arrays because they themselves our laid out in a similar fashion (rows by cols) and are easy to translate into array[row][col] structure.

Practical Example An example from our textbook that doesn’t model a typical 2D object is an online shopping cart. The shopping cart is represented as an array. In this case, it will be an array of items (themselves an array), creating a multidimensional array. $shoppingCart = array(); Then, each item is represented as an associative array. $item = array(); $item['product_name'] = 'Book'; $item['product_price'] = 15.05; $item['item_quantity'] = 1; Items are then "added to the cart" simply by adding them to the $shoppingCart array. $shoppingCart[] = $item; Additional items can easily be defined and added. The $shoppingCart array contains a series of items, and each item contains its own set of elements. This same approach can be used could be used to represent employees within a department, departments within an organization, and even organizations within groups of organizations.