473,881 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive Function Help (pls) pt 2

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 a sequence of spaces;
recursive function 2 uses input to display ascending sequence of digits;
likewise, recursive function 3 uses input to display descending sequence of
digits.

I have not followed the instructions completely regarding the digits, but I
feel I'm close - I'm looking for suggestions for the spacing issue - I'm
baffled there. The following in the output from my program:
Enter an integer between 1 and 9 : 4
012343210012343 210
01232100123210
0121001210
0101010
1Press any key to continue

It should display
012343210012343 210
01232100123210
0121001210
010010
00
00
010010
0121001210
01232100123210
012343210012343 210
Here's my code:

#include <iostream>
#include <string>
using namespace std;
void Spacing (int);
void Ascending (int, int);
void Descending (int);
// Main can only have the function calls and loops
int main()
{
cout <<"Enter an integer between 1 and 9 : ";
int Value;
cin >> Value;

int count = 0;
while (Value > count)
{
Ascending (0, Value);
Spacing (Value);
Ascending (0, Value);
cout <<endl;
Spacing (Value);
// Descending (Value);
--Value;
}

return 0;
}
// This recursive function takes user input and displays given number of
spaces
void Spacing (int n)
{
char c = ' ';
string space = "";
int i = n;
n = n - (i - 1);
while (i <= n)
{
cout<<n;
++i;
}
}
/* This was an experiment for Function descending
string Reverse (int Number)
{
string s = "";
string c;
for(int bit; Number != 0;)
{
bit = Number % 2;
Number = Number / 2;
c = char ('0' + bit);
s = s + c;
}
cout <<s<<endl<<endl ;
return s;
}
*/
// This recursive function is supposed to only take the user
// input and display each digit from 0 - number -
// It's currently displaying all my digits (I'm working on it)
void Ascending (int current, int stopval)
{
cout <<current;
if (current < stopval)
{
Ascending(curre nt + 1, stopval);
cout << current;
}
}
// Again, this will be the recursive descending fuction
// used only to display input number - 0.
/*void Descending (int descend)
{
while (descend >= 0)
{
cout <<descend;
descend--;
}
cout << endl;
}
*/
Jul 22 '05 #1
2 2895
mi******@sbcglo bal.net wrote:

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 a sequence of spaces;
recursive function 2 uses input to display ascending sequence of digits;
likewise, recursive function 3 uses input to display descending sequence of
digits.


May I suggest you forget the hourglass at the moment and simply concentrate
on the individual pieces. It's much simpler that way.

OK. Your assignment is to write a function (which must be recusive) to output
a given number of spaces.

The goal is: output a given number of spaces.
Q: When is this goal extremely simple to reach?
A: If the number of spaces to output equals 0. Because then the
function has to do nothing.

So lets start writing the function with what we know up to now:

void Spacing( int n )
{
if( n == 0 )
return;

Q: What about other numbers?
A: Those cases are much to hard for us, thus we take a lazy approach:
If the goal is to output n spaces, let us output 1 space and let us
call a function which outputs the remaining n-1 spaces.
Q: But which function should we call?
A: Well, there is function Spacing. This function claims to be able to
output n spaces, thus it must be simple for this function to output
n-1 spaces.

void Spacing( int n )
{
if( n == 0 )
return;

cout << " ";
Spacing( n-1 );
}
Q: That's it?
A: That's it!
See. In the recursive call there is always 1 subtracted from n.
So if we start out with a value of lets say 5 for n, what happens.
5 doesn't equal 0, thus the if is not taken.
1 space gets sent to cout, then function Spacing is called with an
argument of 5 - 1 -> 4. That function does:
send 1 space to cout
call function Spaceing with an Argument of 4 - 1 -> 3. That function does:
send 1 space to cout
call function Spaceing with an Arg. of 3 - 1 -> 2. That function does:
send 1 space to cout
call function Spacing with an Arg. of 2 - 1 -> 1. That function does:
send 1 space to cout
call function Spaceing with an Arg. of 1 - 1 -> 0. That function does:
the first if detects that the passed argument happens to be 0, thus
it simply returns
function returns
function returns
function returns
function returns
function returns

and the program returns to the called which called Spacing( 5 ). Now count
how many spaces have been sent to cout. They are exactly 5. Each invocation
of Spacing, with the exception of the last one, has output exactly 1 space.

The trick is the subtraction. By always subtracting 1, we make the number of
spaces to output smaller and smaller until we finaly hit 0. And this is our
simple case, we know how to deal with that: do nothing.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2


I'm sure you'll be able to extend Karl's tutorial to the other
functions. Each time the function calls itself again, it is not the
same n, I hope you understand; even though it has the same name. If
you have too much trouble, just tell us.

Much nicer code, by the way. :)

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2018
by: | last post by:
Hello All, this is my first post. OK - The goal is to display the following (note: substitute 1' ' for 2'*') by using 3 recursive functions. 0123454321001234543210 **012343210012343210** ****01232100123210**** ******0121001210****** ********010010******** **********00********** **********00**********
4
2436
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2, opt1=opt1, opt2=opt2, opt3=opt3) return if type(arg2) is ListType:
4
9059
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
9
16857
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script type='text/javascript'> function myFunc(lev) { // if (lev) return myFunc(lev-1); var aStack=; nextFunc = arguments.callee;
2
1614
by: Synapse | last post by:
hi everyone..i need help on the code below. i want to convert it to a recursive function. can anyone help me..pls! thanks a lot!:) #include <iostream.h> int main() { int i, j; i=0;
3
6662
by: Babikie | last post by:
Write a program that performs a reverse recursion with following functions. void swop (char,int,int); void reverse (char); void rev(char,int, int); User should enter a string and all character should be reversed. Note! The rev function should be used as the recursive function, accepting the string, plus the first and last position of string. The reverse() passes the first and the last position of the string to the rev() which performs the...
3
3069
by: NatRoger | last post by:
Hi, We are using DB2 V7 (MVS) in our shop, which dont have the luxury of the 'WITH clause' - CTE support to accomplish recursive queries. Here is my problem - Table1 From-ID To-ID A1 M1 - Member A1's id changes to M1 M1 D1 - M1 changes to D1
3
4254
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 reference, which means it is only collected by the mark-and-sweep collector, not by reference counting. Here is some code that demonstrates it: === def outer():
3
2348
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any other choice? For example, def func(self, x, y, A, B, C): #x, y change in recursive call
0
9926
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9552
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7952
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7108
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.