
-----------------------------------
Poisonous
Wed Jan 10, 2007 4:49 pm

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.

-----------------------------------
r.3volved
Wed Jan 10, 2007 5:20 pm

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
Wed Jan 10, 2007 5:27 pm

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
Wed Jan 10, 2007 6:36 pm

Re: Recursion
-----------------------------------
finally! :eyes:  :wall: 


#include 
using namespace std;
void power(int n, int c, int T);
int p = 0;

int main()
{
  int nNum, nCommonRatio;
  coutnNum;
  coutnCommonRatio;
  power(nNum, nCommonRatio, nCommonRatio * nNum);
  return 0;
}

void power(int n, int c, int T)
{
	if(p==n)
		return;
	p++;
	cout1 character.

-----------------------------------
Poisonous
Thu Jan 11, 2007 5:07 pm

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.

yeah..i didn't realize that..

now i have trouble with the code, just don't know where/how to start..

-----------------------------------
wtd
Thu Jan 11, 2007 5:24 pm

Re: 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.

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
Thu Jan 11, 2007 5:32 pm

Re: 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.

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
Thu Jan 11, 2007 5:56 pm

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.
