rekursion addition
-
#include <stdio.h> int summeVon1Bis(int obergrenze){ if(obergrenze==0) return 0; else return obergrenze+summeVon1Bis(obergrenze-1); } int main () { printf("%d\n",summeVon1Bis(100)); return 0; }
-
So habe ich es auch anfangs verstanden, aber das ist wohl nicht gemeint.
Guckst du ensis Post um 22:51:52 am 08.04.2010
-
#include <stdio.h> void anzeigeVon1Bis(int obergrenze){ if(obergrenze){ anzeigeVon1Bis(obergrenze-1); printf("%d ",obergrenze); } } int main () { anzeigeVon1Bis(100); return 0; }
-
EOP schrieb:
volkard schrieb:
EOP schrieb:
volkard schrieb:
static
hat da nichts zu suchen.Und wieso bitteschön? Das ist eine völlig übliche Vorgehensweise innerhalb rekursiver funktionen.
Nö.
Sometimes, however, you want to retain a value between function calls. You could accomplish this by making a global variable, but that variable would not be under the sole control of the function. C and C++ allow you to create a static object inside a function; the storage for this object is not on the stack but instead in the program’s static storage area. This object is initialized once the first time the function is called and then retains its value between function invocations. For example, the following function returns the next character in the string each time the function is called:
Siehe link oben.
Lern Lisp!
Deine Funktion geht ohne static genauso.
-
volkard schrieb:
#include <stdio.h> int summeVon1Bis(int obergrenze){ if(obergrenze==0) return 0; else return obergrenze+summeVon1Bis(obergrenze-1); } int main () { printf("%d\n",summeVon1Bis(100)); return 0; }
Auch für dich gilt: Genauuer lesen!
gefordett ist eine rekursive funktion, die von 1 bis 100 zählt ( mehr nicht) :
Gefordert ist nicht eine Funktion, die die Zahlen von 1 bis 100 addiert.
-
das führt doch zu nichts. versuchen wir doch lieber gemeinsam probleme zu lösen anstatt uns gegenseitig zu dissen...
mit dem satz ist mir ein platz im dr. sommer team sicher
lg lolo
-
noobLolo schrieb:
... dr. sommer team sicher
...
du meintest zwei bei kallvas
-
Big Brother schrieb:
So habe ich es auch anfangs verstanden, aber das ist wohl nicht gemeint.
Guckst du ensis Post um 22:51:52 am 08.04.2010Oki doki - dann hatte ensi aber ein Problem, sich bzw. es korrekt zu erklären.
Hätte er besser gesagt: So wie Fakultät aber mit "und" statt mit "mal".
-
ensi schrieb:
* ich weiß nicht, woran es liegt, aber es gibt einen stack overflow - kann mir jemand helfen bitte?
Der Fehler ist nicht in Deinem Code. Deswegen kannste ihn auch nicht finden.
Vielleicht compilierst Du aus Versehen eine andere Datei.
Beim MS-Compiler ist die *.ncb korrupt und muß (während die IDE geschlossen ist) gelöscht werden.
Sowas halt.
-
volkard schrieb:
void anzeigeVon1Bis(int obergrenze){ if(obergrenze){ anzeigeVon1Bis(obergrenze-1); printf("%d ",obergrenze); } }
Da könnte man noch die Notwendigkeit eines Rekursionsstacks für die Ausgaben entfernen:
void anzeigeVonBis(int untergrenze, int obergrenze) { if (untergrenze <= obergrenze) { printf("%d ", untergrenze); anzeigeVonBis(untergrenze + 1, obergrenze); } } void anzeigeVon1Bis(int obergrenze) { anzeigeVonBis(1, obergrenze); }
Und dann könnte man eine neues Schlüsselwort einführen, um sich nicht darauf verlassen zu müssen, dass der Compiler erkennt, dass kein Stack notwendig ist:
void anzeigeVonBis(int untergrenze, int obergrenze) { while (untergrenze <= obergrenze) { printf("%d ", untergrenze); untergrenze++; } }