1 / 67

Informatik I for D-MAVT

Exercise Session 8. Informatik I for D-MAVT. Themen. Nachbesprechung Übung 6 Pointer/ Referenzen und Funktionen Rekursion Vorbesprechung Übung 8. Nachbesprechung. Pointer und Arrays.

bevis
Download Presentation

Informatik I for D-MAVT

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. Exercise Session 8 Informatik I for D-MAVT

  2. Themen • NachbesprechungÜbung 6 • Pointer/Referenzen und Funktionen • Rekursion • VorbesprechungÜbung 8

  3. Nachbesprechung • Pointer und Arrays Wiesobrauchtesbeider Definition vomersten Element des float arrays a " a[0] = 5; " und beider Definition des floats b " *b = 4; “? #include <iostream> using namespace std; int main() { float* a = new float[4]; a[0] = 5; float* b = new float; *b = 4; delete[] a; delete b; } 1. Element: a[0] = 5 *a = 5 2. Element: a[1] = 5 *(a+1) = 5 b[0] = 4 *b = 4

  4. Übung 6: Nachbesprechung • Aufgabe 2: • Struct Darstellung a:

  5. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 … … next prev undef undef

  6. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev undef undef

  7. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev undef NULL

  8. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev … … next prev NULL 0x00346b10 undef undef

  9. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev NULL 0x00346b10 undef undef

  10. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev NULL 0x00346b10 undef

  11. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev … … next prev NULL 0x00346b10 0x00346b70 undef undef

  12. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 0x00346b70 undef undef

  13. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 0x00346b70 undef

  14. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 0x00346b70 NULL

  15. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 ptr 0x00346b70 NULL

  16. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 ptr 0x00346b70 NULL

  17. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 ptr 0x00346b70 NULL

  18. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 ptr NULL 0x00346b70 NULL

  19. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 ptr NULL 0x00346b70 NULL

  20. Repetition: Verkettete Listen structMannschaft { char* Name; unsigned intg,u,v; unsigned intToreG, ToreB; …Mannschaft* next;Mannschaft* prev;}; int main() { Mannschaft* Bundesliga = new Mannschaft; Bundesliga->Name = "FCB"; Bundesliga->prev = NULL; Bundesliga->next = new Mannschaft; Bundesliga->next->Name = "S04"; Bundesliga->next->prev = Bundesliga; Bundesliga->next->next = new Mannschaft; Bundesliga->next->next->Name = "Bremen"; Bundesliga->next->next->prev = Bundesliga->next; Bundesliga->next->next->next = NULL; Mannschaft* ptr = Bundesliga->next; Bundesliga->next = Bundesliga->next->next; Bundesliga->next->prev = Bundesliga; ptr->next = NULL; ptr->prev = Bundesliga->next; Bundesliga->next->next = ptr; return 0; } Bundesliga 0x00346ab0 FCB … next prev S04 … next prev Bremen … next prev NULL 0x00346b10 ptr NULL 0x00346b70

  21. Übung 6: Nachbesprechung • Aufgabe 3: • Dynamische Arrays • Grösstes Element ? ?

  22. Übung 6: Nachbesprechung • Aufgabe 3: • Sortieren (Bubble sort) http://fairuzelsaid.wordpress.com

  23. Übung 6: Nachbesprechung • Aufgabe 3: • Sortieren (Selectionsort) wikipedia.org

  24. Themen • NachbesprechungÜbung 6 • Pointer/Referenzen und Funktionen • Rekursion • VorbesprechungÜbung 8

  25. Repetition: Pointer structpoint_t{ float x; float y; }; Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  26. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  27. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  28. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  29. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  30. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  31. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  32. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  33. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  34. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  35. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  36. Repetition: Pointer structpoint_t{ float x; float y; } Stack (vereinfacht) point_t p; point_t* pp; point_t** ppp; point_t& rp = p; p.x = 1.0; p.y = 2.0; pp = &p; ppp = &pp; pp->x = 5.0; (*ppp)->y = 3.0; rp.x = 10;

  37. Repetition: Funktionen • Entscheide, ob mit dieser Funktionssignatur die Multiplikation (c = a*b) implementiert werden kann • Falls „Ja“, bestimme für jede Funktionssignaturwie die Funktion aufgerufen werden kann ? ? ? ? ?

  38. Themen • NachbesprechungÜbung 6 • Pointer/Referenzen und Funktionen • Rekursion • VorbesprechungÜbung 8

  39. Rekursion • Rekursion: Funktion die sich selbst aufruft • Abbruchbedingung –> endet Rekursion • Für jeden Funktionsaufruf wird ein neuer Satz lokaler Variablen angelegt

  40. Rekursion II Stack (vereinfacht) • Beispiel: intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  41. Rekursion II Stack (vereinfacht) • Beispiel: intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  42. Rekursion II Stack (vereinfacht) • Beispiel: intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  43. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  44. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  45. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  46. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  47. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum sum sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  48. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum sum sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; }

  49. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum sum sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; } sum

  50. Rekursion II Stack (vereinfacht) • Beispiel: main intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return 1 + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } intsum(int b){ if(b > 0) return b + sum(b-1); //Rekursion else return 0; //Abbruchbedingung } sum sum sum int main() { int b = 3; int a = 0; a = sum(b); // a = 6 return 0; } sum

More Related