Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Raphael Fischer fischrap@student.ethz.ch 27.03.2014 Informatik II - Übung 05 Raphael Fischer fischrap@student.ethz.ch 27.03.2014.

Ähnliche Präsentationen


Präsentation zum Thema: "Raphael Fischer fischrap@student.ethz.ch 27.03.2014 Informatik II - Übung 05 Raphael Fischer fischrap@student.ethz.ch 27.03.2014."—  Präsentation transkript:

1 Raphael Fischer fischrap@student.ethz.ch 27.03.2014
Informatik II - Übung 05 Raphael Fischer

2 Besprechung Übungsblatt 4
Raphael Fischer

3 L4.A1 – Stack Zwei Attribute: buffer und size
Vergrößern der Kapazität durch grow() Bedingung für grow(): in push(): size() == capacity() Java-Bibliotheksfunktionen möglich: int[] Arrays.copyOf(int[] original, int newLength) Raphael Fischer

4 L4.A2 – Ackermann-Funktion
Rekursive Definition der Ackermann-Funktion: Raphael Fischer

5 L4.A2 – Ackermann-Funktion
Raphael Fischer

6 L4.A2.b – Pseudocode (Beispiel)
n m L4.A2.b – Pseudocode (Beispiel) while(stack.size() > 1){ .... push n on stack push m on stack As long as the stack's size is greater than 1 pop the uppermost element from stack to m [m] pop the uppermost element from stack to n [n] if n = 0 then push m+1 on stack [A(0,m)=m+1] elseif m = 0 then push n-1 on stack; push 1 on stack [A(n,0)=A(n-1,1)] else push n-1 on stack push m-1 on stack [A(n,m)=A(n-1,A(n,m-1))] the uppermost element from the stack is the result “Funktionsaufruf” if n == 0  result = m+1 else if m == 0  push(n-1), push(1) else push(n-1), push(n), push(m-1) Raphael Fischer

7 L4.A3 – Bytecode Quellcode-Bytecode-Zuordnung klar?
Parameterreihenfolge / Rückgabe klar? return A(n-1, A(n, m-1)) 21: aload 0 22: iload 1 23: iconst 1 24: isub 25: aload 0 26: iload 1 27: iload 2 28: iconst 1 29: isub 30: invokevirtual 33: invokevirtual 36: ireturn Raphael Fischer

8 Call by value vs. Call by reference
Aufgabe 1 Raphael Fischer

9 Lösung // swap1: Geht nicht void swap2(int a, int b) { //Kein Effekt }
void swap1(int & a, int & b) { int t = a; a = b; b = a; } void swap2(int a, int b) { } // swap1: Geht nicht void swap2(int a, int b) { //Kein Effekt } ((Vorname Nachname))

10 Lösung void swapFoo1(Foo & a, Foo & b) { Foo t = a; a = b; b = t; } void swapFoo2(Foo a, Foo b) { //Swap x-Values of a and b: int t = a.x; a.x = b.x; b.x = t; //Swap y-Values of a and b: t = a.y; a.y = b.y; b.y = t; void swapFoo1(Foo a, Foo b) { //Swap x-Values of a and b: int t = a.x; a.x = b.x; b.x = t; //Swap y-Values of a and b: t = a.y; a.y = b.y; b.y = t; } void swapFoo2(Foo a, Foo b) { //Kein Effekt } ((Vorname Nachname))

11 Call by reference / by value: C++ vs Java
C++ und Java: swap(int a, int b) Funktioniert nicht! Nur C++: swap(int& a, int& b) Funktioniert swap(int a, int b): int a = 1; int b = 2; swap(int& a, int& b): a: b: int a = 1; int b = 2; int a = 1; int b = 2; Raphael Fischer

12 Call by reference / by value: C++ vs Java
C++ Style: Objekte sind genau wie primitive Typen: SwapFoo1 funktioniert SwapFoo2 funktioniert nicht! Java Style: Die Referenzen auf Objekte werden kopiert (Call- by-value) Nur SwapFoo2-Style funktioniert swapFoo2(Foo a, Foo b): Foo a: int x = 1; int y = 2; Foo b: int x = 2; int y = 1; Foo a: int x = 1; int y = 2; Foo b: int x = 2; int y = 1; swapFoo(Foo a, Foo b): Foo a: Foo b: Foo a: int x = 1; int y = 2; Foo b: int x = 2; int y = 1; Foo a: Foo b: Raphael Fischer

13 Listen Raphael Fischer

14 Listen public class List { public int value; public List next; public List(int value, List next) { this.value = value; this.next = next; } myList value 76 next value 15 next value 22 next value 3 next value 32 next null Raphael Fischer

15 Aufgabe 2 - Lösung Liste: Array: Stack: List:
Entfernen eines Wertes ist einfach («aushängen») Einfügen eines Wertes ist auch einfach Ausgeben aller Werte ist einfach (den Next-Pointern folgen) Array: Es ist schnell, den n-ten Wert zu lesen (a[n];) Stack: Hinzufügen neuer Werte mit push N-mal pop List: Neue Elemente/Aufgaben hinten anhängen Zu bearbeitende Elemente vorne lesen und entfernen Raphael Fischer

16 Übungsblatt 5 Hilfsklasse Lists mit vielen static Funktionen, die
Aufgabe 1: Listen nicht verändern Aufgabe 2: Listen verändern Aufgabe 3: Listen sortieren Raphael Fischer

17 Beispiel: toString() 76,15,22,3,34,null myList null
public static String toString(List list) { if (list == null) return "null"; return list.value + "," + toString(list.next); } myList value 76 next value 15 next value 22 next value 3 next value 32 next null 76,15,22,3,34,null u5a1.Lists.toString(myList) Raphael Fischer

18 Aufgaben Verwendet nirgends Schleifen!
Nur Rekursion und andere Hilfsfunktionen Raphael Fischer

19 Ü5.A1: Funktionen, die Listen nicht verändern
String toString(List list) List add(List list, int value) int size(List list) int sum(List list) List sublist(List list, int index) throws ... List last(List list) int valueAt(List list, int index) throws ... int index(List list, int value) throws ... Bereits implementiert Aufgabe 1 Wieso keine Veränderung? Raphael Fischer

20 Ü5.A2: Funktionen, die Listen verändern
void append(List list, int value) throws ... void concat(List head, List tail) throws ... void insertAt(List list, int i, int value) throws ... void insertAt(List list, int i, List nl) throws ... List remove(List list, int index) throws ... Aufgabe 2 Warum?

21 Ü5. A3 Listen sortieren List insertSorted(List list, int value) throws ... List sort(List list) throws ... Aufgabe 3 InsertSorted: Füge Wert in bereits sortierte Liste ein sort: Nehme ein Element nach dem anderen und füge es in neue Liste ein Ihr dürft Schleife verwenden Challenge: Es gibt aber Lösung mit 2-3 Zeilen! (Rekursiv) Raphael Fischer

22 Ü5.A4 – Dynamisch wachsender Stack mit verketteten Listen
Neuimplementieren der Stack-Methoden mit Hilfe der Liste Raphael Fischer

23 …viel Spass! Raphael Fischer


Herunterladen ppt "Raphael Fischer fischrap@student.ethz.ch 27.03.2014 Informatik II - Übung 05 Raphael Fischer fischrap@student.ethz.ch 27.03.2014."

Ähnliche Präsentationen


Google-Anzeigen