473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

recursively annoying...

TMS
119 100+
This recursive function is intended to print all odd numbers starting with the number passed to it. It does that but then, it prints out odd numbers starting with 429496295, and decreasing after that until memory is full or something. The only way I knew it was actually doing what I want it to do plus these other numbers is by hitting pause/break immediately after allowing it to run.
How am I accessing these crazy numbers?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned int odd(unsigned int n);
  6.  
  7. int main()
  8. {
  9. odd(20);
  10.  
  11. return 0;    
  12. }
  13. unsigned int odd(unsigned int n)
  14. {
  15.     if(n==0)
  16.     {
  17.  
  18.         return 1;
  19.     } //end if
  20.     if((n%2)==0)
  21.     {
  22.         return odd(n-1);
  23.     } // end if
  24.     else
  25.     {
  26.         cout << n << ", " ;
  27.         return odd(n-2);
  28.     } //end else
  29. }  //end odd()
  30.  
  31.  
Oct 9 '06 #1
1 1626
The problem is that in your odd function you return odd(n-2) for odd numbers. This makes it impossible for the function to meet the stopping condition n==0, because odd number - 2 would always be odd. And the crazy numbers are caused by underflow. Since you are using unsigned int, if you subtract 2 from 1, you will get the largest odd unsiged int 429496295 and so on.

This code will do what you need:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned int odd(unsigned int n);
  6.  
  7. int main()
  8. {
  9. odd(20);
  10.  
  11. return 0;    
  12. }
  13. unsigned int odd(unsigned int n)
  14. {
  15.     if(n==0)
  16.     {
  17.  
  18.         return 1;
  19.     } //end if
  20.     if((n%2)==0)
  21.     {
  22.         return odd(n-1);
  23.     } // end if
  24.     else
  25.     {
  26.         cout << n << ", " ;
  27.         return odd(n-1);
  28.     } //end else
  29. }  //end odd()
  30.  
  31.  
Oct 9 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ruby Tuesday | last post by:
Is there a fast way to read files/directory recursively? Instead of inspecting each file(s)/dir(s), is there a way to know that its a file or a directory from its hidden attribut both for windows...
3
by: Raseliarison nirinA | last post by:
hi all, i found an unanswered question at http://www.faqts.com/knowledge_base/index.phtml/fid/538 with possible response below. i've tried to send it at faqt.python but can't figure out how to...
4
by: Nicolas Fleury | last post by:
Hi everyone, Is there a way to compare recursively two objects (compare their members recursively)? I'm only interested in equality or non-equality (no need for lower-than...). Thx and...
2
by: John | last post by:
Hi all. If my table looks like the following EMPLID NAME BU SAL ELIG_CONFIG1 1001 Tom 10 50000 1002 Sarah 10 49000 1003 John 20 ...
1
by: Antonio Lopez Arredondo | last post by:
hi all !!! I need to copy a folder and its subfolders to another location; which class should I use ? could only find the System.IO.Directory.MOVE but don't know how to COPY. thanks in...
2
by: melo | last post by:
Hello, I've been struggling with a function(s) to recursively set all folders and files to NOT read-only. So, I thought I'd post this message. What I need to do is: given a starting path, I...
1
by: Lanny McDonald | last post by:
I have built a treeview that looks something like: Root .....FolderLevel1a .........FolderLevel2a
3
by: Kamen TOMOV | last post by:
Hi, Is uploading recursively directories to a web server possible with JavaScript? I mean is it possible read a directory recursively and dynamically construct <input type="file"> with value...
11
by: shanmugaster | last post by:
Hi, I have a string array which may contain 5 digit values or NULL or just blank spaces. I should print the value of array as an integer. that is it should print 0 when it encounters NULL and...
9
by: pamela fluente | last post by:
What is the most current (for framework 2.0) and easy way to copy recursively all files from folder "Folder1" to folder "Folder2" ? Is there any simple function in the framework to do that? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.