Download presentation
Presentation is loading. Please wait.
Published byOliver McDowell Modified over 9 years ago
1
Multidimensional Arrays 01204111 Computer and Programming
2
Agenda Quick review on 1-dimensional array. Declaring and creating multidimensional arrays 2-dimensional array – Nested for-loop – Applications 3-dimensional array
3
Quick review
4
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]); 10 9 8 7 6 9 8 7 6 10 9 8 7 6
5
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]); 10 8 6 4 2 86428642 8 6 4 2 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.
6
Vectors In mathematics, a vector is an ordered sequence of items. It can be easily represented as a 1D array. 10 5 7 12 3 9 10 5 7 12 3 9
7
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 4 -2 3 0 -3. = 1(-2) + 2. 3 + (-1). 0 + 4(-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.
8
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)
9
2-dimensional arrays
10
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. 1.2 2.1 6.5 17.0 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1-dimensional array 2-dimensional array 3-dimensional array
11
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. 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 0 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 2 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 3
12
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
13
2-dimensional arrays: declaration Since 2-dimensional arrays have 2 indices, when we declare them, we put 1 comma in the brackets: type [,] varname;
14
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]; 0000 0000 0000 tab
15
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 0 1 2 0123 Both indices run from 0 to their range minus one.
16
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 00 00 79 36 Do not forget that an array must be created with the new operator before using it.
17
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 } }; 101001000 202002000 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 } };
18
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.
19
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] 123 456 789 101112 131415
20
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!
21
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.
22
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();
23
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.
24
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)
25
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(); } 1011101 1000000 1110110 1100100 0001101 1011101 1000000 1110110 1100100 0001101 1011101 #.###.# #...... ###.##. ##..#.....##.# #.###.# #...... ###.##. ##..#.....##.# #.###.# (Click to show)
26
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.
27
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); 1030300
28
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(); }
29
Matrices: 2D arrays
30
Matrices We can naturally represent matrices as 2D arrays. 1 0 1 5 3 7 0 4 0 9 5 0 1015 3704 0950 Usually the matrix entries are floating-point number. So we usually declare an array of double for a matrix.
31
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) 1015 3704 0950
32
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
33
Nested for-loops When working with 2D arrays, we usually have to write nested loop to iterate over all elements in the arrays.
34
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
35
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
36
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: 1 0 1 5 3 7 0 4 0 9 5 0
37
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
38
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
39
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
40
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; }
41
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)
42
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. 1 0 1 5 3 7 0 4 0 9 5 0 1 0 1 3
43
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. 1 0 1 5 3 7 0 4 0 9 5 0 1 0 1 3
44
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.
45
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.
46
3-dimensional arrays
47
3D arrays 3D arrays is very similar to 2D arrays, but they have 3 indices. 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 1010 0110 1001 0101 3-dimensional array
48
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}} };
49
credits[1,…] 67 89 1011 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}} }; 12 34 45
50
credits[1,…] 67 89 1011 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}} }; 12 34 45 credits[0, 2, 1] = 100; 100 credits[1, 1, 0] = 50; 50
51
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.
52
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)
53
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 1 1 2 2 3 3
54
Conclusions
55
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.