27
© Olivier Saurer, 2010 Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

Embed Size (px)

Citation preview

Page 1: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Informatik I for D-MAVT

Exercise Session 4

Page 2: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Administratives

Ich bin nächste Woche abwesend

Übungsstunde vom 31.3.2010 Jens Puwein wird mich vertreten

Im selben Raum (HG D1.1)

Zusammen mit seiner Übungsgruppe

Übungen können an Jens abgegeben werden, er wird sie mir dann weitergeben

Page 3: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Bemerkung

Übungen bis jetzt zufriedenstellend gelöst Speziell die Programmieraufgaben!

Aber: mehr Disziplin bei ÜbungsabgabeAbgabe jeweils Mittwoch in Übungsstunde

Page 4: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Nachbesprechung

Nicht initialisierte Variablen haben einen zufälligen Wert Je nach Compilereinstellung (z.B. MS Visual Studio C++-

Compiler im Debug-Modus) macht Compiler automatische Initialisierung mit 0

int a; a10x7824

Speicheradresse Speicherinhalt

Compiler reserviert 4 Bytes im Speicher

0x7828

0x7820a2 a3 a4

Der Wert von a ist was auch immer zuvor an dieser Speicheradresse gespeichert war (interpretiert als Integer)!

Page 5: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 2: Gültige Variablennamen

Beginnen mit Buchstabe oder _ aber nicht mit einer Ziffer

Dürfen keine Sonderzeichen enthalten

Page 6: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 2: Auswertung von algebraischen Operatoren

Bsp: Es gilt:7.0 * (3 / 7) * 5

07.0 * * 5

0.07.0 * * 5

0.0 * 5

0.0 * 5.0

0.0

Ergebnistyp: double

4 Þ int

4.f Þ float

4.0 Þ double

Page 7: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 2: Typkonvertierung

Bsp:

double(9/2)

double(4)

4.0

Ergebnistyp: double

(double)9/2

9.0 / 2

9.0 / 2.0

Ergebnistyp: double

4.5

Page 8: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 2: Programmanalyse

Eine instanziierte Variable hat immer einen Wert der Wert kann zufällig sein (wenn nicht

initialisiert)

oder definiert (nach Initialisierung)

Bsp:int main()

{

int s1, s2 = 4;

double d1, d2 = 4.0;

}

s1 und d1 haben zufällige Werte

s2 = 4 und d2 = 4.0

Page 9: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Repetition: if – else I

Einfache Verzweigungenint a = 4;if (a = 2)

cout << "a == 2" << endl;else

cout << "a != 2" << endl;

Ausgabe: a == 2

a = 4;if (2 = a)

cout << "a == 2" << endl;else

cout << "a != 2" << endl;

Ausgabe: Kompiliert nicht!

int a = 4;if (a == 3)

cout << "a == 2" << endl;else

cout << "a != 2" << endl;

Ausgabe: a != 2

int a = 4;if (3 == a)

cout << "a == 2" << endl;else

cout << "a != 2" << endl;

Ausgabe: a != 2

? ?

? ?

Page 10: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Repetition: if – else II

Geschachtelte Verzweigungenbool a = false;bool b = true;

if (a)cout << "a == true";elseif(b)cout << "a == false && b == true";else cout << "a == false && b == false";

Ausgabe: a == false && b == true

bool a = true;bool b = true;

if (a)cout << "a == true" << endlelsecout << "a == false" << endl;if(b)cout << "b == true" << endl;else cout << "a == false" << endl;

Ausgabe: a == true b == true

? ?

Page 11: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Verkürzte Operatoren

Inkrement / Dekrement

Verkürzte Zuweisungs-Operatoren

x++ // postfix a = b++; // a = b; b = b + 1;++x // prefix a = ++b; // b = b + 1; a = b;x-- // postfix a = b--; // a = b; b = b - 1;--x // prefix a = --b; // b = b - 1; a = b;

x += 2; // x = x + 2x -= 2; // x = x - 2X *= 2; // x = x * 2

x /= 2; // x = x / 2 x %= 2; // x = x % 2

Page 12: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstrukturen: while – Schleifen

while – Schleife

Bedingung: Muss vom Typ bool sein

Anweisungsblock wird solange wiederholt bis die Bedingung zu false auswertet

Nach Abarbeitung des Anweisungsblock wird die Bedingung immer neu ausgewertet

Die Bedingung muss im Anweisungsblock verändert werden, sonst können Endlosschleifen entstehen

Page 13: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstrukturen: while – Schleifen int a = 1, b = 0;while(a = 0) b += 1;

Werte von a, b? Anzahl Iterationen?

a = 0, b = 0Keine Iteration

int a = 5, b = 0;while( a > 5 && a < 10) b += 1;

Werte von a, b?Anzahl Iterationen?

a = 5, b = 0Keine Iteration

int a = 5, b = 0;while(a > b) b += 2;

