7 Replies - 39040 Views - Last Post: 08 October 2010 - 11:22 PM Rate Topic: -----

#1 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Recursive function that prints odd numbers

Posted 08 October 2010 - 07:48 PM

I'm trying to figure out how to get this function to work. Basically, I'll enter a number(eg. 9) and using recursion my program should print the odd numbers which would be 1,3,5,7, and 9. Right now, my program complies, runs, and then crashes. Based on my code, can someone help me and tell me where I'm going wrong?

#include <iostream>
using namespace std;

int printoddnum(int n); 

int main() 
{
     int n;
     
     cout<<"Enter a number"<<endl; 
     cin>>n; 
     
     printoddnum(n); 
     
     system("pause"); 
     return 0;  
}
int printoddnum(int n) 
{
     int i = 0; 
      
       while(i <= n) 
       {
            printoddnum(n + 1);
            if (n % 2 != 0) 
            {
                 return n; 
                 cout<<n<<endl; 
            }
            
            i++;         
       }

}

This post has been edited by izic: 08 October 2010 - 07:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Recursive function that prints odd numbers

#2 Crunch   User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 07:53 PM

Where's your code ?
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 08:02 PM

your returning before printing for one. two this is mixing recursion and loops in a way that results in infinitive calls. i is always 0 and n keeps getting bigger witch with each recursion, so nothing ever gets past the recursive call, that is why it crashes. i would do this by taking a number checking weather or not it was odd by using the same check you used. if it was even then subtract 1 (also check for 0 as it will be even as well and we don't want to print any further). then i would print this number and pass it minus 2.

*check if n is 0, if it is return.
*check if n is even, if so subtract 1.
*print n
*call function again but - 2
*return form function

This post has been edited by ishkabible: 08 October 2010 - 08:07 PM

Was This Post Helpful? 1
  • +
  • -

#4 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 10:52 PM

You're advice is helping so far but I'm still running into a big issue. My loop won't terminate.
int printoddnum(int n) 
{ 
       if(n == 0) 
       return 0; 
       
       if (n % 2 != 0) 
       {
                 cout<<n; 
                 printoddnum(n - 2); 
                 return n; 
       }
       if (n % 2 == 0) 
       {
                 cout<<n; 
                 printoddnum(n - 1); 
                 return n;
       }
}

Was This Post Helpful? 0
  • +
  • -

#5 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:07 PM

Why would it terminate:
  if(n == 0) 
        return 0; 
       

As it is not becoming true by any means.Why??
if (n % 2 != 0)
      {
      cout<<n;
      printoddnum(n - 2);
      return n;
      }

Also you don't have to print the number when it is even and you could decrease n by 1 in each case.

This post has been edited by aaa111: 08 October 2010 - 11:16 PM

Was This Post Helpful? 1
  • +
  • -

#6 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:07 PM

You are finding negative numbers:

Quote

Enter a number
9
97531-1-3-5-7-9-11-13-15-17...


Change

int printoddnum(int n) 
{ 
    if(n == 0) 
        return 0; 
...



to


int printoddnum(int n) 
{ 
    if(n <= 0) 
        return 0; 
...


This post has been edited by n8wxs: 08 October 2010 - 11:09 PM

Was This Post Helpful? 1
  • +
  • -

#7 izic   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:15 PM

Thanks alot everyone. I greatly appreciate your help and I will leave positive feedback. Also, where can I find some really good recursion tutorials/exercises?
Was This Post Helpful? 0
  • +
  • -

#8 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Recursive function that prints odd numbers

Posted 08 October 2010 - 11:22 PM

Here:
http://www.cprogramm...l/lesson16.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1