Docsity

Multi-dimensional Arrays: Rectangular and Jagged Arrays in CS112, Slides of C programming

Toggle sidebar
bg1
1
CS 112 Introduction to
Programming
Lecture #22:
Multi-dimensional Arrays
http://flint.cs.yale.edu/cs112/
2
Two-Dimensional Arrays
rA one-dimensional array stores a list of
values
rA two-dimensional array, also called double-
subscripted array, can be thought of as a
table of values, with rows and columns
ma two-dimensional array element is referenced
using two index numbers
3
Two Types of Double-Subscripted Arrays
rrectangular arrays
moften represent tables in which each row is the same size and
each column is the same size, e.g.,
int[,] a1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] a11 = new int[3,4];
rjagged arrays
•arrays of arrays
•arrays that compose jagged arrays can be of different lengths,
e.g.,
int[][] array2 = new int[ 3 ][];
array2[ 0 ] = new int[] { 1, 2 };
array2[ 1 ] = new int[] { 3};
array2[ 2 ] = new int[] { 4, 5, 6 };
array2[2][1] = 3;
4
Double-Subscripted Arrays
Double-subscripted array with three rows and four columns.
Row 0
Row 1
Row 2
Column 1Column 0 Column 2 Column 3
a[0][0] a[0][3]a[0][1] a[0][2]
a[1][0] a[1][3]a[1][1] a[1][2]
a[2][0] a[2][3]a[2][2]
Column index (or subscript)
Row index (or subscript)
Array name
a[2][1]
Outline
TwoDimensionalAr
rays.cs
1 // Fig. 7.14: TwoDimensionalArrays.cs
2 // Initializing two-dimensional arrays.
3 using System;
4 using System.Drawing;
5 using System.Collections;
6 using System.ComponentModel;
7 using System.Windows.Forms;
8 using System.Data;
9
10 public class TwoDimensionalArrays : System.Windows.Forms.Form
11 {
12 private System.Windows.Forms.Button showOutputButton;
13 private System.Windows.Forms.Label outputLabel;
14
15 // Visual Studio .NET generated code
16
17 [STAThread]
18 static void Main()
19 {
20 Application.Run( new TwoDimensionalArrays() );
21 }
22
23 private void showOutputButton_Click( object sender,
24 System.EventArgs e )
25 {
26 // declaration and initialization of rectangular array
27 int[,] array1 = new int[,] { { 1, 2, 3}, { 4, 5, 6} };
28
29 // declaration and initialization of jagged array
30 int[][] array2 = new int[ 3][];
31 array2[ 0 ] = new int[] { 1, 2};
32 array2[ 1 ] = new int[] { 3};
33 array2[ 2 ] = new int[] { 4, 5, 6};
34
35 outputLabel.Text += "Values in array1 by row are\n";
Declare and initialize a
rectangular integer array
named array1
Declare a jagged
array named
array2 with 3 rows
Initialize the first
element in array2 to be
an array that contains
two integers
Initialize the second element in
array2 to be an array that
contains 1 integer
Initialize the third element
in array2 to be an array that
contains 3 integers
Outline
TwoDimensionalAr
rays.cs
Program Output
36
37 // output values in array1
38 for ( int i = 0; i < array1.Length; i++ )
39 {
40 for ( int j = 0; j < array1[i].Length; j++ )
41 outputLabel.Text += array1[ i, j ] + " ";
42
43 outputLabel.Text += "\n";
44 }
45
46 outputLabel.Text += "\nValues in array2 by row are\n";
47
48 // output values in array2
49 for ( int i = 0; i < array2.Length; i++ )
50 {
51 for ( int j = 0; j < array2[ i ].Length; j++ )
52 outputLabel.Text += array2[ i ][ j ] + " ";
53
54 outputLabel.Text += "\n";
55 }
56
57 } // end method showOutputButton_Click
58
59 } // end class TwoDimensionalArrays
pf2

Partial preview of the text

Download Multi-dimensional Arrays: Rectangular and Jagged Arrays in CS112 and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #22:

Multi-dimensional Arrays

http://flint.cs.yale.edu/cs112/

Two-Dimensional Arrays

r A one-dimensional array stores a list of

values

r A two-dimensional array, also called double-

subscripted array, can be thought of as a

table of values, with rows and columns

m a two-dimensional array element is referenced

using two index numbers

Two Types of Double-Subscripted Arrays

r rectangular arrays

m often represent tables in which each row is the same size and

each column is the same size, e.g.,

int[,] a1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

int[,] a11 = new int[3,4];

r jagged arrays

  • arrays of arrays
  • arrays that compose jagged arrays can be of different lengths,

e.g.,

int[][] array2 = new int[ 3 ][];

array2[ 0 ] = new int[] { 1, 2 };

array2[ 1 ] = new int[] { 3 };

array2[ 2 ] = new int[] { 4, 5, 6 };

array2[2][1] = 3;

Double-Subscripted Arrays

Double-subscripted array with three rows and four columns.

Row 0

Row 1

Row 2

Column 0 Column 1 Column 2 Column 3

a[0][0] a[0][1] a[0][2]a[0][3]

a[1][0] a[1][1] a[1][2]a[1][3]

a[2][0] a[2][2]a[2][3]

Column index (or subscript)

Row index (or subscript)

Array name

a [2][1]

