Ok well I got a final tomorrow and for the life of me I can not understand recursion. I have tried and tried and no hope lol. The professor gave us a study guide and I cant not figure out 2 problems. Just wondering if anyone can help me before I take this Final tomorrow. Thanks in advance.

(a) Write and test a recursive function that prints the extreme characters on separate lines. For
example: a parameter of “IMAGINE” It would print:
IE
MN
AI
G

(b) Write a Recursive function that given an integer n computes and returns the sum of the first even
integers less than or equal to n. For example: n=10, it would compute and return 2+4+6+8+10. For
n=11, it would compute and return 2+4+6+8+10.

for (b) I tried it. I got something along the lines of

int a(int n)
{
if(n<=0)
return 0;
else
if(n%2==0)
return n+a(n-2)
}

click here for a good example on recursion.

commented: Well played. +5

teehee. that recursion joke never gets old. i am giggling on the inside.

me too, but my final is gonna kill me lol. its 12am here and im still lost... :(.

The first one is easy. Just make a function that strips off the ends of a string:

void stripper(string word)
{
     int size = word.size();     

     //Let the strippin' begin!
     if(size > 2)
     {
          cout << word[0] << word[size] << endl;
          word = word.substr(1, size-2);
          
          //enjoy recursive deliciousness
          stripper(word);
     }
     else if(size == 2)
     {
          cout << word[0] << word[1] << endl;
          return;
     }
     else 
     {
          cout << word[0] << endl;
          return;
     }
}

I'll leave ye' with this for now.. show us what ye' can do with problem #2.

Thanks man. Ill try problem 2. My stupid Visual studio keeps giving me random errors with files not even associated with my code.lol Thanks again though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.