übungsquellcode
-
int main() //Hier fängt dein Programm logisch an! { int count; for (count=1;count<=5;++count) //eine Schleife, die von 1 bis einschließlich 5 zählt { b+=f1(a+1)+1; //gibt es (0+1)+1 also 2 in die funktion f1? cout << b << " "; //Ausgabe von b } } int f1(int a) //Definition der Funktion f1 { b = f2(a+1)+1; //und hier kommt dann (2+1)+1? 4 dies wird an f2 gegeben? return (b); } int f2(int a) //Definition der Funktion f2 { return (b+a); //(1+4)=5 und jetzt? }
-
S.lukas schrieb:
int main() //Hier fängt dein Programm logisch an! { int count; for (count=1;count<=5;++count) //eine Schleife, die von 1 bis einschließlich 5 zählt { b+=f1(a+1)+1; //gibt es (0+1)+1 also 2 in die funktion f1? -> Nein, es wird a+1 an die Funktion f1 übergeben, also 1 cout << b << " "; //Ausgabe von b } } int f1(int a) //Definition der Funktion f1 { b = f2(a+1)+1; //und hier kommt dann (2+1)+1? 4 dies wird an f2 gegeben? --> Nein, es wird all das, was in den Klammern steht, an die Funktion übergeben return (b); } int f2(int a) //Definition der Funktion f2 { return (b+a); //(1+4)=5 und jetzt? --> Das wird an die Stelle zurückgegeben, von der Du f2 aufgerufen hast. Dort steht f2(...). Dafür setzt Du jetzt den hier berechneten Ausdruck ein }Viele Grüße
freakC++
-
yeah habe es jetzt verstanden jetzt
-

-
komme aber nicht auf die
9 25 57 121 249
-
#include <iostream> using namespace std; int a=0, b=1; int f1(int a); int f2(int b); int main() { int count; for (count=1;count<=5;++count) { b+=f1(a+1)+1; cout << b << " "; } } int f1(int a) { b = f2(a+1)+1; return (b); } int f2(int a) { return (b+a); }Am einfachsten (im Kopf) ist es, wenn man das Ganze gedanklich umschreibt. Erstmal verwenden wir nicht die gleichen Bezeichner mehrfach:
int a=0, b=1; int f1(int x); int f2(int y); int main() { int count; for (count=1;count<=5;++count) { b+=f1(a+1)+1; cout << b << " "; } } int f1(int x) { b = f2(x+1)+1; return (b); } int f2(int y) { return (b+y); }Man sieht dass sich der Wert von a nie ändert. Also wird f1 immer mit dem Wert 0+1=1 aufgerufen. f2 wird somit immer mit dem Wert 1+1=2 aufgerufen, also können die Parameter auch eliminiert werden:
int a=0, b=1; int f1(); int f2(); int main() { int count; for (count=1;count<=5;++count) { b+=f1()+1; cout << b << " "; } } int f1() { b = f2()+1; return (b); } int f2() { return (b+2); }Einsetzen von f2 in f1 ergibt:
int f1(int x) { b = b+2+1; // b+=3; return (b); }Einsetzen von f1 in main ergibt:
int main() { int count; for (count=1;count<=5;++count) { b+=3; b+=b+1; cout << b << " "; } }also
int main() { int count; for (count=1;count<=5;++count) { b=2*b+7; cout << b << " "; } }was ohne weiteres nachvollziehbar sein sollte.
-
vielen dank für die übersichtliche information.
-
camper schrieb:
int b=1; int main(){ // ... b+=f1(a+1)+1; //... } int f1(int a){ b = f2(a+1)+1; return (b); }Ich habe den Standard vor mir. Allein mein Standardsprech reicht nicht

Das hat alles eine definierte Abfolge, die ich nur nicht aus 1.9/13ff herauslesen kann?!
-
Furble Wurble schrieb:
camper schrieb:
int b=1; int main(){ // ... b+=f1(a+1)+1; //... } int f1(int a){ b = f2(a+1)+1; return (b); }Ich habe den Standard vor mir. Allein mein Standardsprech reicht nicht

Das hat alles eine definierte Abfolge, die ich nur nicht aus 1.9/13ff herauslesen kann?!Gut aufgepasst! Ist tatsächlich bedenkswert. Aber zum Glück unproblematisch
n3337 schrieb:
5.17 Assignment and compound assignment operators [expr.ass]
1 The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand. The result in all cases is a bit-field if the left operand is a bit-field. In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single compound assignment operator. —end note ]b=b+f1(a+1)+1;wäre hingegen unspezifiziert. Und ich könnte mal wieder die Frage in den Raum stellen, wie viele mögliche Ergebnisse es bei 5 Schleifendurchläufen gibt

-
Danke.
Ist 'ne schöne Anfängeraufgabe...