Sie sind auf Seite 1von 114

1

7
arrays und
vectors,
Suchen und
Sortieren
2006 Pearson Education, Inc. All rights reserved.
2

7.1 Einfhrung
7.2 Arrays
7.3 Deklaration von arrays
7.4 Beispiele zum Gebrauch von arrays
7.5 bergabe von arrays an Funktionen
7.6 Fallstudie: Die Klasse GradeBook mit einem array zum
Abspeichern von Noten
7.7 Einfhrung in die C++ STL Klasse vector
7.8 Suchen und Sortieren
7.8.1 Lineare Suche
7.8.2 Binre Suche
7.8.3 Sortieren durch direktes Einfgen (Insertion-Sort)
7.8.4 Merge-Sort (rekursive Implementierung)
7.9 Mehrdimensionale arrays
7.10 Fallstudie: Die Klasse GradeBook mit einem zweidimensionalen
array

2006 Pearson Education, Inc. All rights reserved.


3

7.1 Einfhrung

Array
Datenstruktur, die zusammengehrige Datenelemente
desselben Typs enthlt
Datenstruktur der Sprache C++ (STL-Klasse') in zwei
Varianten:
array: Behlt immer die Gre, in der es vom Compiler
whrend des bersetzungsvorgangs erzeugt wurde
Weniger komfortabel zu nutzen, aber schneller und
weniger Speicherverbrauch (normalerweise auf Stack)
vector: Gre kann whrend der Programmausfhrung
verndert werden
Bei Bedarf automatisches Wachsen und Schrumpfen,
aber langsamer und mehr Speicherverbrauch (auf Heap)

2006 Pearson Education, Inc. All rights reserved.


4

7.1 Einfhrung

Arrays in C++ und C


Statt der in diesem Kapitel behandelten C++-Arrays (array
und vector) kann in C++ auch das eingebaute (built-in)
Array aus C verwendet werden.
Diese C-Arrays sind unsicherer und weniger komfortabel zu
nutzen.
Wegen ihrer Bedeutung fr einige Anwendungsgebiete (s. z.B.
Veranstaltung Betriebssysteme) werden sie im Kapitel 8 im
Zusammenhang mit Zeigern noch ausfhrlich besprochen.

2006 Pearson Education, Inc. All rights reserved.


5

7.2 Arrays
Array (array, vector und C-Array)
Gruppe aufeinanderfolgender Speicherpltze
Alle haben denselben Typ.
Index
Ganzzahliger Positionszhler, der einen spezifischen Platz, d.h ein
spezifisches Element im Array anspricht
Wird in eckige Klammern [ ] gesetzt
Muss positive ganze Zahl oder ganzzahliger Ausdruck sein
Das erste Element hat den Index 0.

2006 Pearson Education, Inc. All rights reserved.


6

Fig.7.1 | Array mit 12 Elementen

2006 Pearson Education, Inc. All rights reserved.


7

7.2 Arrays

Fr das Array c in Fig. 7.1 gilt


c ist der Arrayname
c hat 12 Elemente ( c[0], c[1], c[11] )
Der Wert von c[0] ist 45
Die eckigen Klammern, die einen Arrayindex
umschlieen, sind in C++ ein Operator.

2006 Pearson Education, Inc. All rights reserved.


8

Hufiger Programmierfehler

Es ist wichtig, sich den Unterschied zwischen


dem siebten Element des Arrays und dem
Array Element mit Index 7 klar zu machen:
Array Indices beginnen mit 0, so dass das
siebte Element des Arrays den Index 6 hat,
whrend das Array Element mit Index 7 das
achte Element des Arrays ist.
Dieser Unterschied fhrt hufig zu einem
Eins-daneben-Fehler (off-by-one error).

2006 Pearson Education, Inc. All rights reserved.


9

Operatoren Assoziativitt Typ

() [] links nach rechts Klammern, Index


++ -- static_cast< Typ >( Operand ) links nach rechts unr (postfix)
++ -- + - ! rechts nach links unr (prefix)
* / % links nach rechts multiplikativ
+ - links nach rechts additiv
<< >> links nach rechts Ausgabe / Eingabe
< <= > >= links nach rechts Vergleich
== != links nach rechts Gleichheit
&& links nach rechts logisches UND
|| links nach rechts logisches ODER
?: rechts nach links Bedingung
= += -= *= /= %= rechts nach links Zuweisung
, links nach rechts Komma

Fig.7.2 | Operatorvorrang und Assoziativit.

2006 Pearson Education, Inc. All rights reserved.


10

7.3 Deklaration von arrays


arrays belegen Platz im Speicher.
Vom Programmierer werden Typ und Anzahl der
Elemente als Typparameter des Klassentemplates array
angegeben:
array< int, 12 > c;
c ist ein Array von 12 ints
Die arraygre muss eine ganzzahlige Konstante grer
als Null sein.
arrays knnen Werte jedes Datentyps (auch selbst-
definierte Klassen) enthalten, jedoch keine Referenzen.
Mehrere arrays des gleichen Typs und der gleichen
Elementanzahl knnen in einer einzigen Deklaration
deklariert werden.
Mit einer kommaseparierten Liste der Namen

2006 Pearson Education, Inc. All rights reserved.


11

7.4 Beispiele zum Gebrauch von arrays

Einsatz einer Schleife zur Initialisierung der


arrayelemente
Deklaration des arrays mit Angabe von Typ und Anzahl
der Elemente
Wiederholungsanweisung, um jedes Arrayelement
anzusprechen
Im Krper der Wiederholungsanweisung wird jedes
einzelne Arrayelement initialisiert.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.3: fig07_03.cpp 12
2 // Initializing an array's elements to zeros and printing the array. Outline
3 #include <iostream>
4 #include <iomanip>
5 #include <array>
6 using namespace std; fig07_03.cpp
7
8 int main() (1 von 1)
9 {
10 array< int, 10 > n; // n is an array of 10 integers
11 // initialize elements of array n to 0
12 for( size_t i = 0; i < n.size(); ++i ) {
13 n[ i ] = 0; // set element at location i to 0
14 }
15 cout << "Element" << setw( 13 ) << "Value" << endl;
16 // output each array element's value
17 for( size_t j = 0; j < n.size(); ++j ) {
18 cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
19 }
20 } // end main

Element Value
0 0
1 0 n[ j ] gibt den int zurck, der dem
2 0 Index j im Array n zugeordnet ist.
3 0
4 0
5 0
6 0
7 0
8 0 Jeder int wurde mit 0 initialisiert.
9 0 2006 Pearson Education,
Inc. All rights reserved.
13

7.4 Beispiele zum Gebrauch von arrays

Die Kontrollvariablen i und j, die die Indices der


arrayelemente bezeichnen, werden mit dem Typ
size_t deklariert.
size_t stellt laut C++ Standard einen ganzzahligen Typ
ohne Vorzeichen dar.
Dieser Typ soll fr jede Variable, die die Gre oder die
Indices von Arrays repsentiert, verwendet werden.
size_t ist definiert im Namensbereich std und im
Header <cstddef>, der von verschiedenen anderen
Headern eingeschlossen wird.

2006 Pearson Education, Inc. All rights reserved.


14

7.4 Beispiele zum Gebrauch von arrays

Initialisierung eines arrays bei der Deklaration


mit Hilfe einer Initialisierungsliste
Initialisierungsliste
Werte werden in geschweifte Klammern ({}) eingeschlossen.
Einzelne Werte in der Liste werden durch Kommas getrennt.
Beispiel
array< int, 5 > n = { 10, 20, 30, 40, 50 };
Es wird ein Array mit 5 Elementen erzeugt.
Die Indexwerte sind 0, 1, 2, 3, 4.
Die Werte werden mit 10, 20, 30, 40, 50
initialisiert.

2006 Pearson Education, Inc. All rights reserved.


15

7.4 Beispiele zum Gebrauch von arrays

Initialisierung eines Arrays bei der Deklaration mit


Hilfe einer Initialisierungsliste
Falls weniger Initialisierungswerte angegeben werden als
Elemente im Array vorhanden sind, werden die restlichen
Elemente entsprechend ihres Typs wertinitialisiert
( int z.B. mit 0 ).
Beispiel
array< int, 10 > n = { 0 };
Initialisiert das erste Element explizit mit 0.
Initialisiert die restlichen neun Elemente implizit mit 0.
Bei einer leeren Initialisierungsliste {} werden alle
Elemente implizit wertinitialisiert.

2006 Pearson Education, Inc. All rights reserved.


16

Hufiger Programmierfehler

Falls mehr Initialisierungswerte angegeben


werden als Elemente im Array vorhanden
sind, fhrt dies zu einer Fehlermeldung des
Compilers.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.4: fig07_04.cpp 17
2 // Initializing an array in a declaration. Outline
3 #include <iostream>
4 #include <iomanip>
5 #include <array>
6 using namespace std; fig07_04.cpp
7
8 int main() (1 von 1)
9 {
10 // use initializer list to initialize array n
11 array< int, 10 > n = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
12
13 cout << "Element" << setw( 13 ) << "Value" << endl;
14
15 // output each array element's value
16 for( size_t i = 0; i < n.size(); ++i ) {
17 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;
18 }
19 } // end main

Element Value
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37
2006 Pearson Education,
Inc. All rights reserved.
18

7.4 Beispiele zum Gebrauch von arrays

Beispiel: Festlegen der arraygre mit einer


konstanten Variablen und Setzen der
arrayelemente aufgrund von Berechnungen
Ein 10-elementiges array soll mit geraden Zahlen
(beginnend mit 2) gefllt werden.

Fr die Festlegung der Gre des arrays kann eine als


const deklarierte Variable verwendet werden.
Dann wird eine Wiederholungsstruktur benutzt, die den
Wert fr das aktuelle Element berechnet und das
arrayelement mit dem berechneten Wert initialisiert.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.5: fig07_05.cpp
19
2 // Set array s to the even integers from 2 to 20.
3 #include <iostream>
Outline
4 #include <iomanip>
5 #include <array>
6 using namespace std;
fig07_05.cpp
7
8 int main()
9 {
(1 von 1)
10 // constant variable can be used to specify array size
11 const size_t arraySize = 10; Soll die Arraygre mit einer Variablen festgelegt
12 array< int, arraySize > s; // array s has 10 elements
werden, muss diese als const erklrt werden.
13
14 for( size_t i = 0; i < arraySize; ++i ) { // set the values
15 s[ i ] = 2 + 2 * i;
16 }
Die Schleifenvariable wird als Arrayindex
17 cout << "Element" << setw( 13 ) << "Value" << endl; und als Rechengre verwendet.
18 // output contents of array s in tabular format
19 for( size_t j = 0; j < arraySize; ++j ) {
20 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
21 }
22 } // end main

Element Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20 2006 Pearson Education,
Inc. All rights reserved.
20

7.4 Beispiele zum Gebrauch von arrays

Konstante Variablen
Deklaration unter Verwendung des const Qualifizierers
Auch als Namenskonstanten oder Read-Only-Variablen
bezeichnet
Mssen zum Zeitpunkt ihrer Deklaration mit einem
konstanten Ausdruck initialisiert werden und knnen
danach nicht mehr modifiziert werden
Knnen berall dort stehen, wo ein konstanter Ausdruck
erwartet wird

2006 Pearson Education, Inc. All rights reserved.


21

Hufiger Programmierfehler

Wird einer konstanten Variablen bei ihrer


Deklaration kein Wert zugewiesen, fhrt dies
zu einer Fehlermeldung des Compilers.
Wird einer konstanten Variablen in einer
ausfhrbaren Anweisung ein Wert zugewiesen,
fhrt dies zu einer Fehlermeldung des
Compilers.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.7: fig07_07.cpp 22
// A const variable must be initialized.
2
Outline
3
4 int main()
5 {
6 const int x; // Error: x must be initialized
fig07_07.cpp
7
8 x = 7; // Error: cannot modify a const variable
(1 von 1)
9 } // end main

Fehlermeldung des Microsoft Visual C++ Compilers:

C:\fig07_07.cpp(6) : error C2734: 'x' : const object must be initialized


if not extern
C:\fig07_07.cpp(8) : error C2166: l-value specifies const object

Fehlermeldung des GNU C++ Compilers:

fig07_07.cpp:6: error: uninitialized const `x'


fig07_07.cpp:8: error: assignment of read-only variable `x'

2006 Pearson Education,


Inc. All rights reserved.
23

Hufiger Programmierfehler

Nur Konstanten knnen zur Deklaration der


Gre von arrays verwendet werden.
Wird hierfr keine Konstante verwendet, fhrt
dies zu einer Fehlermeldung des Compilers.

Soll die Arraygre vernderbar sein, muss ein


vector verwendet werden (s. Abschnitt 7.7) .

2006 Pearson Education, Inc. All rights reserved.


24

Betrachtung zum Software Engineering

Die Festlegung von arraygren mit konstanten


Variablen anstelle von Literalkonstanten macht
Programme skalierbarer.
Soll das array bzw. mehrere gleich groe arrays
eine andere Gre bekommen, muss nur an einer
einzigen Stelle im Programm die Definition der
konstanten Variablen gendert werden.

2006 Pearson Education, Inc. All rights reserved.


25

7.4 Beispiele zum Gebrauch von arrays

Aufsummieren der Elemente eines arrays


arrayelemente knnen eine Folge von Werten darstellen.
Diese Werte knnen wie folgt aufsummiert werden:
Innerhalb einer Schleife wird jedes arrayelement
angesprochen.
Der Wert jedes Elementes wird zu einer laufenden
Summe aufaddiert.

2006 Pearson Education, Inc. All rights reserved.


26

7.4 Beispiele zum Gebrauch von arrays

Bereichsbasierte for-Schleife
Um ber alle Elemente in einem Array (bzw. allgemeiner:
Container) zu iterieren, gibt es eine krzere Form der for-
Schleife, die bereichsbasierte for-Schleife :
for( int n : numbers ) {
cout << n << ' ';
}
Alle Elemente des arrays numbers werden der Reihe nach
ber den Bezeichner n angesprochen.
Hierdurch ist die Definition eines Zhlers zum Durchlaufen des
Arrays nicht mehr ntig und die hufige Fehlerquelle, dass
dieser Zhler den erlaubten Bereich fr die Indices verlsst,
wird vermieden.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.8: fig07_08.cpp 27
2 // Compute the sum of the elements of an array. Outline
3 #include <iostream>
4 #include <array>
5 using namespace std;
6 fig07_08.cpp
7 int main()
8 { (1 von 1)
9 const size_t arraySize = 10; // specifies size of array
10 array< int, arraySize > numbers = { 42, 15, 94, 8, 23, 78, 4, 91, 16, 87 };
11 int total = 0;
12
13 // sum contents of array numbers
14 for( int n : numbers ) {
15 total += n;
16 }
17
18 cout << "Total of array elements: " << total << endl;
19 } // end main

Total of array elements: 458

2006 Pearson Education,


Inc. All rights reserved.
28

7.4 Beispiele zum Gebrauch von arrays

Graphische Darstellung von arraydaten durch


Histogramme
Eine einfache Darstellung der Werte in einem array kann
durch ein um 90 Grad gedrehtes Histogramm, das z.B. aus
Sternchen (*) aufgebaut wird, realisiert werden.
Im folgenden Beispiel wird dies fr eine Verteilung von
Prozentpunkten auf elf Bereiche demonstriert.
Geschachtelte for Anweisungen steuern die Ausgabe des
Histogramms.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.9: fig07_09.cpp
29
// Bar chart printing program.
2
Outline
3 #include <iostream>
4 #include <iomanip>
5 #include <array>
6 using namespace std; fig07_09.cpp
7
8 int main() (1 von 2)
9 {
10 const size_t size = 11;
11 array< unsigned, size > numbers = { 0, 0, 0, 0, 4, 15, 23, 42, 16, 8, 0 };
12
13 cout << "Grade distribution:" << endl;
14 // for each element of array numbers, output a bar of the chart
15 for( size_t i = 0; i < size; ++i ) {
16 // output bar labels ("0-9:", ..., "90-99:", "100:" )
17 if( 0 == i )
18 cout << " 0-9: ";
19 else if( 10 == i )
20 cout << " 100: ";
21 else
22 cout << i * 10 << "-" << ( i * 10 ) + 9 << ": ";
23 // print bar of asterisks
24 for( unsigned stars = 0; stars < numbers[ i ]; ++stars ) {
Fr jedes Arrayelement wird die
25 cout << '*';
entsprechende Anzahl von
26 }
27 cout << endl; // start a new line of output
Sternchen ausgegeben
28 } // end outer for
29 } // end main
2006 Pearson Education,
Inc. All rights reserved.
Grade distribution: 30
0-9: Outline
10-19:
20-29:
30-39:
40-49: ****
fig07_09.cpp
50-59: ***************
60-69: ***********************
70-79: ****************************************** (2 von 2)
80-89: ****************
90-99: ********
100:

2006 Pearson Education,


Inc. All rights reserved.
31

7.4 Beispiele zum Gebrauch von arrays

Die Elemente eines arrays als Zhler verwenden


Ein array soll zur Zhlung von ganzzahligen Gren
verwendet werden (Beispiel: Hufigkeit der Augenzahlen
eines Wrfels).
Die Gren, die gezhlt werden sollen, spielen die Rolle der
Indices des arrays.
Immer wenn eine Gre als Index benutzt wird, wird der
zugehrige arraywert um Eins erhht.
Dadurch entsteht die Hufigkeitsverteilung der zu zhlenden
Gren in dem array.
Im folgenden Beispiel wird dies fr die Hufigkeitsverteilung
beim Wrfeln mit einem Wrfel demonstriert.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.10: fig07_10.cpp
32
2 // Frequency of rolling a six-sided die 6,000,000 times using an array.
3 #include <iostream>
Outline
4 #include <iomanip>
5 #include <array>
6 #include <random>
fig07_10.cpp
7 #include <ctime>
8 using namespace std;
9
(1 von 2)
10 int main()
11 {
12 default_random_engine engine( static_cast< unsigned >( time( nullptr ) ) );
13 uniform_int_distribution< unsigned > randomInt( 1, 6 );
14
15 const size_t arraySize = 7; // ignore element zero
16 array< unsigned, arraySize > frequency = { 0 }; // initialize to 0s
17
18 // roll die 6,000,000 times; use die value as frequency index
19 for( unsigned roll = 1; roll <= 6000000; ++roll ) {
20 ++frequency[ randomInt( engine ) ];
Inkrementierung des frequency Wertes
21 } an der Indexposition, die durch die
22 cout << "Face" << setw( 13 ) << "Frequency" << endl; Zufallszahl ausgewhlt wird
23 // output each array element's value
24 for( size_t face = 1; face < arraySize; ++face ) {
25 cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ]
26 << endl;
27 }
28 } // end main

