rekursion addition
-
mngbd schrieb:
ensi schrieb:
am ende hat man doch logischerweise die summe 1 + 2 + 3 + 4 ... + 100, weil es doch rückwärts vom stack genommen wird oder?
Ja.
Die Summe erhält man aber (wenn man
return z
stattreturn 1
sagt), als 100 + 99 + ... + 1.
-
ensi schrieb:
also wo ist jetzt der hacken?
, d.h. ich überspring die 100?
Ne, da hab ich mich doch geirrt...
ensi schrieb:
am ende hat man doch logischerweise die summe 1 + 2 + 3 + 4 ... + 100, weil es doch rückwärts vom stack genommen wird oder?
Für die Summenbildung müsstest du deine Funktion geringfügig ändern, damit der richtige Wert rauskommt:
int zaehlen ( int z ) { if ( z >= 100 ) return z; return z + zaehlen (z+1); }
Was deinen Stackoverflow angeht, k.A. ... was hast du fürn Rechner?
-
noobLolo schrieb:
EOP schrieb:
int zaehlen( int i ) { static int z = 0; z = i; return( z < 10 ? zaehlen( z + 1 ) : z ); }
was soll denn der schmarn?
Na dann probier's doch aus du noob. :p
-
EOP schrieb:
Na dann probier's doch aus du noob. :p
das ist auf den ersten blick schon so madig das tu ich meinem compiler sicher nicht an. aber wenn du mir erklärst wofür das "static int z;" steht überleg ichs mir nochmal...
lg lolo
-
nicht alles was nicht falsch ist ist richtig
-
Oder noch besser:
int zaehlen( int i ) { static int z = 0; return z = i, z < 10 ? zaehlen( z + 1 ) : z ; }
-
noobLolo schrieb:
nicht alles was nicht falsch ist ist richtig
Nicht jeder, der keinen Plan hat liegt richtig.
Das ist völlig korrektes C. Du solltest dir mal die Definition von static in Funktionen durchlesen. :p
-
static
hat da nichts zu suchen.
-
EOP schrieb:
Oder noch besser:
int zaehlen( int i ) { static int z = 0; return z = i, z < 10 ? zaehlen( z + 1 ) : z ; }
omg schlimmer gehts immer;)
-
EOP schrieb:
Oder noch besser:
int zaehlen( int i ) { static int z = 0; return z = i, z < 10 ? zaehlen( z + 1 ) : z ; }
Je unleserlicher, umso besser, oder wie? (Davon mal abgesehen, das die Funktion nicht das macht, was sie soll.
)
-
volkard schrieb:
static
hat da nichts zu suchen.Und wieso bitteschön? Das ist eine völlig übliche Vorgehensweise innerhalb rekursiver funktionen.
Big Brother schrieb:
(Davon mal abgesehen, das die Funktion nicht das macht, was sie soll.
)
Ach gibt sie etwa nicht 10 aus? (respektive 100 oder was auch immer).
-
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ö.
-
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.
-
EOP schrieb:
Big Brother schrieb:
(Davon mal abgesehen, das die Funktion nicht das macht, was sie soll.
)
Ach gibt sie etwa nicht 10 aus? (respektive 100 oder was auch immer).
Ja, tut sie. Es soll aber die Summe der Zahlen ausgegeben werden, also 1+2+...+10, bzw. 1+2+...+100.
-
Big Brother schrieb:
EOP schrieb:
Big Brother schrieb:
(Davon mal abgesehen, das die Funktion nicht das macht, was sie soll.
)
Ach gibt sie etwa nicht 10 aus? (respektive 100 oder was auch immer).
Ja, tut sie. Es soll aber die Summe der Zahlen ausgegeben werden, also 1+2+...+10, bzw. 1+2+...+100.
Genauer lesen:
gefordett ist eine rekursive funktion, die von 1 bis 100 zählt ( mehr nicht) :
Genau das macht sie:
int zaehlen( int i ) { static int z = 0; z = i; cout << "z: " << z << endl; return z < 10 ? zaehlen( z + 1 ) : z ; }
-
#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.