1 / 48

Informatik 1

Informatik 1. Übung 6. Übung 6. Nachbesprechung. Ich gebe nicht auf. Code schön formatieren. if ( x > 0) { a = 1; b = 1; // Kommentar. if (y > 0) { c = 1; } else { d = 1; } e = 1; }. Maximaler Wert im Array. Programmiermuster.

katina
Download Presentation

Informatik 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Informatik 1 Übung 6

  2. Übung 6 Nachbesprechung

  3. Ich gebe nicht auf • Code schön formatieren if (x > 0) { a = 1; b = 1; // Kommentar. if (y > 0) { c = 1; } else { d = 1; } e = 1; }

  4. Maximaler Wert im Array • Programmiermuster const int N = 100; // Anzahl Elemente int data[N]; // Das Array int maximum = 0; // Maximales bisher gefundenes Element // Alle Elemente durchprobieren for(int i=0; i<N; i++) { // Elementweise mit dem bisheringen Maximum vergleichen if (data[i] > maximum) { maximum = data[i]; } }

  5. Quiz • Was gibt das Programm aus? int x = 10; int y = 20; cout << x;

  6. Quiz • Was gibt das Programm aus? int x = 10; int y = 20; int* p = &y; cout << p;

  7. Quiz • Was gibt das Programm aus? int x = 10; int y = 20; int* p = &y; cout << *p;

  8. Quiz • Was gibt das Programm aus? int x = 10; int y = 20; int* p; cout << *p;

  9. Quiz • Was gibt das Programm aus? int x = 10; int y = 20; int* p = &x; *p = 11; p = &y; *p = 21; cout << x; cout << y;

  10. Quiz • Was gibt das Programm aus? int x[2] = {10, 20}; cout << (x+1);

  11. Quiz • Was gibt das Programm aus? int x[2] = {10, 20}; cout << *x + 1;

  12. Quiz • Was gibt das Programm aus? int x[2] = {10, 20}; cout << *(x + 1);

  13. Quiz • Was gibt das Programm aus? int x[4] = {1,2,3,5}; cout << *(x + x[*(x + 1)]);

  14. Listen • Dynamische Datenstrukturen • Besteht aus Zellen • Daten • Zeiger auf nächste Zelle • Zellen werden bei Bedarf erstellt

  15. Listen • Dynamische Datenstrukturen • Besteht aus Zellen • Daten • Zeiger auf nächste Zelle • Zellen werden bei Bedarf erstellt data data data data head next next next next

  16. Listen data data data data knoten head next next next next struct knoten { int data; // Daten der Zelle knoten* next; // Nächste Zelle } knoten* head; // Anfang der Liste (globale Variable)

  17. Listen data data data data head next next next next // Anfang der Liste knoten* head; // Wie komme ich zum markierten Element?

  18. Listen data data data data head next next next next // Anfang der Liste knoten* head; // Wie komme ich zum markierten Element? head->next->next->data

  19. Listen data data data data head next next next next // Anfang der Liste knoten* head; // Wie erstelle ich ein neues Element?

  20. Listen data data data data data head next next next next k next // Anfang der Liste knoten* head; // Wie erstelle ich ein neues Element? knoten* k = new knoten; k-> next = head; head = k;

  21. Listen data data data data head next next next next // Anfang der Liste knoten* head; // Wie gebe ich alle Elemente aus?

  22. Listen data data data data k head next next next next // Anfang der Liste knoten* head; // Wie gebe ich alle Elemente aus? knoten* k = head; while(k != NULL) { cout << k->data; k = k->next; }

  23. Vorlesung 6 wiederholung

  24. Referenzen • Wie Zeiger, aber • zeigen immer auf das selbe • können nicht „umgehängt“ werden • sind immer initialisiert

  25. Referenzen • Schreibweise // Schreibweise typ& name = ziel; // Ganze Zahl x int x = 1; // y zeigt auf x int& y = x; // Verändert den Wert von x von 1 auf 2 y = 2;

  26. Referenzen • Vergleich mit Zeigern

  27. Funktionen • Auslagern von Funktionalität • Wie mathematische Funktionen • y = f(x) • Rückgabewert • Argumente

  28. Funktionen • Schreibweise // Schreibweise rückgabe-typ name(argument-typargument-name, ...) { ... returnresultat; } // Schreibweise int summe(int a, int b) { return a + b; }

  29. Funktionen • Alle Typen erlaubt • ausser Arrays als Rückgabewerte • Kein Rückgabewert • void verwenden // Schreibweise void summe(int a, int b) { }

  30. Funktionen • Aufruf von Funktionen // Summiert zwei Zahlen int summe(int a, int b) { return a + b; } // Benutzt die summe-Funktion int main() { int i = summe(1,2); }

  31. Funktionen • Aufruf von Funktionen • Funktion muss definiert sein • Typen müssen stimmen

  32. Funktionen • Prototypen • Deklariert eine Funktion, die später implementiert wird // Schreibweise int summe(int a, int b); // Schreibweise int summe(int, int);

  33. Funktionen • Aufbau .cpp-Datei // Bibliotheken #include <iostream> // Globale Variablen – überall sichtbar int evil; // Funktionen int summe(int a, int b) { int r = a + b; // lokale Variable – nur im Block sichtbar return r; }

  34. Funktionen • Funktionsargumente • Typen müssen stimmen • Werte werden kopiert int summe(int a, int b) { return a + b; } int main() { int x = 1; int y = 2; int i = summe(x,y); }

  35. Funktionen • call by value void increase(int x) { x = x + 1; } int main() { int i = 1; increase(i); cout << i; }

  36. Funktionen • call by reference void increase(int&x) { x = x + 1; } int main() { int i = 1; increase(i); cout << i; }

  37. Quiz • Was gibt das Programm aus? int summe(int a, int b) { return a + b; } int main() { cout << summe(1,1); }

  38. Quiz • Was gibt das Programm aus? int summe(int a, int b) { return a + b; } int main() { cout << summe(1, “abc“); }

  39. Quiz • Was gibt das Programm aus? int summe(int a, int b) { return a + b; } int main() { cout << summe(1.9f, 1.8f); }

  40. Quiz • Was gibt das Programm aus? int summe(int a, int b) { return (a + b) / 2.0f; } int main() { cout << summe(1, 2); }

  41. Quiz • Was gibt das Programm aus? int increase(int a) { ++a; return a; } int main() { int x = 1; increase(x); cout << x; }

  42. Quiz • Was gibt das Programm aus? int increase(int&a) { ++a; return a; } int main() { int x = 1; increase(x); cout << x; }

  43. Quiz • Was gibt das Programm aus? int increase(int&a) { ++a; return a; } int main() { int a = 1; int x = 2; x = increase(x); cout << a; }

  44. Serie 7 Übung

  45. Turtle-Graphik • SchildkrötemitFarbe • Richtung • Position • Funktionssyntax • :: TeilvomFunktionsnamen // Bewegt die turtle 10 pixel vorwärts. ifm::forward(10);

  46. Raytracer • Fertiges Programm • Zwei Funktionen implementieren • Diese werden bereits verwendet

  47. Raytracer

  48. Raytracer • Vector3f • magischerTyp • Vektoraddition und Skalarmultiplikation Vector3f v1, v2, v3; // Vektoraddition v1 = v2 + v3; // Skalarprodukt float f = v1.dot(v2); // Normalisiert den Vektor v1. normalize();

More Related