Werte von a, b?Anzahl Iterationen?

a = 5, b = 63 Iterationen

int a = 1, b = 0;while(a = 3) b += 1;

Werte von a, b?Anzahl Iterationen?

Endlosschleife!

?

?

?

?

Page 14: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstruktur: do while – Schleifen

do-while – Schleife

Die Bedingung wird erst nach Ausführung des Anweisungsblocks ausgewertet

Der Anweisungsblock wird mindestens einmal ausgeführt

Page 15: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstrukturen: do - while – Schleifen

int a = 1, b = 0;do b += 1;while(a = 0)

Werte von a, b? Anzahl Iterationen?

a = 0, b = 11 Iteration

int a = 5, b = 0;do b += 1;while( a > 5 && a < 10)

Werte von a, b?Anzahl Iterationen?

a = 5, b = 11 Iteration

int a = 5, b = 0;do b += 2;while(a > b)

Werte von a, b?Anzahl Iterationen?

a = 5, b = 63 Iterationen

?

?

?

Page 16: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstruktur: for – Schleife I

for – Schleife

Initialisierung: Definieren des Schleifen Indexes

Abbruchbedingung: Die Schleife wird abgearbeitet bis die Abbruchbedingung zu true auswertet

Inkrement: Schleifen Index wird aktualisiert

Page 17: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstruktur: for – Schleife II

for – Schleife

Allgemeiner Ablauf einer for – Schleife1. Initialisierung des Indexes

2. Auswertung der Abbruchbedingung

3. Abarbeitung des Anweisungsblocks

4. Aktualisierung des Inkrements

5. Springe zu Schritt 2.

Page 18: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Kontrollstruktur: for – Schleife III

Beispiel:for(int i = 0; i < 10; i *= i, i -= 3)

cout << i << ", ";

Ausgabe: 0, -3, 6, ?

Page 19: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Schleifen: break - Anweisung

break : Schleife wird vorzeitig abgebrochen

Kann in while, do-while und for – Schleifen verwendet werden

double d = 0.0;for(int i = 1; i < 100; i++){

if (i > 5){ cout << "i = " << i << endl; break;}d += 1.0/i;

}cout << "d = " << d << endl;

Ausgabe:

i = 6d = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.2833

?

Page 20: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Schleifen: continue - Anweisung

continue : Rest des Anweisungsblocks wird übersprungen und die nächste Iteration ausgeführt

double d = 0.0;for(int i = 1; i < 100; i++){

if (i > 5){ cout << i << ", "; continue;}d += 1.0/i;

}cout << "d = " << d << endl;

Ausgabe:

i = 6, 7, 8, 9, 10, … 99d = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.2833

?

Page 21: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 4 – Aufgabe 1

Transformiere die while Schleife in eine do-while Schleife

Transformiere die do-while Schleife in eine while Schleife

int t = 17, f = 0;while(t > 3)t /= 2f += t;

int t = 17, f = 0;if( t > 3) do t /= 2; while(t > 3);f += t;

int a = 2, b = 0;do{ a *= a; b += a;}while(a < 30)

int a = 2, b = 0;a *= a;b += a;while(a < 30){ a *= a; b += a;}

?

?

Page 22: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 4 – Aufgabe 1

Transformiere die for - Schleife in eine while - Schleife

Transformiere die while - Schleife in eine for - Schleife

int k = 0;for (int a = 0; a < 15; a *= a, a -= 3) k = a;

int k = 0;int a = 0;while(a < 15){ k = a; a *= a; a -= 3;}

?

int i = 25, j = 0;int k = 0;while(i > j && i > 0){ k = i; i = i – 2*j; j++;}

int k = 0;for(int i = 25, j = 0; i > j && i > 0; i = i-2*j,j++) k = i; ?

Page 23: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 4 – Aufgabe 2

Programmanalyse1. int main(int argc, char** argv){2. int a, b, c;3. cout << "Einlesen von a und b "

<< endl;4. cin >> a >> b;5. while(b != 0){6. c = a % b;7. a = b;8. b = c;9. }10. cout << "a = " << a << endl;11. return 0;12. }

Was gibt das Programm aus, wenn a = 11 und b = 7?

Ausgabe: 1

Was gibt das Programm aus, wenn a = 12 und b = 9?

Ausgabe: 3

Was berechnet dieses Programm?

ggT (grösster gemeinsamer Teiler)

?

?

?

Page 24: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Übung 4 – Aufgabe 3

Um Zufallszahlen zu generieren verwende srand

srand kann mit time(NULL)initialisiert werden

Dokumentation zu srand und time findet ihr unter http://www.cplusplus.com/reference/

Verwendet zwei verschachtelte Schleifen Die äussere Schleife wird für alle n Stellen des Passworts

ausgeführt

Die innere Schleife generiert solange ein neues Zeichen bis das gewünschte Zeichen vorliegt

