Help me!!!
-
This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.
-
This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.
simply write - int main() { long a,b; long c = 0; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; for (int i = a; i <= b; ++i) c += i; cout << c; return 0; } your result() function is wrong which always returns zero. Manish Agarwal manish.k.agarwal @ gmail DOT com
-
simply write - int main() { long a,b; long c = 0; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; for (int i = a; i <= b; ++i) c += i; cout << c; return 0; } your result() function is wrong which always returns zero. Manish Agarwal manish.k.agarwal @ gmail DOT com
can you tell me why it is wrong and if i follow my way then what is the solution ? By the way, thank you for your reply. This is my first time, seem that i have a lot of work to do.
-
This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.
long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } this function is wrong because result(d + 1,0) = 0 bacause d+1 > 0 if you replace e for 0: result(d + 1,0)->result(d + 1,e), this function still wrong because result(d,e)=result(d+1,e)=.....=result(e,e)=0 ----------------- conglt
-
This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.
if (d >= 1 , d < e)
is legal syntax, but doesn't do what you want. Use&&
for "and" in boolean tests. Look at your exit condition ofresult()
- it only ever returns 0. Recursion is something you should really save for later, when you have more experience with the language.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.
long result(long d , long e) { if (d >= 1 , d <= e) { return d + result(d+1,e); } else return (0); } Instead of calling c = a + result(a , b) + b; only call c = result(a,b); Evaluation of recursive call is - return 2 + result(2+1,5); return 2 + 3 + result(3+1,5); return 2 + 3 + 4 + result(4+1,5); return 2 + 3 + 4 + 5 + result(6,5); After if condition became false evaluation is return 2 + 3 + 4 + 5 + 0; return 2 + 3 + 4 + 5 ; return 2 + 3 + 9; return 2 + 12 ; return 14 ; In your code long result(long d , long e) { if (d >= 1 , d < e)// After first iteration e became 0 and condition false and return 0; { return (result(d + 1,0)); } else return (0); } c = a+ result(a,b) + b = 2 + 0 + 5 Write recursive function carefully. Regards Amar:)
-
This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.
are you student from Viet Nam ??? In your program : you see again if statement: if (d >= 1 , d < e)//or (d>=1 && d
-
are you student from Viet Nam ??? In your program : you see again if statement: if (d >= 1 , d < e)//or (d>=1 && d
Thankz for all your reply. I know my problem now.