Beruflich Dokumente
Kultur Dokumente
Topic 7 - Arrays
Outline
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
7.10
Introduction
Arrays
Declaring and Allocating Arrays
Examples Using Arrays
7.4.1
Allocating an Array and Initializing Its Elements
7.4.2
Totaling the Elements of an Array
7.4.5
Using Arrays to Analyze Survey Results
7.4.3
Using Histograms to Display Array Data Graphically
7.4.4
Using the Elements of an Array as Counters
7.4.5
Using Arrays to Analyze Survey Results
Passing Arrays to Methods
Passing Arrays by Value and by Reference
Sorting Arrays
Searching Arrays: Linear Search and Binary Search
7.8.1
Searching an Array with Linear Search
7.8.2
Searching a Sorted Array with Binary Search
Multiple-Subscripted Arrays
for/each Repetition Structure
7.1 Introduction
Data structures
7.2 Arrays
A group of contiguous memory locations
Same name
Same type
7.2 Arrays
Name of array (Note that
all elements of this array
have the same name, c)
Fig. 7.1
A 12-element array.
c[ 0 ]
-45
c[ 1 ]
c[ 2 ]
c[ 3 ]
72
c[ 4 ]
1543
c[ 5 ]
-89
c[ 6 ]
c[ 7 ]
62
c[ 8]
-3
c[ 9 ]
c[ 10 ]
6453
c[ 11 ]
-78
7.2 Arrays
Operators
Associativity
Type
++ -- + - ! (type)
right to left
left to right
multiplicative
left to right
additive
left to right
relational
== !=
left to right
equality
&
left to right
left to right
left to right
&&
left to right
logical AND
||
left to right
logical OR
?:
right to left
conditional
= += -= *= /= %=
right to left
assignment
() [] . ++ --
Fig. 7.2
left to right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Outline
InitArray.cs
Allocate x to be
of size 10
Declare an integer
array x
an integer array z
Initialize z to be of size
ARRAY_SIZE
34
35
36
37
38
39
40
41
42
43
44
45
Outline
InitArray.cs
Add values in the arrays to
output
Program Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
class SumArray
{
// main entry point for application
Total the
static void Main( string[] args )
array a
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
SumArray.cs
contents of
Program Output
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Outline
class Histogram
{
// main entry point for application
static void Main( string[] args )
{
int[] n = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
string output = "";
Histogram.cs
output += "Element\tvalue\tHistogram\n";
// build output
for ( int i = 0; i < n.Length; i++ )
{
output += "\n" + i + "\t" + n[ i ] + "\t";
11
Outline
Histogram.cs
Program Output
12
13
14
15
16
Easy to program
Runs slowly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Outline
BubbleSorter.cs
[STAThread]
static void Main()
{
Application.Run( new BubbleSorter() );
}
17
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Outline
18
// one pass
// one comparison
// one swap
Perform
contents of for loop
Perform b.Length-1
passes
for each element of array b
Program Output
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Outline
21
LinearSearcher.c
s
int[] a = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,
28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 };
// Visual Studio .NET generated code
[STAThread]
static void Main()
{
Application.Run( new LinearSearcher() );
}
private void searchButton_Click( object sender,
System.EventArgs e )
{
int searchKey = Int32.Parse( inputTextBox.Text );
int elementIndex = LinearSearch( a, searchKey );
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Outline
if ( elementIndex != -1 )
outputLabel.Text =
"Found value in element " + elementIndex;
LinearSearcher.c
s
else
outputLabel.Text = "Value not found";
} // end method searchButton_Click
22
return -1
Program Output
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Outline
System;
System.Drawing;
System.Collections;
System.ComponentModel;
System.Windows.Forms;
System.Data;
BinarySearchTest
.cs
24
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Outline
BinarySearchTest
.cs
if ( element != -1 )
Compute midpoint of
displayLabel.Text = "Found value in element " +
element;
current search space
else
Call method BinarySearch on
displayLabel.Text = "Value not found";
} // end findButton_Click
25
then
found
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Outline
26
BinarySearchTest
.cs
if ( key == array[ middle ] )
// match
return middle;
Output all elements of the
else if ( key < array[ middle ] )
high = middle - 1;
// search low end of array array within two indices and
else
mark the middle element.
low = middle + 1;
Print spaces
If the middle element matches
the for the other
elements
// end BinarySearch
search key, return the index
of the
middle element
return -1;
99
100
101
102
103
104
105
106
107
108
else
outputLabel.Text +=
array[ i ].ToString( "00" ) + "
}
outputLabel.Text += "\n";
Outline
27
";
BinarySearchTest
.cs
} // end BuildOutput
} // end class BinarySearchTest
Program Output
Outline
28
BinarySearchTest
.cs
Program Output
29
Jagged Arrays
Arrays of arrays
Arrays that compose jagged arrays can be of different
lengths
2002 Prentice Hall. All rights reserved.
30
Column 1
Column 2
Column 3
Row 0
a[0, 0]
a[0, 1]
a[0, 2]
a[0, 3]
Row 1
a[1, 0]
a[1, 1]
a[1, 2]
a[1, 3]
Row 2
a[2, 0]
a [2, 1] a[2, 2]
a[2, 3]
Fig. 7.13 Double-subscripted array with three rows and four columns.
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
class ForEach
{
// main entry point for the application
static void Main( string[] args )
{
int[,] gradeArray = { { 77, 68, 86, 73 },
{ 98, 87, 89, 81 }, { 70, 90, 86, 81 } };
ForEach.cs
32