473,396 Members | 1,998 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,396 software developers and data experts.

Infinite Recursive call

I recently had an issue with my recursive project in class. Here is the code.

Expand|Select|Wrap|Line Numbers
  1. // Recursion.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. //define recursive function to get values and store in array
  10. void getInts(int myArrayParam[], int indexParam)
  11. {
  12.     //recursive call
  13.     if (indexParam<=2)
  14.     {
  15.         cout<<"Please enter whole number number "<<indexParam+1<<":";
  16.         cin>>myArrayParam[indexParam];
  17.         indexParam++;
  18.         getInts(myArrayParam, indexParam;
  19.     }
  20. }
  21.  
  22. //define recursive function to ouput values in array
  23. void displayInts(int myArrayParam2[],int indexParam2)
  24. {
  25.     if(indexParam2<=2)
  26.     {
  27.         cout<<"Whole number number "<<indexParam2+1<<" is "<<myArrayParam2(indexParam2)<<"."<<endl;
  28.         indexParam2--;
  29.         displayInts(myArrayParam2, indexParam2);
  30.     }
  31. }
  32.  
  33. int main()
  34. {
  35.     //declare a three element int array and a variable to increment/decrement
  36.     int myArray[3];
  37.     int index;
  38.  
  39.     //call recursive function to get values and store in array
  40.     getInts(myArray, index);
  41.  
  42.     //set index to largest element
  43.     index = 2;
  44.  
  45.     //call recursive function to output values in array
  46.     displayInts(myArray, index);
  47.  
  48.     cout<<endl<<endl<<endl;
  49.     cout<<"Press any key to exit ";
  50.     getch()
  51.     return 0;
  52. }
  53.  
Somehow it will not end the recursion making it infinite. How to I end this madness.
Aug 1 '08 #1
4 2096
Banfa
9,065 Expert Mod 8TB
You don't initialise index before calling getInts.

In displayInts your end coondition is wrong if indexParam2 <= 2 at the first call then it will be <= 2 for a very long time, seeming infinite.

And finally you don't have this problem with this code since the posted code does not compile.
Aug 1 '08 #2
JosAH
11,448 Expert 8TB
And finally you don't have this problem with this code since the posted code does not compile.
I like that conclusion ;-)

kind regards,

Jos
Aug 1 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
And the indexParam goes up instead of down.
Aug 1 '08 #4
I C. I'll figure it out. .
Aug 4 '08 #5

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

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
37
by: Protoman | last post by:
Hi!!! Protoman here, I need to write an infinite precision number class b/c I want to compute pi. Give me some sample code. Also, when I run my program and it computes pi, will my computer freeze...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
4
by: Gregory Piñero | last post by:
Hi, Would anyone be able to tell me why my function below is getting stuck in infinite recusion? Maybe I'm just tired and missing something obvious? def...
5
by: Tinku | last post by:
I am sorry for asking this silly question, but i don't understand why it is happening please suggest me ================================= #include <stdio.h> int main() { static int i=1;...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
8
by: Pivot_Tables | last post by:
Hi, I have created a recursive SQL Query in DB2 and it works fine until some point in the tree where the data gets into infinite loop. Below are some sample data from my relationship table. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.