Outline

TwoDimensionalAr

rays.cs

1 // Fig. 7.14: TwoDimensionalArrays.cs 2 // Initializing two-dimensional arrays. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class TwoDimensionalArrays : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 [STAThread] 18 static void Main() 19 { 20 Application.Run( new TwoDimensionalArrays() ); 21 } 22 23 private void showOutputButton_Click( object sender, 24 System.EventArgs e ) 25 { 26 // declaration and initialization of rectangular array 27 int[,] array1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; 28 29 // declaration and initialization of jagged array 30 int[][] array2 = new int[ 3 ][]; 31 array2[ 0 ] = new int[] { 1, 2 }; 32 array2[ 1 ] = new int[] { 3 }; 33 array2[ 2 ] = new int[] { 4, 5, 6 }; 34 35 outputLabel.Text += "Values in array1 by row are\n";

Declare and initialize a

rectangular integer array

named array

Declare a jagged

array named

array2 with 3 rows

Initialize the first

element in array2 to be

an array that contains

two integers

Initialize the second element in

array2 to be an array that

contains 1 integer

Initialize the third element

in array2 to be an array that

contains 3 integers

Outline

TwoDimensionalAr

rays.cs

Program Output

37 // output values in array 38 for ( int i = 0; i < array1.Length; i++ ) 39 { 40 for ( int j = 0; j < array1[i].Length; j++ )

41 outputLabel.Text += array1[ i, j ] + " ";

43 outputLabel.Text += "\n"; 44 } 45 46 outputLabel.Text += "\nValues in array2 by row are\n"; 47 48 // output values in array 49 for ( int i = 0; i < array2.Length; i++ ) 50 { 51 for ( int j = 0; j < array2[ i ].Length; j++ )

52 outputLabel.Text += array2[ i ][ j ] + " ";

54 outputLabel.Text += "\n"; 55 } 56 57 } // end method showOutputButton_Click 58 59 } // end class TwoDimensionalArrays

Outline

DoubleArray.cs

1 // Fig. 7.15: DoubleArray.cs 2 // Manipulating a double-subscripted array. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class DoubleArray : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 private int[][] grades; 16 private int students, exams; 17 18 // Visual Studio .NET generated code 19 20 [STAThread] 21 static void Main() 22 { 23 Application.Run( new DoubleArray() ); 24 } 25 26 private void showOutputButton_Click( object sender, 27 System.EventArgs e ) 28 29 { 30 grades = new int[ 3 ][]; 31 grades[ 0 ] = new int[]{ 77, 68, 86, 73 }; 32 grades[ 1 ] = new int[]{ 96, 87, 89, 81 }; 33 grades[ 2 ] = new int[]{ 70, 90, 86, 81 }; 34

Initialize array grades to have 3 rows

Initialize each element in array grades

Outline

DoubleArray.cs

35 students = grades.Length; // number of students 36 exams = grades[ 0 ].Length; // number of exams 37 38 // line up column headings 39 outputLabel.Text += " "; 40 41 // output the column headings 42 for ( int i = 0; i < exams; i++ ) 43 outputLabel.Text += "[" + i + "] "; 44 45 // output the rows 46 for ( int i = 0; i < students; i++ ) 47 { 48 outputLabel.Text += "\ngrades[" + i + "] "; 49 50 for ( int j = 0; j < exams; j++ ) 51 outputLabel.Text += grades[ i ][ j ] + " "; 52 } 53 54 outputLabel.Text += "\n\nLowest grade: " + Minimum() + 55 "\nHighest grade: " + Maximum() + "\n"; 56 57 for ( int i = 0; i < students; i++ ) 58 outputLabel.Text += "\nAverage for student " + i + " is " + 59 Average( grades[ i ] ); 60 61 } // end method showOutputButton_Click 62

Output each row

Output each

element of the row

Output the minimum and

maximum grades

Output the average for each

row

Outline

DoubleArray.cs

63 // find minimum grade in grades array 64 public int Minimum() 65 { 66 int lowGrade = 100; 67 68 for ( int i = 0; i < students; i++ ) 69 70 for ( int j = 0; j < exams; j++ ) 71 72 if ( grades[ i ][ j ] < lowGrade ) 73 lowGrade = grades[ i ][ j ]; 74 75 return lowGrade; 76 } 77 78 // find maximum grade in grades array 79 public int Maximum() 80 { 81 int highGrade = 0; 82 83 for ( int i = 0; i < students; i++ ) 84 85 for ( int j = 0; j < exams; j++ ) 86 87 if ( grades[ i ][ j ] > highGrade ) 88 highGrade = grades[ i ][ j ]; 89 90 return highGrade; 91 } 92

Examine each element

in grades array

If the current array element is less

then the lowest grade, set the value of

lowGrade to be the current element

Examine each element in

grades array

If the current array element higher

than the highest grade, set the value

of highGrade to be the current

element

Outline

DoubleArray.cs

Program Output

93 // determine average grade for a particular student 94 public double Average( int[] setOfGrades ) 95 { 96 int total = 0; 97 98 for ( int i = 0; i < setOfGrades.Length; i++ ) 99 total += setOfGrades[ i ]; 100 101 return ( double ) total / setOfGrades.Length; 102 } 103 104 } // end class DoubleArray

Total the grades for the array

Divide the total by the

number of grades