I Had This Homework About The Recursive Math, But My Professor Gave Us Very Vague Lecture Information.

This First Recursive Program:
 ``123456789101112131415161718192021`` ``````#include using namespace std; int f (int n) { if (n == 5) { return 0; } else { return n * n * n + f (n-1); } } int main() { cout<<"1^3 + 2^3 + 3^3 + ... + n^3 = \n"<

Is Calling Statement Of n=50;
Which Mean The Increment By 1, Starting From
 2^3 + 3^3 + 4^3+ ... + n^3 and for n = 50

But Base On This Program, My Professor Also Want To Have Similar Recursive, This Time, Instead Of The Increment Of The n Integers. He Want The Program To Be Written Something Like This:

 8^2 + 8^3 + 8^4 + ... + 8^n and for n = 30

I Really Lost From Here.
Could Somebody Give Me Some Hint / Helps Base On Program 1 And Applies The Concepts Into The Program 2, Please?

Much Appreciate And Thanks In Advanced.
On entering f(n) for the second problem, you need to calculate 8^n.
You'll need a loop to do that.
Then call f(n-1) and add the result.
Does that help?
Here you go chap:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include #include using namespace std; int b(int base) { int a = 0; int n = 1; for(int nN = 0;nN < base;nN++) { a = n * n * n + a; n++; } return a; } int e(int base,int exponent) { int a = base; int b = base; int n = 1; int c = 0; for(int nN = 0;nN < exponent;nN++) { for(int nN2 = 1;nN2 < n;nN2++) { a = a * b; } c += a; a = base; n++; } return c; } int main(int nNumberofArgs,char* pszArgs[]) { char input; int base; int exponent; int output; cout << "This is a recursive math program.\nEnter b to change base. Enter e to change exponents:"; cin >> input; switch(input) { case 'b': case 'B': cout << "1^3+2^3+3^3...n^3" << endl; cout << "Enter base: "; cin >> base; output = b(base); break; case 'e': case 'E': cout << "b^1+b^2+b^3...b^n" << endl; cout << "Enter base: "; cin >> base; cout << "Enter exponent: "; cin >> exponent; output = e(base,exponent); break; default: cout << "Please enter 'b' or 'e'"; break; } cout << "Output = " << output << endl; system("PAUSE"); return 0; } ``````

I like helping people :)
@greenleaf800073: This needs to be recursive.

I don't understand this:
 ``1234`` ``````if (n == 5) { return 0; }``````

What kind of condition is that? Is this what your teacher gave you?
@lowestone: Uh You mean ND04 right? Because I fixed that in my code.
greenleaf8000073 wrote:
@lowestone: Uh You mean ND04 right? Because I fixed that in my code.

I believe what he meant is that the OP requires a recursive solution and yours is not.

http://danzig.jct.ac.il/cpp/recursion.html
Topic archived. No new replies allowed.