Please Help Recursive Math
Feb 17, 2013 at 12:07am Feb 17, 2013 at 12:07am UTC
I Had This Homework About The Recursive Math, But My Professor Gave Us Very Vague Lecture Information.
This First Recursive Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
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" <<endl;
cout<<f(20)<<"\n" <<endl;
system("pause" );
return 0;
}
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.
Feb 17, 2013 at 12:23am Feb 17, 2013 at 12:23am UTC
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?
Feb 17, 2013 at 12:42am Feb 17, 2013 at 12:42am UTC
Here you go chap:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
#include <cstdio>
#include <cstdlib>
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 :)
Feb 17, 2013 at 1:14am Feb 17, 2013 at 1:14am UTC
@greenleaf800073: This needs to be recursive.
I don't understand this:
1 2 3 4
if (n == 5)
{
return 0;
}
What kind of condition is that? Is this what your teacher gave you?
Feb 17, 2013 at 1:15am Feb 17, 2013 at 1:15am UTC
@lowestone: Uh You mean ND04 right? Because I fixed that in my code.
Feb 17, 2013 at 6:10am Feb 17, 2013 at 6:10am UTC
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.