Multidimensional Arrays 01204111 Computer and Programming.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
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.
©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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 8 Arrays and Strings
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
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.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Multi-Dimensional Arrays
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Course A201: Introduction to Programming 09/30/2010.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Fortran: Control Structures Session Three ICoCSIS.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Arrays. Arrays are objects that help us organize large amounts of information.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays Chapter 7.
Arrays and Collections
Chapter 6: Using Arrays.
EGR 2261 Unit 10 Two-dimensional Arrays
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Multidimensional Arrays Computer and Programming

Agenda Quick review on 1-dimensional array. Declaring and creating multidimensional arrays 2-dimensional array – Nested for-loop – Applications 3-dimensional array

Quick review

Quick review (1) What is the output of the following program? int [] a = new int[5]; for(int i=0; i<5; i++) a[i] = 10 - i; for(int i=0; i<5; i++) Console.WriteLine(a[i]); int [] a = new int[5]; for(int i=0; i<5; i++) a[i] = 10 - i; for(int i=0; i<5; i++) Console.WriteLine(a[i]);

Quick review (2) What is the output of the following program? int [] a = new int[5]; for(int i=0; i<5; i++) a[i] = 10 – i*2; for(int i=1; i<=5; i++) Console.WriteLine(a[i]); int [] a = new int[5]; for(int i=0; i<5; i++) a[i] = 10 – i*2; for(int i=1; i<=5; i++) Console.WriteLine(a[i]); You are trying to access a[5] which does not exist. You will see an error message: System.IndexOutOfRangeException. You are trying to access a[5] which does not exist. You will see an error message: System.IndexOutOfRangeException.

Vectors In mathematics, a vector is an ordered sequence of items. It can be easily represented as a 1D array

Thinking corner: dot products A dot product of two vectors A=[a 1, a 2, …, a n ] and B=[b 1, b 2, …, b n ] is a scalar a 1 b 1 + a 2 b 2 + … + a n b n = 1(-2) (-1) (-3) = -8 Write method DotProduct that takes two parameters: array a and array b of double, which have the same length, and returns their dot product.

Dot product: solution static double DotProduct(double [] a, double [] b) { int d = a.Length; double p = 0; for(int i = 0; i < d; i++) p += a[i] * b[i]; return p; } static double DotProduct(double [] a, double [] b) { int d = a.Length; double p = 0; for(int i = 0; i < d; i++) p += a[i] * b[i]; return p; } (Click to show)

2-dimensional arrays

Array Dimensions Arrays are table-like structures. You can also think of them as matrices in mathematics. An array's dimension specifies how many indices an array has dimensional array 2-dimensional array 3-dimensional array

Higher Dimensions You can arrays with higher than 3 dimensions, but it will be very hard to visualize. In this course, we shall not focus on them

1D, 2D, 3D, … To save space, we sometimes refer to – 1-dimensional arrays as 1D arrays – 2-dimensional arrays as 2D arrays – 3-dimensional arrays as 3D arrays

2-dimensional arrays: declaration Since 2-dimensional arrays have 2 indices, when we declare them, we put 1 comma in the brackets: type [,] varname;

2-dimensional arrays: creation To create the array, we use the new operator and supply the ranges of both indices. int [,] tab; tab = new int[3,4]; int [,] tab; tab = new int[3,4]; tab

Elements in an array int [,] tab; tab = new int[3,4]; int [,] tab; tab = new int[3,4]; tab[0,0]tab[0,1]tab[0,2]tab[0,3] tab[1,0]tab[1,1]tab[1,2]tab[1,3] tab[2,0]tab[2,1]tab[2,2]tab[2,3] tab Both indices run from 0 to their range minus one.

Example 1 int [,] d; d = new int[2,2]; d[0,0] = 7; d[0,1] = 9; d[1,0] = 3; d[1,1] = 6; int [,] d; d = new int[2,2]; d[0,0] = 7; d[0,1] = 9; d[1,0] = 3; d[1,1] = 6; d Do not forget that an array must be created with the new operator before using it.

