Woche 6 Cedric Tompkin April 11, 2018 Cedric Tompkin Woche 6 April 11, 2018 1 / 29
Figure: Mehr Comics Cedric Tompkin Woche 6 April 11, 2018 2 / 29
Learning Objectives Dir kennst Return-by-value und Return-by-reference. Du weisst, wozu wir Referenzen brauchen. Du kennst Arrays und deren Schwächen. Du kennst Vectors und deren Stärken und Schwächen. Du weisst, wie man Swap und Maximal Value an einem Array macht. Du kennst Chars. Cedric Tompkin Woche 6 April 11, 2018 3 / 29
Welcome back 1 Woche 1 1 Das erste Programm: hello world, Ein- und Ausgabe 2 Ausdrücke: Korrektheit, Auswertungsreihenfolge, L- und R-Werte 3 Binärzahlen, Integers, unsigned Ints 2 Woche 2 1 Boolean: ==, >= usw., und, oder, not, Short-Circuit evaluation 2 if, else if etc. 3 Woche 3 1 for, while, do while 2 Break, continue 3 Scope 4 Double und Floats 1 Operationen:, % 5 Typen und Typeconversion Cedric Tompkin Woche 6 April 11, 2018 4 / 29
Welcome back 1 Woche 4 1 Fliesskommazahlen Binärdarstellung 2 Floatingpointsysteme 3 Wertebereich double und floats 4 Floating point guidelines 5 Function Declaration 6 Pre- und Postconditions 2 Woche 5 1 Libraries 2 Referenzen 3 Call by reference 4 Call by value Cedric Tompkin Woche 6 April 11, 2018 5 / 29
Nachbesprechung Wenn ihr wollt, dass ich euren Code genau anschaue, dann notiert das in einem Kommentar ganz oben. Je lesbarer der Code ist, desto besser kann ich darauf eingehen. Cedric Tompkin Woche 6 April 11, 2018 6 / 29
Nachbesprechung Figure: eine originelle Variablenwahl Cedric Tompkin Woche 6 April 11, 2018 7 / 29
Return by Value 1 // POST : return value is the number of distinct real solutions of the quadratic 2 // equation ax ^2 + bx + c = 0. If there are infinitely many solutions 3 // ( a = b = c = 0), the return value is -1. Otherwise, the return value 4 // is a number n from {0,1,2} and the solutions are written to x1,..., xn 5 int solve_quadratic_equation ( const double a, const double b, const double c, 6 double & x1, double & x2) Cedric Tompkin Woche 6 April 11, 2018 8 / 29
Return-by-Reference 1 int & increment ( int & m) { 2 return ++ m; 3 } 4 int main () { 5 int n = 3; 6 increment ( increment (n)); 7 return 0; 8 } Cedric Tompkin Woche 6 April 11, 2018 9 / 29
Mehr Gründe für Referenzen Angenommen Datentyp Matrix existiert Angenommen Matrix mit 10 20 Einträgen 1 void read_ij ( Matrix A, unsigned int i, unsigned int j) Was ist das Problem? Cedric Tompkin Woche 6 April 11, 2018 10 / 29
Mehr Gründe für Referenzen Angenommen Datentyp Matrix existiert Angenommen Matrix mit 10 20 Einträgen 1 void read_ij ( Matrix A, unsigned int i, unsigned int j) Was ist das Problem? Riesige Datenmenge muss kopiert werden Delay. Besser: 1 Adresse: 1 void read_ij ( Matrix & A, unsigned int i, unsigned int j) Cedric Tompkin Woche 6 April 11, 2018 10 / 29
Warum dies so ineffizient ist Figure: Prozessoren sind viel besser als Speichermedien Cedric Tompkin Woche 6 April 11, 2018 11 / 29
Mehr Gründe für Referenzen Es gibt Elemente, von denen man keine Kopien machen kann. Beispiel: std::cout Cedric Tompkin Woche 6 April 11, 2018 12 / 29
Mehr Gründe für Referenzen Es gibt Elemente, von denen man keine Kopien machen kann. Beispiel: std::cout 1 int a = 5; 2 int b = a; // making a copy of an int 3 std :: ostream o = std :: cout ; // Error : copying std :: cout is impossible 4 std :: ostream & o = std :: cout ; // This works, try it! Cedric Tompkin Woche 6 April 11, 2018 12 / 29
Arrays Arrays sind primitive Listen Kennen ihre Länge nicht Aufgabe des Programmierers, diese nicht zu verletzen. 1 int a[] = {7,5,0,3,8}; 2 std :: cout << a [0]; // outputs 7 3 std :: cout << a [4]; // outputs 8 4 std :: cout << a [5]; // outputs random garbage ( or sometimes segmentation fault ) 5 std :: cout << a [ -10]; // outputs random garbage ( or sometimes segmentation fault ) Cedric Tompkin Woche 6 April 11, 2018 13 / 29
Ein- und Ausgabe von Arrays 1 int numbers [ 10]; 2 for ( int i = 0; i < 10; i ++) 3 std :: cin >> numbers [i]; 4 5 for ( int i = 0; i < 10; i ++) 6 std :: cout << numbers [i] << " "; 7 std :: cout << "\n"; Cedric Tompkin Woche 6 April 11, 2018 14 / 29
Array to Array Assignment 1 int a[] = {7,5,0,3,8}; 2 int b [5]; 3 b = a; // does not compile Geht NICHT! for-loop 1 int a[] = {7,5,0,3,8}; 2 int b [5]; 3 for ( int i = 0; i < 5; i ++) 4 b[i] = a[i]; Cedric Tompkin Woche 6 April 11, 2018 15 / 29
Variable Länge Assignment und Grösse anzeigen möglich Cedric Tompkin Woche 6 April 11, 2018 16 / 29 Vectors Figure: Vectors
Vectors 1 std :: vector <int > a (10, 0); 2 std :: vector <int > b = a; // b is now 0000000000 3 std :: cout << a. size (); // Outputs 10 Cedric Tompkin Woche 6 April 11, 2018 17 / 29
Vectors 1 std :: vector <int > a (10, 0); 2 std :: vector <int > b = a; // b is now 0000000000 3 std :: cout << a. size (); // Outputs 10 1 a. push_back (7) ; 2 std :: cout << a. size () << "\n"; // outputs 11 3 for ( int i = 0; i < a. size (); i ++) // outputs 00000000007 4 std :: cout << a[i]; Cedric Tompkin Woche 6 April 11, 2018 17 / 29
Vectors 1 std :: vector <int > a (5,1) ; 2 std :: cout << a [0]; // outputs 1 3 std :: cout << a [4]; // outputs 1 4 std :: cout << a [5]; // outputs random garbage 5 std :: cout << a [ -10]; // outputs random garbage Cedric Tompkin Woche 6 April 11, 2018 18 / 29
Vectors 1 std :: vector <int > a (5,1) ; 2 std :: cout << a [0]; // outputs 1 3 std :: cout << a [4]; // outputs 1 4 std :: cout << a [5]; // outputs random garbage 5 std :: cout << a [ -10]; // outputs random garbage 1 std :: cout << a.at (0) ; // outputs 1 2 std :: cout << a.at (4) ; // outputs 1 3 std :: cout << a. at (5) ; // program crashes 4 std :: cout << a.at ( -10) ; // program crashes Cedric Tompkin Woche 6 April 11, 2018 18 / 29
Woran erkennt man, dass nicht ich diesen Code geschrieben habe? Cedric Tompkin Woche 6 April 11, 2018 18 / 29 Vectors 1 std :: vector <int > a (5,1) ; 2 std :: cout << a [0]; // outputs 1 3 std :: cout << a [4]; // outputs 1 4 std :: cout << a [5]; // outputs random garbage 5 std :: cout << a [ -10]; // outputs random garbage 1 std :: cout << a.at (0) ; // outputs 1 2 std :: cout << a.at (4) ; // outputs 1 3 std :: cout << a. at (5) ; // program crashes 4 std :: cout << a.at ( -10) ; // program crashes
Vectors 1 using namespace std ; 2 3 vector <int > a (5,1) ; 4 cout << a [0]; // outputs 1 5 cout << a [4]; // outputs 1 6 cout << a [ 5]; // outputs random garbage 7 cout << a [ -10]; // outputs random garbage Cedric Tompkin Woche 6 April 11, 2018 19 / 29
Vectors 1 using namespace std ; 2 3 vector <int > a (5,1) ; 4 cout << a [0]; // outputs 1 5 cout << a [4]; // outputs 1 6 cout << a [ 5]; // outputs random garbage 7 cout << a [ -10]; // outputs random garbage 1 vector <int > a (5,1) ; 2 cout << a.at (0) ; // outputs 1 3 a. at (2) = 5; // changes a to 11511 Cedric Tompkin Woche 6 April 11, 2018 19 / 29
Swap Programming Environment Cedric Tompkin Woche 6 April 11, 2018 20 / 29
Swap Programming Environment 1 int a[] = {6,1,3,2,1}; 2 // want to swap positions 1 and 3 ( positions range from 0 to 4) 3 int b = a [1]; 4 a [1] = a [3]; 5 a [3] = b; Hilfsvariable b Cedric Tompkin Woche 6 April 11, 2018 20 / 29
Maximal Element Programming Environment Cedric Tompkin Woche 6 April 11, 2018 21 / 29
Maximal Element Programming Environment 1 int a[] = {6,1,3,2,1}; 2 int n = 5; // number of elements of array a 3 // compare the current maximal element with the next number, if the next number is bigger it 4 // is the new maximal element 5 int b = a [0]; 6 for ( int i = 1; i < n; i ++) { // notice that i starts as 1, because b = a [0] 7 if(a[i] > b){ 8 b = a[i]; 9 } 10 } Cedric Tompkin Woche 6 April 11, 2018 21 / 29
Chars (65 =)1000001 = A (66 =)1000010 = B... (97 =)1100001 = a (98 =)1100010 = b... 1 char letter = a ; 2 int number = letter ; // implicit type conversion - > number = 97 3 int number = 66; 4 char letter = number ; // implicit type conversion - > letter = B Cedric Tompkin Woche 6 April 11, 2018 22 / 29
Chars Figure: ASCII table Cedric Tompkin Woche 6 April 11, 2018 23 / 29
Tipps Aufgabe 1: Wenn ihr euch nicht sicher seid, dann probiert es im Compiler aus. Aufgabe 2: Fast die Mulö auf cppreference min-max Überlegt euch genau, womit ihr int min; int max; initialisiert. longest sequence zwei Variablen, int lon_seq; int cur-seq Swap: bool swapped; do... while(swapped); Cedric Tompkin Woche 6 April 11, 2018 24 / 29
Zum Schluss eine ernste Frage Ist es möglich, sich mit senkrecht nach unten feuernden Maschinengewehren einen Jetpack zu bauen? Cedric Tompkin Woche 6 April 11, 2018 25 / 29
Zum Schluss Wir berücksichtigen Gewicht und Schubkraft: Schub-gewichts-verhältnis = Schub Gewicht Mondrakete Saturn V: 1.5, AK-47: ca. 2 Schubkraft = Massenauswurfsrate Ausströmgeschwindigkeit 10 Kugeln Sekunde 8Gramm 715 Meter Kugel Sekunde 5.8kg Ausströmendes Gas: +30% Gewehr kann ca. 2 kg mehr heben als Eigengewicht. Cedric Tompkin Woche 6 April 11, 2018 26 / 29
Zum Schluss Figure: Figure: Cedric Tompkin Woche 6 April 11, 2018 27 / 29
Zum Schluss Die Lösung: mindestens 25, im Idealfall mehr als 300 AK-47 mit je 250 Patronen geladen. Cedric Tompkin Woche 6 April 11, 2018 28 / 29
Zum Schluss Figure: Für weitere Antworten Cedric Tompkin Woche 6 April 11, 2018 29 / 29