Gewünschte Zeichen sind A-z, 0-9, und Sonderzeichen (siehe ASCII-Tabelle)

Page 25: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

ASCII-Tabelle

Char Dec Oct Hex Char Dec Oct Hex Char Dec Oct Hex Char Dec Oct Hex(nul) 0 0 0x00 (sp) 32 40 0x20 @ 64 100 0x40 ` 96 140 0x60(soh) 1 1 0x01 ! 33 41 0x21 A 65 101 0x41 a 97 141 0x61(stx) 2 2 0x02 " 34 42 0x22 B 66 102 0x42 b 98 142 0x62(etx) 3 3 0x03 # 35 43 0x23 C 67 103 0x43 c 99 143 0x63(eot) 4 4 0x04 $ 36 44 0x24 D 68 104 0x44 d 100 144 0x64(enq) 5 5 0x05 % 37 45 0x25 E 69 105 0x45 e 101 145 0x65(ack) 6 6 0x06 & 38 46 0x26 F 70 106 0x46 f 102 146 0x66(bel) 7 7 0x07 ' 39 47 0x27 G 71 107 0x47 g 103 147 0x67(bs) 8 10 0x08 ( 40 50 0x28 H 72 110 0x48 h 104 150 0x68(ht) 9 11 0x09 ) 41 51 0x29 I 73 111 0x49 i 105 151 0x69(nl) 10 12 0x0a * 42 52 0x2a J 74 112 0x4a j 106 152 0x6a(vt) 11 13 0x0b + 43 53 0x2b K 75 113 0x4b k 107 153 0x6b(np) 12 14 0x0c , 44 54 0x2c L 76 114 0x4c l 108 154 0x6c(cr) 13 15 0x0d - 45 55 0x2d M 77 115 0x4d m 109 155 0x6d(so) 14 16 0x0e . 46 56 0x2e N 78 116 0x4e n 110 156 0x6e(si) 15 17 0x0f / 47 57 0x2f O 79 117 0x4f o 111 157 0x6f(dle) 16 20 0x10 0 48 60 0x30 P 80 120 0x50 p 112 160 0x70(dc1) 17 21 0x11 1 49 61 0x31 Q 81 121 0x51 q 113 161 0x71(dc2) 18 22 0x12 2 50 62 0x32 R 82 122 0x52 r 114 162 0x72(dc3) 19 23 0x13 3 51 63 0x33 S 83 123 0x53 s 115 163 0x73(dc4) 20 24 0x14 4 52 64 0x34 T 84 124 0x54 t 116 164 0x74(nak) 21 25 0x15 5 53 65 0x35 U 85 125 0x55 u 117 165 0x75(syn) 22 26 0x16 6 54 66 0x36 V 86 126 0x56 v 118 166 0x76(etb) 23 27 0x17 7 55 67 0x37 W 87 127 0x57 w 119 167 0x77(can) 24 30 0x18 8 56 70 0x38 X 88 130 0x58 x 120 170 0x78(em) 25 31 0x19 9 57 71 0x39 Y 89 131 0x59 y 121 171 0x79(sub) 26 32 0x1a : 58 72 0x3a Z 90 132 0x5a z 122 172 0x7a(esc) 27 33 0x1b ; 59 73 0x3b [ 91 133 0x5b { 123 173 0x7b(fs) 28 34 0x1c < 60 74 0x3c \ 92 134 0x5c | 124 174 0x7c(gs) 29 35 0x1d = 61 75 0x3d ] 93 135 0x5d } 125 175 0x7d(rs) 30 36 0x1e > 62 76 0x3e ^ 94 136 0x5e ~ 126 176 0x7e(us) 31 37 0x1f ? 63 77 0x3f _ 95 137 0x5f (del) 127 177 0x7f

Page 26: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Advanced Topics

ostream::operator<< ostream& operator<< (bool& val );

ostream& operator<< (int& val ); ostream& operator<< (long& val ); ostream& operator<< (float& val ); ...

ostream

Ein Datentyp wie bool, int, float, …

operator<<

Eigentlich wie eine Funktion (z.B. main())

Aber mit speziellem Syntax

Auch +,-,*, usw. sind Operatoren! Wozu könnten Operatoren nützlich sein?

© Roland Angst, 2010 Institute of Visual Computing

Page 27: © Olivier Saurer, 2010Institute of Visual Computing Informatik I for D-MAVT Exercise Session 4

© Olivier Saurer, 2010 Institute of Visual Computing

Advanced Topics

std::cout Eine Variable (genannt Object) vom Typ ostream

Wird in <iostream> definiert und initialisiert

std::cout<< “Hallo Welt!”; Ruft den <<-Operator des ostream-Objects cout

auf: Äquivalentes Statement: std::cout<<(“Hallo Welt”);

Demo…

© Roland Angst, 2010 Institute of Visual Computing