Array initialization As in 1D arrays, we can initialize a 2D array when we create it. We can also leave the new part out. int [,] a = new int [2,3] { { 10, 100, 1000 }, {20, 200, 2000 } }; int [,] a = new int [2,3] { { 10, 100, 1000 }, {20, 200, 2000 } }; int [,] a = new int [2,3] { { 10, 100, 1000 }, {20, 200, 2000 } }; int [,] a = new int [2,3] { { 10, 100, 1000 }, {20, 200, 2000 } }; int [,] a = { { 10, 100, 1000 }, {20, 200, 2000 } }; int [,] a = { { 10, 100, 1000 }, {20, 200, 2000 } };

How to visualize a 2D array (1) You can view it in either direction. double [,] a = new double[5,3]; a[0,0]a[1,0]a[2,0]a[3,0]a[4,0] a[0,1]a[1,1]a[2,1]a[3,1]a[4,1] a[0,2]a[1,2]a[2,2]a[3,2]a[4,2] a[0,0]a[0,1]a[0,0] a[1,0]a[1,1]a[1,2] a[2,0]a[2,1]a[2,2] a[3,0]a[3,1]a[3,2] a[4,0]a[4,1]a[4,2] It usually does not matter which way you visualize it, as long as the first index runs from 0 to 4, and the second index runs from 0 to 2.

How to visualize a 2D array (2) However, in C# the way you initialize a 2D array is to group the elements based on the first index first. This is also how the array is stored in the memory. double [,] a = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}, {13,14,15} }; double [,] a = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}, {13,14,15} }; a[0,0]a[0,1]a[0,0] a[1,0]a[1,1]a[1,2] a[2,0]a[2,1]a[2,2] a[3,0]a[3,1]a[3,2] a[4,0]a[4,1]a[4,2]

Example 2 (1) You can use a 2D array to represent many table-like data. int [,] map = { {1,0,1,1,1,0,1}, {1,0,0,0,0,0,0}, {1,1,1,0,1,1,0}, {1,1,0,0,1,0,0}, {0,0,0,1,1,0,1}, {1,0,1,1,1,0,1} }; int [,] map = { {1,0,1,1,1,0,1}, {1,0,0,0,0,0,0}, {1,1,1,0,1,1,0}, {1,1,0,0,1,0,0}, {0,0,0,1,1,0,1}, {1,0,1,1,1,0,1} }; It's a maze!

Example 2 (2) How to display the maze on the screen? int [,] map = { {1,0,1,1,1,0,1}, {1,0,0,0,0,0,0}, {1,1,1,0,1,1,0}, {1,1,0,0,1,0,0}, {0,0,0,1,1,0,1}, {1,0,1,1,1,0,1} }; int [,] map = { {1,0,1,1,1,0,1}, {1,0,0,0,0,0,0}, {1,1,1,0,1,1,0}, {1,1,0,0,1,0,0}, {0,0,0,1,1,0,1}, {1,0,1,1,1,0,1} }; Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}", map[0,0], map[0,1], map[0,2], map[0,3], map[0,4], map[0,5], map[0,6]); Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}", map[1,0], map[1,1], map[1,2], map[1,3], map[1,4], map[1,5], map[1,6]); //… and so on Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}", map[0,0], map[0,1], map[0,2], map[0,3], map[0,4], map[0,5], map[0,6]); Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}", map[1,0], map[1,1], map[1,2], map[1,3], map[1,4], map[1,5], map[1,6]); //… and so on This is a bad idea.

Example 2 (3) Let's try to print just the first line first! Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}", map[0,0], map[0,1], map[0,2], map[0,3], map[0,4], map[0,5], map[0,6]); Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}", map[0,0], map[0,1], map[0,2], map[0,3], map[0,4], map[0,5], map[0,6]); How can we remove these duplications? Use some kind of loops! for(int i = 0; i < 7; i++) Console.Write("{0}", map[0,i]); Console.WriteLine(); for(int i = 0; i < 7; i++) Console.Write("{0}", map[0,i]); Console.WriteLine();

