Recursion
Author |
Message |
Poisonous
|
Posted: Wed Jan 10, 2007 4:49 pm Post subject: Recursion |
|
|
OK, I need to do a recursion problem for C++. The user has to input a number and a common ratio. And I have to display the number times the common ratio to the power of a number which increments by one every time.
For example:
Number x Common Ratio^1
Number x Common Ratio^2
Number x Common Ratio^3
Number x Common Ratio^4
Number x Common Ratio^5
So for instance the output should look like this WITHOUT using the power function, using recursion :
5 x 3^1 = 15
5 x 3^2 = 45
5 x 3^3 = 135
You get the point. This is totally new stuff for me and I'm completely stumped.
I had it but I'm not supposed to use the power function built in c++. There is no point in doing recursion then.
Thanks in advance. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
r.3volved
|
Posted: Wed Jan 10, 2007 5:20 pm Post subject: RE:Recursion |
|
|
You need to post some sample input that you want and the sample output you would would like to achieve.
I don't understand where you application is supposed to end. |
|
|
|
|
|
Clayton
|
Posted: Wed Jan 10, 2007 5:27 pm Post subject: Re: Recursion |
|
|
create your own power function then! That's probably what is supposed to be recursive in this excercise. just remember that a power is a number multiplied by itself n times, where n is the exponent. |
|
|
|
|
|
Poisonous
|
Posted: Wed Jan 10, 2007 6:36 pm Post subject: Re: Recursion |
|
|
finally!
c++: |
#include <iostream>
using namespace std;
void power(int n, int c, int T);
int p = 0;
int main()
{
int nNum, nCommonRatio;
cout<<"Please enter the number\n";
cin>>nNum;
cout<<"Please enter the common ratio(base)\n";
cin>>nCommonRatio;
power(nNum, nCommonRatio, nCommonRatio * nNum);
return 0;
}
void power(int n, int c, int T)
{
if(p==n)
return;
p++;
cout<<n<<" x "<<c<<" to the power of "<<p<<" = "<<T<<endl;
power(n, c, T * c);
}
|
|
|
|
|
|
|
wtd
|
Posted: Wed Jan 10, 2007 9:16 pm Post subject: RE:Recursion |
|
|
If you're thinking about recursion, you should be thinking in terms of functions, rather than procedures (void functions).
Let's establish a baseline.
code: | int power(int number, int exponent)
{
...
} |
When exponent is zero, then no matter what the number is, the result is going to be one.
If the exponent is one, then the result will be number.
This is easily translated to code.
code: | int power(int number, int exponent)
{
if (exponent == 0)
{
return 1;
}
else if (exponent == 1)
{
return number;
}
else
{
?
}
} |
} |
|
|
|
|
|
Poisonous
|
Posted: Thu Jan 11, 2007 4:40 pm Post subject: Re: Recursion |
|
|
OK, after flying through a couple I got stuck on this one. anyone wanna give me a start.
Quote: Develop a recursive algorithm to determine if there is a palindrome hidden within a longer word or phrase. A palindrome is a word or phrase that has the same sequence of letters when read from left to right and when read from right to left, ignoring the spaces (e.g., “Some like cake, but I prefer pie” contains the palindrome “I prefer pi”). |
|
|
|
|
|
md
|
Posted: Thu Jan 11, 2007 4:43 pm Post subject: RE:Recursion |
|
|
the first step would be to write a function that determines if the passed string is a palindrome. Then you can go through all the possible substrings testing each to see if it is a palindrome (using your new function). |
|
|
|
|
|
wtd
|
Posted: Thu Jan 11, 2007 4:55 pm Post subject: RE:Recursion |
|
|
There are two strings that are always palindromes: a string of length zero, and a string of length one.
That's your foundation. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Poisonous
|
Posted: Thu Jan 11, 2007 4:59 pm Post subject: Re: Recursion |
|
|
But the thing is that the palindrome doesn't necessarily have to consist of one word/phrase. It can be hidden anywhere in the sentence like in the example above. |
|
|
|
|
|
Poisonous
|
Posted: Thu Jan 11, 2007 5:01 pm Post subject: Re: RE:Recursion |
|
|
wtd @ Thu Jan 11, 2007 4:55 pm wrote: There are two strings that are always palindromes: a string of length zero, and a string of length one.
That's your foundation.
err..wanna elaborate? |
|
|
|
|
|
Clayton
|
Posted: Thu Jan 11, 2007 5:02 pm Post subject: Re: Recursion |
|
|
so if spaces don't count, take them out. From there you can work through the string looking for a palindrome >1 character. |
|
|
|
|
|
Poisonous
|
Posted: Thu Jan 11, 2007 5:07 pm Post subject: Re: Recursion |
|
|
Freakman @ Thu Jan 11, 2007 5:02 pm wrote: so if spaces don't count, take them out. From there you can work through the string looking for a palindrome >1 character.
yeah..i didn't realize that..
now i have trouble with the code, just don't know where/how to start.. |
|
|
|
|
|
wtd
|
Posted: Thu Jan 11, 2007 5:24 pm Post subject: Re: RE:Recursion |
|
|
Poisonous @ Fri Jan 12, 2007 6:01 am wrote: wtd @ Thu Jan 11, 2007 4:55 pm wrote: There are two strings that are always palindromes: a string of length zero, and a string of length one.
That's your foundation.
err..wanna elaborate?
An empty string is a palindrome. So is a string with only one character.
If you can get the first and last characters from a string, and they're not the same, then obviously the string is not a palindrome. If they are though, then you need to test the rest of the string.
Eventually, if you do this long enough, you will end up with "the rest of the string" being either an empty string, or a string with only one character. |
|
|
|
|
|
Poisonous
|
Posted: Thu Jan 11, 2007 5:32 pm Post subject: Re: RE:Recursion |
|
|
wtd @ Thu Jan 11, 2007 5:24 pm wrote: Poisonous @ Fri Jan 12, 2007 6:01 am wrote: wtd @ Thu Jan 11, 2007 4:55 pm wrote: There are two strings that are always palindromes: a string of length zero, and a string of length one.
That's your foundation.
err..wanna elaborate?
An empty string is a palindrome. So is a string with only one character.
If you can get the first and last characters from a string, and they're not the same, then obviously the string is not a palindrome. If they are though, then you need to test the rest of the string.
Eventually, if you do this long enough, you will end up with "the rest of the string" being either an empty string, or a string with only one character.
I still don't know what you're trying to get at.
would the above be true for:
SomelikecakebutIpreferpie |
|
|
|
|
|
wtd
|
Posted: Thu Jan 11, 2007 5:56 pm Post subject: RE:Recursion |
|
|
You have two separate problems. You need to find all possible substrings, and you need to check those substrings to see if they are palindromes.
If you try to think about it as one problem, I guarantee you will be working on this same problem ten years from now. |
|
|
|
|
|
|
|