2006 Pearson Education,


Inc. All rights reserved.
Face Frequency 33
1 1000167 Outline
2 1000149
3 1000152
4 998748
5 999626
6 1001158 fig07_10.cpp

(2 von 2)

2006 Pearson Education,


Inc. All rights reserved.
34

7.4 Beispiele zum Gebrauch von arrays

Aufsummieren von Umfrageresultaten mit arrays


40 Studenten bewerten das Mensaessen
Skala von 1-10 :
1 bedeutet furchtbar, 10 bedeutet exzellent
Die 40 Antworten werden in einem int-array gespeichert.
Zur Erstellung einer Zusammenfassung der Ergebnisse, d.h.
einer Hufigkeitsverteilung, wird jedes Element des arrays
als Zhler fr eines der mglichen Umfrageresultate
verwendet.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.11: fig07_11.cpp
35
2 // Student poll program.
3 #include <iostream>
Outline
4 #include <iomanip>
5 #include <array>
6 using namespace std;
fig07_11.cpp
7
8 int main()
9 {
(1 von 2)
10 const size_t responseSize = 40; // size of array responses
11 const size_t frequencySize = 11; // size of array frequency
12
13 // place survey responses in array responses
14 const array< unsigned int, responseSize > responses = { 1, 2, 6, 4,
15 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8,
16 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
17
18 // initialize frequency counters to 0
19 array< unsigned int, frequencySize > frequency = { 0 };
20
21 // for each answer, select responses element and use that value
22 // as frequency subscript to determine element to increment
23 for( size_t answer = 0; answer < responseSize; ++answer ) { Fr jede Antwort wird der
24 ++frequency[ responses[ answer ] ];
frequency Wert an der zugehrigen
25 }
26 cout << "Rating" << setw( 17 ) << "Frequency" << endl;
Indexposition inkrementiert
27 // output each array element's value
28 for( size_t rating = 1; rating < frequencySize; ++rating ) {
29 cout << setw( 6 ) << rating << setw( 17 ) << frequency[ rating ]
30 << endl;
31 }
2006 Pearson Education,
32 } // end main Inc. All rights reserved.
Rating Frequency 36
1 2 Outline
2 2
3 2
4 2
5 5
6 11
fig07_11.cpp
7 5
8 7 (2 von 2)
9 1
10 3

2006 Pearson Education,


Inc. All rights reserved.
37

7.5 bergabe von arrays an Funktionen

Funktionsprototypen von Funktionen, die arrays


als Argumente bernehmen
Die Parameterliste der Funktion muss einen arrayparameter
festlegen.
Beispiel:
void modifyArray( array< int, arraySize > );
Der arrayparameter muss Typ und Anzahl der Elemente
einschlieen.
Nur dann ist das Klassentemplate array ein vollstndiger
Typ.

2006 Pearson Education, Inc. All rights reserved.


38

7.5 bergabe von arrays an Funktionen

bergabe eines Arrays als Argument an eine


Funktion
Angabe des Arraynamens (ohne weitere Informationen)
Array a ist deklariert als
array< int, arraySize > a;
Der Funktionsaufruf
modifyArray( a );
bergibt das array a einschlielich der Information ber
seine Gre an die Funktion modifyArray.
Die Arraygre ist in der Funktion z.B. ber a.size() -
verfgbar.

2006 Pearson Education, Inc. All rights reserved.


39

7.5 bergabe von arrays an Funktionen

arrays werden wertmig bergeben.


Der Funktionsaufruf bergibt eine Kopie des arrays an
die aufgerufene Funktion.
nderungen innerhalb der Funktion wirken sich nur auf
die Kopie aus, das Original im aufrufenden Programmteil
bleibt unverndert.
Das gilt genauso fr vector und alle anderen STL-
Container.
Im Gegensatz dazu werden C-Arrays wegen ihres simpleren
Aufbaus referenzmig an Funktionen bergeben (Details
hierzu s. Kapitel 8).

2006 Pearson Education, Inc. All rights reserved.


40

7.5 bergabe von arrays an Funktionen

Einzelne Arrayelemente werden auch wertmig


bergeben.
Sie werden als Skalare oder skalare Gren bezeichnet.
Um ein einzelnes Element an eine Funktion zu
bergeben, muss der indizierte Name des array-
elements als Argument verwendet werden.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.14: fig07_14.cpp
41
2 // Passing arrays and individual array elements to functions.
3 #include <iostream>
Outline
4 #include <iomanip>
5 #include <array>
6 using namespace std;
fig07_14.cpp
7
8 const int arraySize = 5; // size of array a; global CONSTANT ist okay
9
(1 von 3)
10 void modifyArray( array< int, arraySize > ); // receive array
11 void modifyElement( int ); // receive array element value
12
13 int main()
Funktion bernimmt ein array als Argument
14 {
15 array< int, arraySize > a = { 0, 1, 2, 3, 4 }; // initialize array a
16
17 cout << "Effects of passing entire array by value:"
18 << "\n\nThe values of the original array are:\n";
19 // output original array elements
20 for( const int& elem : a ) {
21 cout << setw( 3 ) << elem;
22 }
23
24 cout << endl;
25
26 // pass array a to modifyArray by value
27 modifyArray( a );
28 cout << "The values of the array after modifyArray are:\n";
29 // array elements are unaffected
30 for( const int& elem : a ) {
31 cout << setw( 3 ) << elem;
2006 Pearson Education,
32 } Inc. All rights reserved.
33
42
34 cout << "\n\n\nEffects of passing array element by value:"
35 << "\n\na[3] before modifyElement: " << a[ 3 ] << endl;
Outline
36
37 modifyElement( a[ 3 ] ); // pass array element a[ 3 ] by value
38 cout << "a[3] after modifyElement: " << a[ 3 ] << endl;
39 } // end main fig07_14.cpp
40
41 // in function modifyArray, "b" is a local copy of (2 von 3)
42 // the original array "a" passed from main
43 void modifyArray( array< int, arraySize > b )
44 {
45 for( int& elem : b ) {
46 elem *= 2; // multiply each array element by 2
47 }
48 cout << "The values of the array in modifyArray are:\n";
49 for( const int& elem : b ) {
50 cout << setw( 3 ) << elem;
51 }
52 cout << endl;
53 } // end function modifyArray
54
55 // in function modifyElement, "e" is a local copy of
56 // array element a[ 3 ] passed from main
57 void modifyElement( int e )
58 {
59 e *= 2; // multiply parameter by 2
60 cout << "Value of element in modifyElement: " << e << endl;
61 } // end function modifyElement

2006 Pearson Education,


Inc. All rights reserved.
Effects of passing entire array by value: 43
Outline
The values of the original array are:
0 1 2 3 4
The values of the array in modifyArray are:
0 2 4 6 8
The values of the array after modifyArray are: fig07_14.cpp
0 1 2 3 4
(3 von 3)
Effects of passing array element by value:

a[3] before modifyElement: 3


Value of element in modifyElement: 6
a[3] after modifyElement: 3

2006 Pearson Education,


Inc. All rights reserved.
44
7.6 Fallstudie: Die Klasse GradeBook mit
einem array zum Abspeichern von Noten
Klasse GradeBook
Stellt eine Notenliste dar, die Noten speichert und analysiert
Soll jetzt die Noten in einem array speichern
Statische (static) Datenelemente
Werden auch Klassenvariablen genannt
Variablen, fr die die Objekte einer Klasse keine eigenen Kopien
enthalten
Eine einzige Instanz einer Klassenvariablen wird von allen Objekten
der Klasse gemeinsam benutzt.
Auf diese Variablen kann auch zugegriffen werden, wenn keine Objekte
der Klasse existieren.
Dazu wird der Klassenname gefolgt vom Scopeoperator :: und dem
Namen des static Datenelements benutzt.
Ganzzahlige const static Datenelemente drfen innerhalb einer
Klassendefinition initialisiert und damit fr die Festlegung der Gre
eines gewhnlichen Arrays in der Klassendefinition verwendet werden.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.16: GradeBook.h
45
2 // Definition of class GradeBook that uses an array to store test grades.
3 // Member functions are defined in GradeBook.cpp
Outline
4
5 #include <string>
6 #include <array>
GradeBook.h
7
8 // GradeBook class definition
9 class GradeBook
(1 von 1)
10 {
11 public:
Anzahl der betrachteten Studenten
12 // constant -- number of students who took the test
13 static const size_t students = 10; // note public data
14
15 // constructor initializes course name and array of grades
16 GradeBook( const std::string &, const std::array< int, students > & );
17
18 void setCourseName( const std::string & ); // set the course name
19 std::string getCourseName() const; // retrieve the course name
20 std::string message() const; // return a welcome message
21 std::string processGradesResults() const; // perform operations on grade data
22 int getMinimum() const; // find the minimum grade for the test
23 int getMaximum() const; // find the maximum grade for the test
24 double getAverage() const; // determine the average grade for the test
25 std::string gradesContents() const; // contents of grades array as a string
26 std::string barChart() const; // return bar chart of grade distribution
27 private:
28 std::string courseName; // course name for this grade book
29 std::array< int, students > grades; // array of student grades
30 }; // end class GradeBook

2006 Pearson Education,


Inc. All rights reserved.
1 // Fig. 7.17: GradeBook.cpp
46
2 // Member-function definitions for class GradeBook that
3 // uses an array to store test grades.
Outline
4
5 #include <sstream>
6 #include <iomanip>
GradeBook.cpp
7 using namespace std;
8
9 #include "GradeBook.h" // GradeBook definition
(1 von 5)
10
11 // constructor initializes courseName and grades array
12 GradeBook::GradeBook( const string& name,
13 const array< int, students >& gradesArray )
14 : courseName( name ), grades( gradesArray )
15 {
16 } // end GradeBook constructor
17
18 // function to set the course name
19 void GradeBook::setCourseName( const string& name )
20 {
21 courseName = name; // store the course name
22 } // end function setCourseName
23
24 // function to retrieve the course name
25 string GradeBook::getCourseName() const
26 {
27 return courseName;
28 } // end function getCourseName

2006 Pearson Education,


Inc. All rights reserved.
32
47
33 // return a welcome message to the GradeBook user
34 string GradeBook::message() const
Outline
35 {
36 return "Welcome to the grade book for\n" + getCourseName() + "!\n\n";
37 } // end function message
GradeBook.cpp
38
39 // perform various operations on the data
40 string GradeBook::processGradesResults() const
(2 von 5)
41 {
42 stringstream resultStream; // write result as text in here
43
44 resultStream << gradesContents(); // write grades array as text
45
46 // call function getAverage to calculate the average grade
47 resultStream << "\nClass average is " << setprecision( 2 ) << fixed
48 << getAverage() << endl;
49
50 // call functions getMinimum and getMaximum
51 resultStream << "Lowest grade is " << getMinimum()
52 << "\nHighest grade is "<< getMaximum() << endl;
53
54 // call function barChart to write grade distribution chart as text
55 resultStream << barChart();
56
57 return resultStream.str(); // return result as a string
58 } // end function processGradesresults
59

2006 Pearson Education,


Inc. All rights reserved.
60 // find minimum grade 48
61 int GradeBook::getMinimum() const Outline
62 {
63 int lowGrade = 100; // assume lowest grade is 100 Schleife durch grades, um
64 // loop through grades array schlechteste Note zu finden
65 for( int grade : grades ) { GradeBook.cpp
66 // if current grade lower than lowGrade, assign it to lowGrade
67 if( grade < lowGrade ) (3 von 5)
68 lowGrade = grade; // new lowest grade
69 } // end for
70 return lowGrade; // return lowest grade
71 } // end function getMinimum
72
73 // find maximum grade
74 int GradeBook::getMaximum() const
75 {
76 int highGrade = 0; // assume highest grade is 0
Schleife durch grades,
77 // loop through grades array
um beste Note zu finden
78 for( int grade : grades ) {
79 // if current grade higher than highGrade, assign it to highGrade
80 if( grade > highGrade )
81 highGrade = grade; // new highest grade
82 } // end for
83 return highGrade; // return highest grade
84 } // end function getMaximum

2006 Pearson Education,


Inc. All rights reserved.
85
49
86 // determine average grade for test
87 double GradeBook::getAverage() const
Outline
88 {
89 int total = 0; // initialize total Schleife durch grades, um Noten
90 aller Studenten zu summieren GradeBook.cpp
91 // sum grades in array
92 for( int grade : grades ) {
93 total += grade;
(4 von 5)
94 }
95 // return average of grades
96 return static_cast< double >( total ) / students;
Durchschnittsnote: Teilen der Summe
97 } // end function getAverage durch die Anzahl der Studenten
98
99 // return the contents of the grades array as a string
100 string GradeBook::gradesContents() const
101 {
102 stringstream gradesStream; // write grades as text in here
103
104 gradesStream << "The grades are:\n\n";
105 // write each student's grade
106 for( size_t student = 0; student < students; ++student ) {
107 gradesStream << "Student " << setw( 2 ) << student + 1 << ": "
108 << setw( 3 ) << grades[ student ] << endl;
109 }
110 return gradesStream.str(); // return grades as a string
111 } // end function gradesContent
112

2006 Pearson Education,


Inc. All rights reserved.
113 // output bar chart displaying grade distribution
50
114 string GradeBook::barChart() const
115 {
Outline
116 stringstream chartStream; // write bar chart as text in here
117
118 chartStream << "\nGrade distribution:" << endl;
GradeBook.cpp
119 // stores frequency of grades in each range of 10 grades
120 const size_t frequencySize = 11;
121 array< unsigned int, frequencySize > frequency = { 0 };
(5 von 5)
122 // for each grade, increment the appropriate frequency
123 for( int grade : grades ) {
124 ++frequency[ grade / 10 ];
Schleife durch grades, um
125 } Hufigkeit zu berechnen
126 // for each grade frequency, write bar in chart
127 for( size_t count = 0; count < frequencySize; ++count ) {
128 // write bar labels ("0-9:", ..., "90-99:", "100:" )
129 if( 0 == count )
130 chartStream << " 0-9: ";
131 else if( 10 == count )
132 chartStream << " 100: ";
133 else
134 chartStream << count * 10 << "-" << ( count * 10 ) + 9 << ": ";
135 // write bar of asterisks
136 for( unsigned int stars = 0; stars < frequency[ count ]; ++stars ) {
137 chartStream << '*';
Histogramm aus
138 }
139 chartStream << endl; // start a new line of text
Sternchen ausgeben
140 } // end outer for
141 return chartStream.str(); // return bar chart as a string
142 } // end function barChart

2006 Pearson Education,


Inc. All rights reserved.
1 // Fig. 7.18: fig07_18.cpp 51
2 // Creates GradeBook object using an array of grades. Outline
3 #include <iostream>
4 #include <array>
5 using namespace std;
6 fig07_18.cpp
7 #include "GradeBook.h" // GradeBook definition
8 (1 von 2)
9 int main()
Verwendung des static Datenelements
10 {
students der Klasse GradeBook
11 // array of student grades
12 const array< int, GradeBook::students > grades =
13 { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
14
15 GradeBook myGradeBook(
16 "CS101 Introduction to C++ Programming", grades );
17 cout << myGradeBook.message();
18 cout << myGradeBook.processGradesResults();
19 } // end main
bergabe von grades an GradeBook Konstruktor

2006 Pearson Education,


Inc. All rights reserved.
Welcome to the grade book for 52
CS101 Introduction to C++ Programming! Outline
The grades are:

Student 1: 87
Student 2: 68 fig07_18.cpp
Student 3: 94
Student 4: 100
Student 5: 83
(2 von 2)
Student 6: 78
Student 7: 85
Student 8: 91
Student 9: 76
Student 10: 87

Class average is 84.90


Lowest grade is 68
Highest grade is 100

Grade distribution:
0-9:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: *
70-79: **
80-89: ****
90-99: **
100: *

2006 Pearson Education,


Inc. All rights reserved.
53
7.7 Einfhrung in die C++ STL Klasse
vector
Das Klassentemplate vector
Im Gegensatz zum array kann die Gre whrend der
Programmausfhrung verndert werden
Bei Bedarf automatisches Wachsen und Schrumpfen:
push_back: Ein Element wird am Ende eines vectors
eingefgt. Falls die Kapazitt des vectors fr ein weiteres
Element nicht ausreicht, wird er automatisch vergrert.
pop_back: Ein Element wird am Ende eines vectors entfernt.
back: Das letzte Element eines vectors wird zurckgegeben
(ohne es zu entfernen):
Durch den greren Verwaltungsaufwand ist ein vector
langsamer und hat mehr Speicherverbrauch als ein array.

2006 Pearson Education, Inc. All rights reserved.


54
7.7 Einfhrung in die C++ STL Klasse
vector
vector Elementfunktion at
Gestattet den Zugriff auf einzelne Elemente
Fhrt Bereichsberprfung durch
Wirft eine Ausnahme (throws an exception) in Form eines
Objekts einer passenden Fehlerklasse (out_of_range), wenn
der angegebene Index ungltig ist.
Wird der Aufruf von at in einen try-Block eingeschlossen,
kann diese Ausnahme in einem direkt folgenden catch-Block
aufgefangen und behandelt werden.
Der (auch mgliche) Zugriff mit eckigen Klammern [ ]
fhrt aus Grnden der Performanz keine
Bereichsberprfung durch!

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.19: fig07_19.cpp
55
2 // Demonstrating C++ Standard Library class template vector.
3 #include <iostream>
Outline
4 #include <iomanip>
5 #include <vector> Verwendung von const, damit
6 #include <array> outputVector einen bergebenen fig07_19.cpp
7 using namespace std;
vector nicht modifizieren kann
8
9 void outputVector( const vector< int > & ); // display the vector
(1 von 4)
10
11 int main() Diese vector-Objekte speichern ints.
12 {
13 vector< int > v1 = { 1, 2, 3, 4, 5, 6, 7 };
14 vector< int > v2 = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
15
16 // print v1 size and contents
17 cout << "Size of vector v1 is " << v1.size()
Funktion size gibt Anzahl der
18 << "\nvector after initialization:" << endl;
19 outputVector( v1 );
Elemente im vector zurck.
20
21 // print v2 size and contents
22 cout << "\nSize of vector v2 is " << v2.size()
23 << "\nvector after initialization:" << endl;
24 outputVector( v2 );
25
26 // add two elements at back of v1 Funktion push_back fgt ein
27 v1.push_back( 8 ); Element am Ende des vectors ein.
28 v1.push_back( 9 );
29 cout << "\n8 and 9 added at back of v1." << endl;

2006 Pearson Education,


Inc. All rights reserved.
29
56
30 // show and remove last element of v2
31 cout << "Last element of v2 will be removed: " << v2.back() << endl;
Outline
32 v2.pop_back();
33 back gibt das letzte Element
34 cout << "\nAfter modification, the vectors contain:\n" eines vectors zurck und
fig07_19.cpp
35 << "v1 ( size = " << v1.size() << " ):" << endl; pop_back entfernt es.
36 outputVector( v1 );
37 cout << "v2 ( size = " << v2.size() << " ):" << endl;
(2 von 4)
38 outputVector( v2 );
39
40 // use inequality (!=) operator with vector objects
41 cout << "\nEvaluating: v1 != v2" << endl;
42 if( v1 != v2 ) {
43 cout << "v1 and v2 are not equal" << endl;
44 }
45
46 // use square brackets to create rvalue
47 cout << "\nv1[5] is " << v1[ 5 ]; Einen Wert des vectors ausgeben
48
49 // use square brackets to create lvalue
50 cout << "\n\nAssigning 1000 to v1[5]" << endl;
51 v1[ 5 ] = 1000;
52 cout << "v1:" << endl; Einen Wert des vectors ndern
53 outputVector( v1 );

2006 Pearson Education,


Inc. All rights reserved.
54
57
55 // attempttry und out-of-range
to use catch-Block fr eine
subscript geregelte Ausnahmebehandlung Outline
56 try {
57 cout << "\nAttempt to display v1.at( 9 )" << endl;
58 cout << v1.at( 9 ) << endl; // ERROR: out of range
59 }
Funktion at fhrt Bereichsberprfung durch fig07_19.cpp
60 catch( out_of_range& ex ) {
61 cerr << "An exception occurred: " << ex.what() << endl;
62 }
(3 von 4)hier einfach
Behandlung der Ausnahme:
63 Aufruf der Elementfunktion what
64 cout << "\nAfter exception-handling program can continue:" << endl;
65
66 array< int, 7 > intArray = { 1, 2, 3, 4, 5, 6, 7 };
67 try {
68 cout << "\nAttempt to display intArray.at( 15 )" << endl;
69 cout << intArray.at( 15 ) << endl; // ERROR: out of range
70 }
71 catch( out_of_range& ex ) {
72 cerr << "An exception occurred: " << ex.what() << endl;
73 }
74 } // end main
75
76 // output vector contents
77 void outputVector( const vector< int > & items )
78 {
79 for( int item : items ) {
80 cout << item << " ";
81 }
82 cout << endl;
83 }

2006 Pearson Education,


Inc. All rights reserved.
Size of vector v1 is 7 58
vector after initialization:
1 2 3 4 5 6 7 Outline
Size of vector v2 is 10
vector after initialization:
10 11 12 13 14 15 16 17 18 19
8 and 9 added at back of v1. fig07_19.cpp
Last element of v2 will be removed: 19
After modification, the vectors contain: (4 von 4)
v1 ( size = 9 ):
1 2 3 4 5 6 7 8 9
v2 ( size = 9 ):
10 11 12 13 14 15 16 17 18
Evaluating: v1 != v2
v1 and v2 are not equal
v1[5] is 6
Assigning 1000 to v1[5]
v1:
1 2 3 4 5 1000 7 8 9
Attempt to display v1.at( 9 )
An exception occurred: invalid vector<T> subscript
After exception-handling program can continue:
Attempt to display intArray.at( 15 )
An exception occurred: invalid array<T, N> subscript

2006 Pearson Education,


Inc. All rights reserved.
59

7.8 Suchen und Sortieren

bersicht: Suchen von Daten


Es soll bestimmt werden, ob ein bestimmter Wert, der
Suchschlssel (search key), in den Daten vorkommt.
Wenn ja: an welcher Stelle?
Algorithmen
Lineare Suche
Einfach, relativ langsam
Binre Suche
Schneller, aber komplizierter

2006 Pearson Education, Inc. All rights reserved.


60

7.8 Suchen und Sortieren


bersicht: Sortieren von Daten
Die Daten sollen in eine bestimmte Reihenfolge gebracht
werden
Oft aufsteigend oder absteigend
Grundlage ist eine Ordnungsrelation fr die Daten
Einfache Algorithmen
Direktes Einfgen (Insertion-Sort)
Direktes Vertauschen (Bubble-Sort)
Direkte Auswahl (Selection-Sort)
Effizientere, aber aufwndigere Algorithmen:
Merge-Sort (Einfgen)
Quick-Sort (Vertauschen)
Heapsort (Auswahl)

2006 Pearson Education, Inc. All rights reserved.


61

7.8 Suchen und Sortieren

bersicht: Gro-O-Schreibweise (Big O notation)


Kurzschreibweise fr eine Abschtzung der Laufzeit (im
ungnstigsten Fall) fr einen Algorithmus
Ist Bestandteil der Komplexittstheorie
Ergebnisse fr wichtigste Algorithmen werden hier
angegeben

2006 Pearson Education, Inc. All rights reserved.


62

7.8 Suchen und Sortieren

Suchalgorithmen
Finden des Elements, das zu einem gegebenen
Suchschlssel passt, in einer gegebenen Datenmenge
Falls ein solches Element existiert
Wesentliche Differenz zwischen unterschiedlichen
Suchalgorithmen
Aufwand, der fr die Durchfhrung der Suche bentigt wird
Ist insbesondere von der Anzahl der Datenelemente
abhngig
Kann in der Gro-O-Schreibweise angegeben werden

2006 Pearson Education, Inc. All rights reserved.


63

7.8.1 Lineare Suche


Lineare Suche
Vergleicht jedes Element eines Arrays / vectors mit dem
Suchschlssel
Bei zufllig angeordneten Daten ist es genauso wahrscheinlich,
dass der Wert gleich beim ersten Element gefunden wird, wie
beim letzten.
Das Programm muss den Suchschlssel durchschnittlich mit der
Hlfte der Elemente des Arrays vergleichen.
Um festzustellen, dass ein Wert nicht in dem Array vorkommt,
muss das Programm den Suchschlssel mit jedem Element des
Arrays vergleichen.
Ist sinnvoll fr kleine oder unsortierte Arrays

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.20: fig07_20.cpp
64
// Linear search of an array.
2
Outline
3 #include <iostream>
4 #include <array>
5 using namespace std;
6 fig07_20.cpp
7 // compare key to every element of array until location is
8 // found or until end of array is reached; return location of (1 von 2)
9 // element if key is found or -1 if key is not found
10 template < typename T, size_t size >
11 int linearSearch( const array< T, size >& items, const T& key )
12 {
13 for( size_t i = 0; i < items.size(); ++i )
14 if( items[ i ] == key ) // if found,
15 return i; // return location of key
16
17 return -1; // key not found
18 } // end function linearSearch

2006 Pearson Education,


Inc. All rights reserved.
19
65
20 int main()
21 {
Outline
22 const size_t arraySize = 100; // size of array
23 array< int, arraySize > a; // create array
24 int searchKey; // value to locate in array
fig07_20.cpp
25
26 for( size_t i = 0; i < arraySize; ++i )
27 a[ i ] = 2 * i; // create some data
(2 von 2)
28
29 cout << "Enter integer search key: ";
30 cin >> searchKey;
31
32 // attempt to locate searchKey in array a
33 int element = linearSearch( a, searchKey ); Funktion gibt den Index des Suchschlssels
34 zurck; -1 falls er nicht gefunden wird
35 // display results
36 if( element != -1 )
37 cout << "Found value in element " << element << endl;
38 else
39 cout << "Value not found" << endl;
40 } // end main

Enter integer search key: 36


Found value in element 18

Enter integer search key: 37


Value not found

2006 Pearson Education,


Inc. All rights reserved.
66

7.8.1 Lineare Suche

Gro-O-Schreibweise
Misst das Anwachsen der Laufzeit eines Algorithmus im
Verhltnis zur Anzahl der verarbeiteten Werte
Betont die dominanten Anteile
Vernachlssigt die Anteile, die unwichtig werden, wenn n
stark anwchst
Vernachlssigt konstante Anteile

2006 Pearson Education, Inc. All rights reserved.


67

7.8.1 Lineare Suche


Gro-O-Schreibweise
Konstante Laufzeit
Die Anzahl der Operationen, die ein Algorithmus
durchfhrt, ist konstant.
Erhht sich nicht, wenn die Anzahl der Werte anwchst
Wird in Gro-O-Schreibweise als O(1) dargestellt
Gesprochen von der Ordnung 1 oder Ordnung 1
Beispiel
berprfen, ob das erste Element eines n-Arrays gleich
dem zweiten Element ist
Es wird immer genau ein Vergleich bentigt, egal
wie gro das Array ist.

2006 Pearson Education, Inc. All rights reserved.


68

7.8.1 Lineare Suche

Gro-O-Schreibweise
Lineare Laufzeit
Die Anzahl der Operationen, die ein Algorithmus
durchfhrt, wchst linear mit der Anzahl der Werte.
Wird in Gro-O-Schreibweise als O(n) dargestellt
Gesprochen von der Ordnung n oder Ordnung n
Beispiel
berprfen, ob das erste Element eines n-Arrays mit
irgendeinem der anderen Elemente gleich ist.
Bentigt n - 1 Vergleiche
n Anteil dominiert, -1 wird ignoriert

2006 Pearson Education, Inc. All rights reserved.


69

7.8.1 Lineare Suche

Gro-O-Schreibweise
Quadratische Laufzeit
Die Anzahl der Operationen, die ein Algorithmus
durchfhrt, wchst quadratisch mit der Anzahl der Werte.
Wird in Gro-O-Schreibweise als O(n2) dargestellt
Gesprochen von der Ordnung n2 oder Ordnung n2
Beispiel
berprfen, ob irgendein Element eines n-Arrays mit
irgendeinem der anderen Elemente gleich ist.
Bentigt n2/2 n/2 Vergleiche
n2 Anteil dominiert, die Konstante 1/2 wird ignoriert,
-n/2 wird ignoriert

2006 Pearson Education, Inc. All rights reserved.


70

7.8.1 Lineare Suche

Effizienz der linearen Suche


Die lineare Suche luft in O(n) Zeit
Ungnstigster Fall: Jedes Element muss berprft werden.
Falls die Gre des Arrays / vectors verdoppelt wird,
verdoppelt sich auch die Anzahl der Vergleiche.

2006 Pearson Education, Inc. All rights reserved.


71

Tipp zur Performanz

Oft ist die Performanz von einfachen Algorithmen


schlecht. Ihr Wert besteht darin, dass sie leicht zu
schreiben, zu testen und zu debuggen sind.
Oft werden komplexere Algorithmen bentigt, um
die optimale Performanz zu erreichen.

2006 Pearson Education, Inc. All rights reserved.


72

7.8.2 Binre Suche


Algorithmus fr die binre Suche
Erfordert, dass der vector / das array zuerst sortiert wird
Dies kann z.B. durch die Funktion sort der
Standardbibliothek erledigt werden.
Sortiert Elemente in aufsteigender Ordnung
Erste Iteration (setzt aufsteigend sortierte Ordnung voraus)
Das mittlere Element des vectors wird berprft.
Falls es mit dem Suchschlssel bereinstimmt, endet der
Algorithmus.
Falls es grer als der Suchschlssel ist, wird die Suche
nur noch in der ersten Hlfte des vectors fortgesetzt.
Falls es kleiner als der Suchschlssel ist, wird die Suche
nur noch in der zweiten Hlfte des vectors fortgesetzt.

2006 Pearson Education, Inc. All rights reserved.


73

7.8.2 Binre Suche


Algorithmus fr die binre Suche
Darauf folgende Iterationen
Das mittlere Element des brig gebliebenen Teilvectors
wird berprft.
Falls es mit dem Suchschlssel bereinstimmt, endet der
Algorithmus.
Falls nicht, wird die Suche in der passenden Hlfte des
Teilvectors fortgesetzt.
Der Algorithmus endet, wenn
ein Element, das mit dem Suchschlssel bereinstimmt,
gefunden wird oder
der aktuelle Teilvector die Gre 0 hat.
Daraus folgt, dass der Suchschlssel nicht im vector
enthalten ist.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig 7.23: Fig07_23.cpp 74
2 // Binary search in a vector of random integers. Outline
3
4 #include <iostream>
5 #include <vector>
6 #include <algorithm> // prototype for std::sort function
fig07_23.cpp
6 #include <random>
7 #include <ctime> (1 von 5)
8 using namespace std;
9
10 // display vector elements from index low through index high
11 template < typename T >
12 void displayElements( const vector< T >& data, size_t low, size_t high )
13 {
14 for( size_t i = 0; i < low; ++i ) // display spaces for alignment
15 cout << " ";
16 for( size_t i = low; i <= high; ++i ) // display elements left in vector
17 cout << data[ i ] << " ";
18 cout << endl;
19 } // end function displayElements
20

2006 Pearson Education, Inc. All rights reserved.


21 // perform a binary search on the data
75
22 template < typename T >
23 int binarySearch( const vector< T >& data, const T& key )
Outline
Initialisierung der Indices low, high
24 { und middle des vectors
25 size_t low = 0; // low index of elements to search
26 size_t high = data.size() - 1; // high index of elements to search
27 size_t middle = ( low + high + 1 ) / 2; // middle element fig07_23.cpp
28 int location = -1; // key's index; -1 if not found
29
(2 von 5)
Initialisierung von location mit -1, um anzuzeigen,
30 do // loop to search for element
31 {
dass der Suchschlssel (noch) nicht gefunden wurde
32 // display remaining elements of vector to be searched
33 displayElements( data, low, high );
34 // output spaces for alignment
35 for( size_t i = 0; i < middle; ++i )
36 cout << " ";
37 cout << " * " << endl; // indicate current middle
Fortsetzung der Suche in der
38
39 if( key < data[ middle ] )
passenden Hlfte des vectors
40 high = middle - 1; // eliminate the higher half
41 else if( data[ middle ] < key )
Test, ob das Element an der Position
42 low = middle + 1; // eliminate the lower half
43 else
middle gleich key ist
44 location = middle; // location is the current middle
45
46 middle = ( low + high + 1 ) / 2; // recalculate the middle
47 } while( ( low <= high ) && ( location == -1 ) );
48 Schleife luft, bis der Teilvector die Gre
49 return location; // return location of key
50 } // end function binarySearch
Null hat oder der Suchschlssel gefunden ist

2006 Pearson Education, Inc. All rights reserved.


51
76
52 int main()
53 {
Outline
54 // use the default random-number generation engine to produce
55 // uniformly distributed pseudorandom int values from 10 to 99
56 default_random_engine engine( static_cast< unsigned >( time( nullptr ) ) );
57 uniform_int_distribution< unsigned int > randomInt( 10, 99 ); fig07_23.cpp
58
59 const size_t size = 15; // vector size
(3 von 5)
60 vector< int > data; // empty vector of ints
61 // fill vector with random ints in range 10-99
62 for( size_t i = 0; i < size; ++i )
63 data.push_back( randomInt( engine ) ); // 10-99
64 std::sort( data.begin(), data.end() ); // sort the data
65 displayElements( data, 0, size - 1 ); // output the data
66
67 // get input from user
68 cout << "\n\nPlease enter an integer value (-1 to quit): ";
69 int searchKey; // value to locate
70 cin >> searchKey; // read an int from user
71 cout << endl;

2006 Pearson Education, Inc. All rights reserved.


72 77
73 // repeatedly input an integer; -1 terminates the program Outline
74 while( searchKey != -1 ) {
75 // use binary search to try to find integer
76 int position = binarySearch( data, searchKey ); Durchfhrung der binren Suche
77
fig07_23.cpp
78 // return value of -1 indicates integer was not found
79 if( position == -1 ) (4 von 5)
80 cout << "The integer " << searchKey << " was not found.\n";
81 else
82 cout << "The integer " << searchKey
83 << " was found in position " << position << ".\n";
84
85 // get input from user
86 cout << "\n\nPlease enter an integer value (-1 to quit): ";
87 cin >> searchKey; // read an int from user
88 cout << endl;
89 } // end while
90 } // end main

2006 Pearson Education, Inc. All rights reserved.


78
26 31 33 38 47 49 51 67 73 74 82 89 90 91 95 Outline
Please enter an integer value (-1 to quit): 38

26 31 33 38 47 49 51 67 73 74 82 89 90 91 95
*
26 31 33 38 47 49 51 fig07_23.cpp
*
The integer 38 was found in position 3. (5 von 5)

Please enter an integer value (-1 to quit): 91

26 31 33 38 47 49 51 67 73 74 82 89 90 91 95
*
73 74 82 89 90 91 95
*
90 91 95
*
The integer 91 was found in position 13.

Please enter an integer value (-1 to quit): 25

26 31 33 38 47 49 51 67 73 74 82 89 90 91 95
*
26 31 33 38 47 49 51
*
26 31 33
*
26
*
The integer 25 was not found.

Please enter an integer value (-1 to quit): -1

2006 Pearson Education, Inc. All rights reserved.


79

7.8.2 Binre Suche

Effizienz der binren Suche


Logarithmische Laufzeit
Die Anzahl der Operationen, die der Algorithmus durchfhrt,
wchst logarithmisch mit der Anzahl der Werte.
Wird in Gro-O-Schreibweise als O(log n) dargestellt
Gesprochen von der Ordnung log n oder Ordnung log n
Beispiel
Einen sortierten vector mit 1000 Elementen binr zu
durchsuchen bentigt hchstens 10 Vergleiche (10 ist die
kleinste ganze Zahl grer als 9.97 = log2 1000).
Wiederholtes ganzzahliges Teilen von 1000 durch 2
ergibt 0 nach 10 Iterationen.

2006 Pearson Education, Inc. All rights reserved.


80
7.8.3 Sortieren durch direktes Einfgen
(Insertion-Sort)
Sortieralgorithmen
Bringen Daten in eine bestimmte Ordnung
Beispielsweise ansteigend oder abfallend
Eine der wichtigsten Anwendungen in der Informatik
Das Endresultat, ein sortiertes array / vector, ist
unabhngig von dem zum Sortieren eingesetzten
Algorithmus.
Die Wahl des Algorithmus beeinflusst allerdings die zum
Sortieren bentigte Laufzeit sowie den Speicherverbrauch.

2006 Pearson Education, Inc. All rights reserved.


81

7.8.3 Insertion-Sort

Insertion-Sort
Einfach, fr groe Arrays nicht sehr effizient
Die erste Iteration nimmt das zweite Element.
Falls es kleiner als das erste Element ist, wird es mit dem
ersten Element vertauscht.
Die zweite Iteration betrachtet das dritte Element.
Es wird an die richtige Stelle des bereits sortierten Teilarrays
aus den ersten beiden Elementen gesetzt. Dadurch ist der
sortierte Bereich um ein Element grer.

In der i-ten Iteration dieses Algorithmus, sind die ersten i
Elemente des ursprnglichen Arrays sortiert.

2006 Pearson Education, Inc. All rights reserved.


82

7.8.3 Insertion-Sort
Beispiel
Unsortiertes Array
34 56 04 10 77 51 93 30 05 52
Erster Schritt (Vertauschen, falls erforderlich)
34 56
Einsetzen des dritten Elements an der passenden Stelle
34 56 04
34 56 04
34 56 04
04 34 56
Einsetzen des vierten Elements an der passenden Stelle
04 34 56 10
04 34 56 10
04 34 56 10
04 10 34 56 und so weiter...

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.24: fig07_24.cpp
83
2 // Sorting an array into ascending order with insertion sort.
3 #include <iostream>
Outline
4 #include <iomanip>
5 #include <array>
6 using namespace std;
fig07_24.cpp
7
8 // sort an array into ascending order
(1 von 2)
9 template < typename T, size_t size >
10 void insertionSort( array < T, size > & items )
11 {
12 // loop over the elements of the array
13 for( size_t next = 1; next < size; ++next ) {
14 T insert = items[ next ]; // save value of next item to insert
15 size_t moveIndex = next; // initialize location to place element
16
17 // search for the location in which to put the current element
18 while( ( moveIndex > 0 ) && ( items[ moveIndex - 1 ] > insert ) ) {
19 // shift element one slot to the right
20 items[ moveIndex ] = items[ moveIndex - 1 ];
Die Position finden, wohin
21 --moveIndex; das gerade betrachtete
22 } // end while Element gehrt
23
24 items[ moveIndex ] = insert; // place insert item back into array
25 } // end for
26 } // end function insertionSort Das Element an die richtige
Position setzen

2006 Pearson Education,


Inc. All rights reserved.
27
84
28 int main()
29 {
Outline
30 const size_t arraySize = 10; // size of array a
31 array< int, arraySize > a = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };
32
fig07_24.cpp
33 cout << "Unsorted array:\n";
34 // output original array
35 for( const int& element : a ) {
(2 von 2)
36 cout << setw( 4 ) << element;
37 }
38
39 insertionSort( a ); // sort the array
40
41 cout << "\nSorted array:\n";
42 // output sorted array
43 for( const int& element : a ) {
44 cout << setw( 4 ) << element;
45 }
46 cout << endl;
47 } // end main

Unsorted array:
34 56 4 10 77 51 93 30 5 52
Sorted array:
4 5 10 30 34 51 52 56 77 93

2006 Pearson Education,


Inc. All rights reserved.
85

7.8.3 Insertion-Sort
Effizienz von Insertion-Sort
In der i-ten Iteration
wird das (i + 1)te Element in die korrekte Position in bezug
auf die ersten i Elemente gesetzt.
Nach der i-ten Iteration
sind die ersten i Elemente sortiert.
Die uere Schleife wird (n 1) mal durchlaufen.
Im ungnstigsten Fall wird die innere Schleife n mal
durchlaufen.
Damit ergibt sich O(n2).
Fr die Bestimmung des Gro-O-Wertes bedeuten
verschachtelte Wiederholungsanweisungen eine
Multiplikation der jeweiligen Anzahl der Iterationen.

2006 Pearson Education, Inc. All rights reserved.


86
7.8.4 Merge-Sort
(rekursive Implementierung)
Merge-Sort
Ein vector wird sortiert, indem
er in zwei gleich groe Teilvectoren zerlegt wird.
Falls die vectorgre ungerade ist, wird ein Teilvector
ein Element grer als der andere.
Jeder Teilvector wird sortiert.
Beim Verschmelzen (merge) der Teilvectoren in einen
groen, sortierten vector
werden wiederholt die kleinsten Elemente in den beiden
Teilvectoren verglichen.
Das kleinere Element wird entfernt und in den groen,
kombinierten vector geschrieben.

2006 Pearson Education, Inc. All rights reserved.


87

7.8.4 Merge-Sort

Merge-Sort
Rekursive Implementierung
Basisfall
Ein vector mit einem Element ist schon sortiert.
Rekursionsschritt
Der vector (mit 2 Elementen) wird in zwei gleich
groe Hlften aufgeteilt.
Falls die vectorgre ungerade ist, wird ein
Teilvector ein Element grer als der andere.
Rekursives Sortieren jedes Teilvectors
Verschmelzen der Teilvectoren in einen groen,
sortierten vector.

2006 Pearson Education, Inc. All rights reserved.


88

7.8.4 Merge-Sort
Merge-Sort
Verschmelzung an einem Beispiel
Kleinere, sortierte Teilvectoren
A: 4 10 34 56 77
B: 5 30 51 52 93
Vergleich des kleinsten Elements in A mit dem kleinsten
Element in B
4 (A) ist kleiner als 5 (B)
4 wird das erste Element im verschmolzenen vector
5 (B) ist kleiner als 10 (A)
5 wird das zweite Element im verschmolzenen vector
10 (A) ist kleiner als 30 (B)
10 wird das dritte Element im verschmolzenen vector
usw.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig 7.27: Fig07_27.cpp 89
2 // Sorting a vector into ascending order with merge sort. Outline
3 #include <iostream>
4 #include <vector>
5 #include <random>
6 #include <ctime>
fig07_27.cpp
7 using namespace std;
8 (1 von 7)
9 // display vector elements from index low through index high
10 template < typename T >
11 void displayElements( const vector< T >& data, size_t low, size_t high )
12 {
13 for( size_t i = 0; i < low; ++i ) { // display spaces for alignment
14 cout << " ";
15 }
16 for( size_t i = low; i <= high; ++i ) { // display elements left in vector
17 cout << " " << data[ i ];
18 }
19 cout << endl;
20 } // end function displayElements
21
22 // function that starts a merge sort
23 template < typename T >
24 void mergeSort( vector< T >& data, size_t low, size_t high )
25 {
26 vector< T > combined( data.size() ); // not-in-place working vector
27 mergeSortHelper( data, low, high, combined );
28 } // end function mergeSort
29
2006 Pearson Education, Inc. All rights reserved.
30 90
31 // recursive function to sort (sub)vectors Outline
32 template < typename T >
33 void mergeSortHelper( vector< T >& data, size_t low, size_t high,
34 vector< T >& combined )
35 {
fig07_27.cpp
36 if( ( high - low ) >= 1 ) { // if not base case (vector with size 1)
37 size_t middle1 = ( low + high ) / 2; // calculate middle of vector (2 von 7)
38 size_t middle2 = middle1 + 1; // calculate next element over
39
40 // output split step
41 cout << "split: ";
42 displayElements( data, low, high );
43 cout << " ";
44 displayElements( data, low, middle1 );
45 cout << " ";
46 displayElements( data, middle2, high );
47 cout << endl;
48
49 // split vector in half; sort each half (recursive calls)
50 mergeSortHelper( data, low, middle1, combined ); // first half of vector
51 mergeSortHelper( data, middle2, high, combined ); // second half of vector
52
Sortierung der (Teil-)vectoren
53 // merge two sorted vectors after split calls return
54 merge( data, low, middle1, middle2, high, combined );
55 } // end if
56 } // end function mergeSortHelper Verschmelzen der beiden sortierten (Teil-)vectoren
in einen greren, sortierten (Teil-)vector

2006 Pearson Education, Inc. All rights reserved.


57
91
58 // merge two sorted subvectors into one sorted subvector
59 template < typename T >
Outline
60 void merge( vector< T >& data, size_t left, size_t middle1,
61 size_t middle2, size_t right, vector< T >& combined )
62 {
63 size_t leftIndex = left; // index into left subvector fig07_27.cpp
64 size_t rightIndex = middle2; // index into right subvector
65 size_t combinedIndex = left; // index into not-in-place working vector
(3 von 7)
66
67 // output two subvectors before merging
68 cout << "merge: ";
69 displayElements( data, left, middle1 );
Schleife, bis das Ende einer der
70 cout << " ";
71 displayElements( data, middle2, right );
Teilvectoren erreicht ist
72
73 // merge vectors until reaching end of either
Test, welches Element am
74 while( leftIndex <= middle1 && rightIndex <= right ) {
75 // place smaller of two current elements into result
Anfang der vectoren
76 // and move to next space in vector kleiner ist
77 if( data[ leftIndex ] <= data[ rightIndex ] )
78 combined[ combinedIndex++ ] = data[ leftIndex++ ];
79 else
Schreiben des kleineren Elements
80 combined[ combinedIndex++ ] = data[ rightIndex++ ]; in den kombinierten vector
81 } // end while
82

2006 Pearson Education, Inc. All rights reserved.


83
92
84 if( leftIndex == middle2 ) { // if at end of left vector Fllen des kombinierten
85 while( rightIndex <= right ) // copy in rest of right vector
Outline
vectors mit den
86 combined[ combinedIndex++ ] = data[ rightIndex++ ];
87 } // end if
restlichen Elementen des
88 else { // at end of right vector
rechten vectors oder
89 while( leftIndex <= middle1 ) // copy in rest of left vector fig07_27.cpp
90 combined[ combinedIndex++ ] = data[ leftIndex++ ];
mit den (4
restlichen
von 7)
91 } // end else
92
Elementen des linken
93 // copy values back into original vector
vectors
94 for( size_t i = left; i <= right; ++i ) {
95 data[ i ] = combined[ i ];
Kopieren des kombinierten vectors
96 }
97
in den Originalvector
98 // output merged vector
99 cout << " ";
100 displayElements( data, left, right );
101 cout << endl;
102} // end function merge

2006 Pearson Education, Inc. All rights reserved.


93
103 int main()
104 {
Outline
105 // use the default random-number generation engine to produce
106 // uniformly distributed pseudorandom int values from 10 to 99
107 default_random_engine engine( static_cast< unsigned >( time( nullptr ) ) );
108 uniform_int_distribution< unsigned int > randomInt( 10, 99 ); fig07_27.cpp
109
110 const size_t size = 10; // vector size
(5 von 7)
111 vector< int > data; // empty vector of ints
112 for( int i = 0; i < size; ++i ) {
113 data.push_back( randomInt( engine ) ); // 10-99
114 }
115 cout << "Unsorted vector:" << endl;
116 displayElements( data, 0, size - 1 ); // print unsorted vector
117 cout << endl;
118
119 mergeSort( data, 0, size - 1 ); // sort vector
120
121 cout << "Sorted vector:" << endl;
122 displayElements( data, 0, size - 1 ); // print sorted vector
123 } // end main

2006 Pearson Education, Inc. All rights reserved.


Unsorted vector: 94
30 47 22 67 79 18 60 78 26 54 Outline
split: 30 47 22 67 79 18 60 78 26 54
30 47 22 67 79
18 60 78 26 54
fig07_27.cpp
split: 30 47 22 67 79
30 47 22 (6 von 7)
67 79

split: 30 47 22
30 47
22

split: 30 47
30
47

merge: 30
47
30 47

merge: 30 47
22
22 30 47

split: 67 79
67
79

merge: 67
79
67 79

merge: 22 30 47
67 79
22 30 47 67 79

2006 Pearson Education, Inc. All rights reserved.


95
split: 18 60 78 26 54 Outline
18 60 78
26 54

split: 18 60 78
18 60 fig07_27.cpp
78
(7 von 7)
split: 18 60
18
60

merge: 18
60
18 60

merge: 18 60
78
18 60 78

split: 26 54
26
54

merge: 26
54
26 54

merge: 18 60 78
26 54
18 26 54 60 78

merge: 22 30 47 67 79
18 26 54 60 78
18 22 26 30 47 54 60 67 78 79

Sorted vector:
18 22 26 30 47 54 60 67 78 79

2006 Pearson Education, Inc. All rights reserved.


96

7.8.4 Merge-Sort
Effizienz von Merge-Sort
O(n log n) Laufzeit:
Das Halbieren der vectoren bedeutet log2 n Ebenen, um den
Basisfall zu erreichen.
Verdopplung der vectorgre erfordert eine weitere
Ebene, vervierfachen zwei weitere Ebenen usw.
O(n) Vergleiche sind auf jeder Ebene erforderlich:
Aufruf von sortSubVector mit einem vector der
Gre n fhrt zu
Zweimal sortSubVector mit Teilvectoren der
Gre n/2
merge mit n 1 (Ordnung n) Vergleichen
Damit ergibt sich insgesamt O(n log n)

2006 Pearson Education, Inc. All rights reserved.


97

Algorithmus Komplexitt

Lineare Suche O(n)


Binre Suche O(log n)
Rekursive lineare Suche O(n)
Rekursive binre Suche O(log n)

Bubble-Sort O(n2)
Selection-Sort O(n2)
Insertion-Sort O(n2)
Merge-Sort O(n log n)

Quick-Sort Ungnstigster Fall: O(n2)


Durchschnittlich: O(n log n)

Fig. 7.28 | Such- und Sortieralgorithmen mit Komplexittswerten.

2006 Pearson Education, Inc. All rights reserved.


98

Genherter
n O(log n) O(n) O(n log n) O(n2)
Dezimalwert

210 1000 10 210 10 210 220


220 1,000,000 20 220 20 220 240
230 1,000,000,000 30 230 30 230 260

Fig. 7.29 | Zahlenwerte zu typischen Gro-O-Ausdrcken.

2006 Pearson Education, Inc. All rights reserved.


99

7.9 Mehrdimensionale arrays

Zweidimensionale Arrays
Matrix oder Tabelle
Werte, die in Zeilen und Spalten angeordnet sind
Elemente werden ber zwei Indices a[ i ][ j ]
angesprochen
Ein Array mit m Zeilen und n Spalten wird als m-mal-n
Array bezeichnet.
Mehrdimensionale Arrays knnen mehr als zwei
Dimensionen haben
Jede weitere Dimension fhrt zu einem zustzlichen Index:
a[ i ][ j ][ k ] usw.

2006 Pearson Education, Inc. All rights reserved.


100

Hufiger Programmierfehler

Spricht man ein Element eines zweidimensionalen


Arrays - wie a[ i ][ j ] - als a[ i, j ] an,
so ist dies ein logischer Fehler.
a[ i, j ] wird wie a[ j ] behandelt, weil C++
den Ausdruck i, j (der einen Kommaoperator
enthlt) einfach als j auswertet (den am weitesten
rechts stehenden der kommaseparierten
Ausdrcke).

2006 Pearson Education, Inc. All rights reserved.


101

7.9 Mehrdimensionale arrays

Deklaration und Initialisierung von


zweidimensionalen arrays
Deklaration des zweidimensionalen Arrays b
array< array< int, 2 >, 2 > b =
{ { 1, 2 }, { 3, 4 } };
1 und 2 initialisieren b[ 0 ][ 0 ] und b[ 0 ][ 1 ]
3 und 4 initialisieren b[ 1 ][ 0 ] und b[ 1 ][ 1 ]
array< array< int, 2 >, 2 > b =
{ { 1 }, { 3, 4 } };
Die Zeile 0 enthlt die Werte 1 und 0 (implizit mit Null
initialisiert).
Die Zeile 1 enthlt die Werte 3 und 4.

2006 Pearson Education, Inc. All rights reserved.


102

Fig.7.30 | Zweidimensionales Array mit drei Zeilen (rows) und vier Spalten (columns).

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.31: fig07_31.cpp
103
2 // Initializing multidimensional arrays.
3 #include <iostream>
Outline
4 #include <array>
5 using namespace std;
6
fig07_31.cpp
7 const size_t rows = 2;
8 const size_t columns = 3;
9 void printArray( const array< array < int, columns >, rows > & );
(1 von 2)
10
11 int main()
12 {
13 array< array < int, columns >, rows > a1 = { 1, 2, 3, 4, 5, 6 };
14 array< array < int, columns >, rows > a2 = { 1, 2, 3, 4, 5 };
15 array< array < int, columns >, rows > a3 = { 1, 2, 3 };
16
17 cout << "Values in a1 by row are:" << endl;
18 printArray( a1 );
19
20 cout << "\nValues in a2 by row are:" << endl;
21 printArray( a2 );
22
23 cout << "\nValues in a3 by row are:" << endl;
24 printArray( a3 );
25 } // end main

2006 Pearson Education,


Inc. All rights reserved.
26
104
27 // output array with two rows and three columns
28 void printArray( const array< array < int, columns >, rows > & a )
Outline
29 {
30 // loop through array's rows
31 for( const auto& row : a ) { Geschachtelte for-Schleifen fig07_31.cpp
32 // loop through columns of current row zur Ausgabe des Arrays
33 for( const auto& element : row ) {
(2 von 2)
34 cout << element << ' ';
35 }
36 cout << endl; // start new line of output
37 } // end outer for
38 } // end function printArray

Values in a1 by row are:


1 2 3
4 5 6
Values in a2 by row are:
1 2 3
4 5 0
Values in a3 by row are:
1 2 3
0 0 0

2006 Pearson Education,


Inc. All rights reserved.
105

7.9 Mehrdimensionale arrays


Manipulationen von mehrdimensionalen arrays
Werden blicherweise mit for-Anweisungen durchgefhrt.
Beispiel
Alle Elemente einer Zeile auf den Wert 0 setzen
for( size_t col = 0; col < 4; ++col ) {
a[ 2 ][ col ] = 0;
}
Beispiel
Alle Elemente eines zweidimensionalen Arrays aufsummieren
total = 0;
for( auto row : a ) {
for( auto element : row ) {
total += element;
}
}
2006 Pearson Education, Inc. All rights reserved.
106
7.10 Fallstudie: Die Klasse GradeBook
mit einem zweidimensionalen array
Klasse GradeBook
Eindimensionales array
Es werden Noten aller Studenten eines Zuges fr eine einzige
Prfung gespeichert.
Zweidimensionales array
Es werden Noten aller Studenten eines Zuges fr alle
Prfungen des Studiums gespeichert.
Jede Zeile stellt alle Noten eines Studenten dar.
Jede Spalte stellt die Noten aller Studenten fr eine
bestimmte Prfung dar.

2006 Pearson Education, Inc. All rights reserved.


1 // Fig. 7.32: GradeBook.h 107
2 // Definition of class GradeBook that uses a 2-dim array to store test grades. Outline
3 // Member functions are defined in GradeBook.cpp
4 #include <array>
5 #include <string>
6 GradeBook.h
7 class GradeBook // GradeBook class definition
8 { (1 von 1)
9 public:
10 // constants
11 static const size_t students = 10; // number of students
12 static const size_t tests = 3; // number of tests
13
GradeBook Konstruktor akzeptiert einen
14 // constructor initializes course name and array of grades
string und ein zweidimensionales Array
15 GradeBook( const std::string &,
16 const std::array< std::array< int, tests >, students > & );
17 void setCourseName( const std::string & ); // set the course name
18 std::string getCourseName() const; // retrieve the course name
19 std::string message() const; // return a welcome message
20 std::string processGradesResults() const; // operations on grade data
21 int getMinimum() const; // find the minimum grade in the grade book
22 int getMaximum() const; // find the maximum grade in the grade book
23 double getAverage( const std::array< int, tests > & ) const; // average
24 std::string gradesContents() const; // return contents of grades array
25 std::string barChart() const; // return bar chart of grade distribution
26 private:
27 std::string courseName; // course name for this grade book
28 std::array< std::array< int, tests >, students > grades; // 2D array
29 }; // end class GradeBook 2006 Pearson Education,
Inc. All rights reserved.
1 // Fig. 7.33: GradeBook.cpp
108
2 // Member-function definitions for class GradeBook that
3 // uses a two-dimensional array to store grades.
Outline
4
5 #include <sstream>
6 #include <iomanip>
GradeBook.cpp
7 using namespace std;
8
9 #include "GradeBook.h" // GradeBook definition
(1 von 5)
10
11 // two-parameter constructor initializes courseName and grades array
12 GradeBook::GradeBook( const string& name,
13 const array< array< int, tests >, students > & gradesArray )
14 : courseName( name ), grades( gradesArray )
15 {
16 } // end GradeBook constructor
17
18 // function to set the course name
19 void GradeBook::setCourseName( const string& name )
20 {
21 courseName = name; // store the course name
22 } // end function setCourseName
23
24 // function to retrieve the course name
25 string GradeBook::getCourseName() const
26 {
27 return courseName;
28 } // end function getCourseName

2006 Pearson Education,


Inc. All rights reserved.
29
109
30 // return a welcome message to the GradeBook user
31 string GradeBook::message() const
Outline
32 {
33 return "Welcome to the grade book for\n" + getCourseName() + "!\n\n";
34 } // end function message
GradeBook.cpp
35
36 // perform various operations on the data
37 string GradeBook::processGradesResults() const
(2 von 5)
38 {
39 stringstream resultStream; // write result as text in here
40
41 // write grades array
42 resultStream << gradesContents();
43
44 // call functions getMinimum and getMaximum
45 resultStream << "\nLowest grade is " << getMinimum()
46 << "\nHighest grade is " << getMaximum() << endl;
47
48 // call function barChart to write grade distribution chart as text
49 resultStream << barChart();
50
51 return resultStream.str(); // return result as a string
52 } // end function processGradesResults
53

2006 Pearson Education,


Inc. All rights reserved.
54 // find minimum grade
110
55 int GradeBook::getMinimum() const
56 {
Outline
57 int lowGrade = 100; // assume lowest grade is 100
58 // loop through rows of grades array
59 for( const auto& student : grades ) {
GradeBook.cpp
60 // loop through columns of current row
61 for( const auto& grade : student ) {
62 // if current grade less than lowGrade, assign it to lowGrade
(3 von 5)
63 if( grade < lowGrade )
64 lowGrade = grade; // new lowest grade Schleifen ber Zeilen und Spalten von grades,
65 } // end inner for um schlechteste Note aller Studenten zu finden
66 } // end outer for
67 return lowGrade; // return lowest grade
68 } // end function getMinimum
69
70 // find maximum grade
71 int GradeBook::getMaximum() const
72 {
73 int highGrade = 0; // assume highest grade is 0 Schleifen ber Zeilen und Spalten von grades,
74 // loop through rows of grades array um beste Note aller Studenten zu finden
75 for( const auto& student : grades ) {
76 // loop through columns of current row
77 for( const auto& grade : student ) {
78 // if current grade greater than lowGrade, assign it to highGrade
79 if( grade > highGrade )
80 highGrade = grade; // new highest grade
81 } // end inner for
82 } // end outer for
83 return highGrade; // return highest grade
84 } // end function getMaximum
2006 Pearson Education,
Inc. All rights reserved.
85 111
86 // determine average grade for particular set of grades Outline
87 double GradeBook::getAverage( const array< int, tests > & setOfGrades ) const
88 {
89 int total = 0; // initialize total
90 // sum grades in array GradeBook.cpp
91 for( const auto& grade : setOfGrades )
92 total += grade; (4 von 5)
93 // return average of grades
94 return static_cast< double >( total ) / tests;
95 } // end function getAverage
96
97 // return the contents of the grades array as a string
98 string GradeBook::gradesContents() const
99 {
100 stringstream gradesStream; // write grades as text in here
101 gradesStream << "The grades are:\n\n";
102 gradesStream << " "; // align column heads
103 for( size_t test = 0; test < tests; ++test )
Fr einen Studenten wird der
104 gradesStream << "Test " << test + 1 << " "; Mittelwert ber alle seine Tests
105 berechnet, indem eine Zeile aus
gradesStream << "Average" << endl; // student average column heading
106 for( size_t student = 0; student < students; ++student ) { dem zweidimensionalen Array als
107 gradesStream << "Student " << setw( 2 ) << student + 1; eindimensionales Array an die
108 for( size_t test = 0; test < tests; ++test ) Funktion getAverage
109 gradesStream << setw( 8 ) << grades[ student ][ test ];
bergeben wird.
110 double average = getAverage( grades[ student ] );
111 gradesStream << setw( 9 ) << setprecision( 2 ) << fixed << average<< endl;
112 } // end outer for
113 return gradesStream.str(); // return grades as a string
114 } // end function gradesContents 2006 Pearson Education,
Inc. All rights reserved.
115
112
116 // return bar chart displaying grade distribution as a string
117 string GradeBook::barChart() const
Outline
118{
119 stringstream chartStream; // write bar chart as text in here
120 chartStream << "\nOverall grade distribution:" << endl;
GradeBook.cpp
121 // stores frequency of grades in each range of 10 grades
122 const size_t frequencySize = 11;
123 array< unsigned int, frequencySize > frequency = { 0 }; // init to all 0s
(5 von 5)
124 // for each grade, increment the appropriate frequency
125 for( const auto& student : grades )
126 for( const auto& test : student )
Berechnung der Verteilung
127 ++frequency[ test / 10 ]; aller Studentennoten
128 // for each grade frequency, write bar in chart
129 for( size_t count = 0; count < frequencySize; ++count ) {
130 // write bar label ("0-9:", ..., "90-99:", "100:" )
131 if( 0 == count)
132 chartStream << " 0-9: ";
133 else if( 10 == count)
134 chartStream << " 100: ";
135 else
136 chartStream << count * 10 << "-" << ( count * 10 ) + 9 << ": ";
137 // write bar of asterisks
138 for( unsigned int stars = 0; stars < frequency[ count ]; ++stars )
139 chartStream << '*';
140 chartStream << endl; // start a new line of output
141 } // end outer for
142 return chartStream.str(); // return bar chart as a string
143 } // end function barChart

2006 Pearson Education,


Inc. All rights reserved.
1 // Fig. 7.34: fig07_34.cpp
113
2 // Creates GradeBook object using a two-dimensional array of grades.
3 #include <array>
Outline
4 #include <iostream>
5 using namespace std;
6
fig07_34.cpp
7 #include "GradeBook.h" // GradeBook definition
8
9 int main()
(1 von 2)
10 {
11 // two-dimensional array of student grades
12 array< array< int, GradeBook::tests >, GradeBook::students > grades =
13 { 87, 96, 70,
14 68, 87, 90,
15 94, 100, 90, Deklaration von grades
16 100, 81, 82,
als 3-mal-10 Array
17 83, 65, 85,
18 78, 87, 65,
19 85, 75, 83,
20 91, 94, 100,
Jede Zeile entspricht einem Studenten;
21 76, 72, 84, jede Spalte entspricht einer Prfung
22 87, 93, 73 };
23
24 GradeBook myGradeBook(
25 "CS101 Introduction to C++ Programming", grades );
26 cout << myGradeBook.message();
27 cout << myGradeBook.processGradesResults();
28 } // end main

2006 Pearson Education,


Inc. All rights reserved.
Welcome to the grade book for
114
CS101 Introduction to C++ Programming! Outline
The grades are:

Test 1 Test 2 Test 3 Average


Student 1 87 96 70 84.33 fig07_34.cpp
Student 2 68 87 90 81.67
Student 3 94 100 90 94.67
Student 4 100 81 82 87.67
Student 5 83 65 85 77.67 (2 von 2)
Student 6 78 87 65 76.67
Student 7 85 75 83 81.00
Student 8 91 94 100 95.00
Student 9 76 72 84 77.33
Student 10 87 93 73 84.33

Lowest grade is 65
Highest grade is 100

Overall grade distribution:


0-9:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: ***
70-79: ******
80-89: ***********
90-99: *******
100: ***

2006 Pearson Education,


Inc. All rights reserved.

Das könnte Ihnen auch gefallen