Thinking corner Can you write a program that prints the whole map (in 0 and 1)? // This only prints the first line for(int i = 0; i < 7; i++) Console.Write("{0}", map[0,i]); Console.WriteLine(); // This only prints the first line for(int i = 0; i < 7; i++) Console.Write("{0}", map[0,i]); Console.WriteLine(); This index specify which line to be printed.

Thinking corner: solution We can loop over the first index as well. for(int j = 0; j < 6; j++) { // This prints one line for(int i = 0; i < 7; i++) Console.Write("{0}", map[j,i]); Console.WriteLine(); } for(int j = 0; j < 6; j++) { // This prints one line for(int i = 0; i < 7; i++) Console.Write("{0}", map[j,i]); Console.WriteLine(); } (Click to show)

Thinking corner: a nicer solution We can have a better maze by printing # and. instead of 1 and 0. for(int j = 0; j < 6; j++) { // This prints one line for(int i = 0; i < 7; i++) if(map[j,i]==0) Console.Write("."); else Console.Write("#"); Console.WriteLine(); } for(int j = 0; j < 6; j++) { // This prints one line for(int i = 0; i < 7; i++) if(map[j,i]==0) Console.Write("."); else Console.Write("#"); Console.WriteLine(); } #.###.# # ###.##. ##..#.....##.# #.###.# # ###.##. ##..#.....##.# #.###.# (Click to show)

Array lengths The program in the previous example does not work right away when we change the size of the maze, because we put 6 and 7 in the code. for(int j = 0; j < 6; j++) { // … for(int i = 0; i < 7; i++) // … Console.WriteLine(); } for(int j = 0; j < 6; j++) { // … for(int i = 0; i < 7; i++) // … Console.WriteLine(); } However, we can read these length directly from the array.

Array properties / methods Method GetLength( j ) – returns the length of the j-th index; the first index starts at 0. Property Length – returns the total number of elements double [,] temp = new double[10,30]; Console.WriteLine(temp.GetLength(0)); Console.WriteLine(temp.GetLength(1)); Console.WriteLine(temp.Length); double [,] temp = new double[10,30]; Console.WriteLine(temp.GetLength(0)); Console.WriteLine(temp.GetLength(1)); Console.WriteLine(temp.Length);

Thinking corner: final solution We read the lengths from the array itself. So that our code works when we change array initialization. for(int j = 0; j < map.GetLength(1); j++) { // … for(int i = 0; i < map.GetLength(2); i++) // … Console.WriteLine(); } for(int j = 0; j < map.GetLength(1); j++) { // … for(int i = 0; i < map.GetLength(2); i++) // … Console.WriteLine(); }

Matrices: 2D arrays

Matrices We can naturally represent matrices as 2D arrays Usually the matrix entries are floating-point number. So we usually declare an array of double for a matrix.

Quick Review We have a 2D array mat representing a matrix. How can we know its "dimension"? I.e., how many rows and how many columns does mat have? mat.GetLength(0) mat.GetLength(1)

Thinking Corner: how many zero's? Write method CountZero that takes – A 2D array mat and returns the number of zero entries in mat. static int CountZero(double [,] mat) { int zcount = 0; for(int r = 0; r < mat.GetLength(0); r++) for(int c = 0; c < mat.GetLength(1); c++) if(mat[r,c] == 0) zcount++; return zcount; } static int CountZero(double [,] mat) { int zcount = 0; for(int r = 0; r < mat.GetLength(0); r++) for(int c = 0; c < mat.GetLength(1); c++) if(mat[r,c] == 0) zcount++; return zcount; } (Click to show) takes a 2D array

Nested for-loops When working with 2D arrays, we usually have to write nested loop to iterate over all elements in the arrays.

Quick practice (1) What is the output of this program? for(int i=0; i<4; i++) for(int j=0; j<2; j++) Console.WriteLine("{0},{1}", i, j); for(int i=0; i<4; i++) for(int j=0; j<2; j++) Console.WriteLine("{0},{1}", i, j); 0, 0 0, 1 1, 0 1, 1 2, 0 2, 1 3, 0 3, 1 0, 0 0, 1 1, 0 1, 1 2, 0 2, 1 3, 0 3, 1 We often learn how nested loops work by considering from the inner loop. This inner loop runs twice. For the first round, j=0, and j=1 in the second round. It prints whatever the value of variable i with the value of j. i, 0 i, 1 i, 0 i, 1

Quick practice (2) What is the output of this program? for(int i=0; i<4; i++) for(int j=0; j<=i; j++) Console.WriteLine("{0},{1}", i, j); for(int i=0; i<4; i++) for(int j=0; j<=i; j++) Console.WriteLine("{0},{1}", i, j); 0, 0 1, 0 1, 1 2, 0 2, 1 2, 2 3, 0 3, 1 3, 2 3, 3 0, 0 1, 0 1, 1 2, 0 2, 1 2, 2 3, 0 3, 1 3, 2 3, 3 What can we learn by looking at the inner loop? This inner loop may runs many times depending on the value of i. It runs variable j from 0 to i. It prints whatever the value of variable i with the value of j. i, 0 i, 1. i, i i, 0 i, 1. i, i

How to read matrices Write a method ReadMatrix that takes as parameters: – integer r denoting the number of rows – integer c denoting the number of columns, reads a r x c matrix from the user, and returns the matrix. The user will enter one double per line, starting from the first row in the following order:

Method ReadMatrix static double [,] ReadMatrix(int r, int c) { double [,] mat = new double[r,c]; for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) mat[i,j] = double.Parse(Console.ReadLine()); return mat; } static double [,] ReadMatrix(int r, int c) { double [,] mat = new double[r,c]; for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) mat[i,j] = double.Parse(Console.ReadLine()); return mat; } Create the array. Read each entry Return the array

A closer look at the nested loops Consider the case when r = 4, and c = 6. The sequence of elements in the array that have been assigned is shown as follows. for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) mat[i,j] = double.Parse(Console.ReadLine()); for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) mat[i,j] = double.Parse(Console.ReadLine()); Unassigned Assigned i = 0 i = 1 i = 2 i = 3

Thinking corner: matrix addition (1) Write method AddMatrix that takes matrices a and b and returns a new matrix c = a + b. Note that you have to create matrix c inside method AddMatrix. What does the method look like? static double [,] AddMatrix(double [,] a, double [,] b) { } static double [,] AddMatrix(double [,] a, double [,] b) { } returns a 2D array

Thinking corner: matrix addition (2) To create 2D array c, we have to read the matrix dimension from a and b. static double [,] AddMatrix(double [,] a, double [,] b) { double [,] c = new double[a.GetLength(0), a.GetLength(1)]; // add a and b return c; } static double [,] AddMatrix(double [,] a, double [,] b) { double [,] c = new double[a.GetLength(0), a.GetLength(1)]; // add a and b return c; }

Thinking corner: solution to matrix addition (3) static double [,] AddMatrix(double [,] a, double [,] b) { double [,] c = new double[a.GetLength(0), a.GetLength(1)]; for(int r = 0; r < a.GetLength(0); r++) for(int c = 0; c < a.GetLength(1); c++) c[r,c] = a[r,c] + b[r,c]; return c; } static double [,] AddMatrix(double [,] a, double [,] b) { double [,] c = new double[a.GetLength(0), a.GetLength(1)]; for(int r = 0; r < a.GetLength(0); r++) for(int c = 0; c < a.GetLength(1); c++) c[r,c] = a[r,c] + b[r,c]; return c; } (Click to show)

Upper right corner Often we would like to find the sum of elements in a specific part of the matrix. In this case, we are given an n x n matrix and we would like to find the sum of the upper right corner entries

Quick analysis (1) Let's start with a specific example. Let n be 4. These are the list of columns that you have to sum, for each row. – row 1: 1, 2, 3, 4 – row 2: 2, 3, 4 – row 3: 3, 4 – row 4: 4 In general: in row i, you will have to look at column i, i + 1, i + 2, …, n

This is based on counting rows and columns starting at 1. Quick analysis (2): 0-index In general: in row i, you will have to look at column i, i + 1, i + 2, …, n. Don't forget the our indices start at 0. – Row numbers start at 0, end at n-1. – In row i, sum entries in columns i, i+1, …, n-1.

static double SumUpperRight(double [,] mat) { int n = mat.GetLength(0); double sum = 0; for(int i = 0; i < n; i++) for(int j = i; j < n; j++) sum += mat[i,j]; return sum; } static double SumUpperRight(double [,] mat) { int n = mat.GetLength(0); double sum = 0; for(int i = 0; i < n; i++) for(int j = i; j < n; j++) sum += mat[i,j]; return sum; } Solution Work on column j, in row i Sum entries in row i. Work on row i Sum upper right corner entries.

3-dimensional arrays

3D arrays 3D arrays is very similar to 2D arrays, but they have 3 indices dimensional array

Declaration, creation, and initialization Declaration: Creation: Initialization: type [,,] varname; int [,,] credits; varname = new type[s1,s2,s3]; credits = new int[50, 4, 5]; int [,,] credits = { {{ 1,2 }, { 3,4 }, {4,5}}, {{ 6,7 }, { 8,9 }, {10,11}} }; int [,,] credits = { {{ 1,2 }, { 3,4 }, {4,5}}, {{ 6,7 }, { 8,9 }, {10,11}} };

credits[1,…] credits[0,…] Example (1) Consider variable credits on the right: You can view it as two 2D arrays: int [,,] credits = { {{ 1,2 }, { 3,4 }, {4,5}}, {{ 6,7 }, { 8,9 }, {10,11}} }; int [,,] credits = { {{ 1,2 }, { 3,4 }, {4,5}}, {{ 6,7 }, { 8,9 }, {10,11}} };

credits[1,…] credits[0,…] Example Sample operations int [,,] credits = { {{ 1,2 }, { 3,4 }, {4,5}}, {{ 6,7 }, { 8,9 }, {10,11}} }; int [,,] credits = { {{ 1,2 }, { 3,4 }, {4,5}}, {{ 6,7 }, { 8,9 }, {10,11}} }; credits[0, 2, 1] = 100; 100 credits[1, 1, 0] = 50; 50

The meaning of each index (1) When an array has many indices, it is helpful to understand the role of each index to see how to use the array. int [,,] sales = new int[ 7, 24, 5 ]; 5 types of product 24 hours 7 days This array keeps the amount of sales for each type of product, for each hour, for each day in the week.

The meaning of each index (2) With this understanding, we can see right away what we are trying to accomplish using the arrays. For example, when we see: int [,,] sales = new int[ 7, 24, 5 ]; 5 types of product 24 hours 7 days sales[3,8,2] = 75; This is the sales of:Wednesday (3) at 8 o'clock (8) for product type 2 (2)

Property Rank To distinguish between arrays with different dimensions, we can use property Rank. string [] a = new string[100]; double [,] b = new double[10,20]; int [,,] c = new int[5,10,20]; string [] a = new string[100]; double [,] b = new double[10,20]; int [,,] c = new int[5,10,20]; a.Rank b.Rank c.Rank

Conclusions

Conclusion To store table-like data, we can use 2D arrays. If our data have more indices, we can use arrays with higher dimensions. Useful for arrays: – Method GetLength – Properties Length, and Rank. It is helpful to try to understand nested loop from inside out.