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

recursive function help (pls)

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**********
********010010********
******0121001210******
****01232100123210****
**012343210012343210**
0123454321001234543210

Obviously, I'm having trouble with the exact specs, but I'm looking for
guidance in the spacing aspect - I'm close with the digits (my code produces
one half of the display)- Thanks

#include <iostream>
#include <string>
using namespace std;

void Fun1 (int);
void Fun2 (int, int);
void Fun3 (int);

int main()
{
cout <<"Enter an integer between 1 and 9 : ";
int Value;
cin >> Value;

int count = 0;
while (Value > count)
{
Fun2 (0, Value);
Fun1 (Value);
Fun2 (0, Value);
cout <<endl;
Fun1 (Value);
// Fun3 (Value);
--Value;
}

return 0;
}
void Fun1 (int n)
{
char c = ' ';
string space = "";
int i = n;
n = n - (i - 1);
while (i <= n)
{
cout<<n;
++i;
}
}
/*
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;
}
*/
void Fun2 (int current, int stopval)
{
cout <<current;
if (current < stopval)
{
Fun2(current + 1, stopval);
cout << current;
}
}
/*void Fun3 (int descend)
{
while (descend >= 0)
{
cout <<descend;
descend--;
}
cout << endl;
}
*/

Jul 22 '05 #1
3 1992
<mi******@sbcglobal.net> wrote...
Hello All, this is my first post.
OK - The goal is to display the following (note: substitute 1' ' for 2'*')
I am not sure I understand the words in the parentheses. So, what
the printout should look like?
by using 3 recursive functions.
Should each recurse in itself directly or through the other two?
0123454321001234543210
**012343210012343210**
****01232100123210****
******0121001210******
********010010********
**********00**********
**********00**********
********010010********
******0121001210******
****01232100123210****
**012343210012343210**
0123454321001234543210

Obviously, I'm having trouble with the exact specs,
Why?
but I'm looking for
guidance in the spacing aspect - I'm close with the digits (my code produces one half of the display)- Thanks
What's "the spacing aspect"?
[...]


Your code contains no comments, your variable names like 'n' or 'i'
and function names like Fun1, Fun2, are of no help either. If you
want advice, rewrite the whole thing but this time _begin_ with
writing comments. Describe what your function is supposed to do.
Only when you're satisfied with the plain English (or whatever is
your language of choice for comments) version, begin writing C++
statements _right_next_ to the comments.

Name variables so that the code can be easily understood, just by
looking at the variables and functions. Name functions so that you
(mainly, after that -- everybody else) could understand what the
function does.

One of my college teachers used to say "A good drawing of the problem
is half of the solution". Well, he taught theoretical mechanics, and
drawings did help. In your case, _pseudo_code_ is of utter importance
before anything. Once you figure out "exact specs", write them down
in the same comments. Then gradually complete the assignment.

Good luck! And don't be afraid of a few do-overs. At some point you
will get used to doing it the right way the first time.

Victor
Jul 22 '05 #2
On Fri, 12 Dec 2003 02:59:47 GMT, <mi******@sbcglobal.net> wrote:
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.
Re.: "..by using...":
The solution to a problem can never be said to be a 'goal'. The goal
is solving the problem. And what the problem is, is not clear.
0123454321001234543210
**012343210012343210**
****01232100123210****
******0121001210******
********010010********
**********00**********
**********00**********
********010010********
******0121001210******
****01232100123210****
**012343210012343210**
0123454321001234543210

Obviously, I'm having trouble with the exact specs, but I'm looking for
guidance in the spacing aspect - I'm close with the digits (my code produces
one half of the display)- Thanks
Again, which spacing aspect? What do you *want* it to do?
#include <iostream>
#include <string>
using namespace std;

void Fun1 (int);
void Fun2 (int, int);
void Fun3 (int);
It's okay to use names like FunX(int) when what the function is or
does is irrelevant to the subject matter. Here your functions ARE the
subject matter, so they should be named in a way that clarifies what
they are meant to do.
int main()
{
cout <<"Enter an integer between 1 and 9 : ";
int Value;
cin >> Value;

int count = 0;
while (Value > count)
{
Fun2 (0, Value);
Fun1 (Value);
Fun2 (0, Value);
Incomprehensible with such names.
cout <<endl;
Fun1 (Value);
// Fun3 (Value);
--Value;
}

return 0;
} void Fun1 (int n)
{
char c = ' ';
string space = "";
If you want to initialize 'space' to a space, you'd want to put a
space between the quotes. If you don't, then you shouldn't call it
'space'. In any case, you never use the variable in the function.
Furthermore, if you use an std::string it usually means that you want
to do something more sophisticated than what you could achieve by
simply declaring a char,

char space = ' ';
int i = n;
n = n - (i - 1);
You mean, n = 1 ?
while (i <= n)
{
cout<<n;
++i;
}
}
/*
string Reverse (int Number)
{
string s = "";
string c;
for(int bit; Number != 0;)
Why would you name an int 'bit' ???!!!
Might as well declare a string and call it 'char'.. ;-)
Maybe 'bit_mask'?

Anyways, I give up. Too hard to follow. Clean it up, and then repost
it; explaining what you want to do, in the first place.
{
bit = Number % 2;
Number = Number / 2;
c = char ('0' + bit);
s = s + c;
}
cout <<s<<endl<<endl;
return s;
}
*/
void Fun2 (int current, int stopval)
{
cout <<current;
if (current < stopval)
{
Fun2(current + 1, stopval);
cout << current;
}
}
/*void Fun3 (int descend)
{
while (descend >= 0)
{
cout <<descend;
descend--;
}
cout << endl;
}
*/


Jul 22 '05 #3
Thanks, see new post !

"Dan W." <da**@raytron-controls.com> wrote in message
news:op********************************@4ax.com...
On Fri, 12 Dec 2003 02:59:47 GMT, <mi******@sbcglobal.net> wrote:
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.


Re.: "..by using...":
The solution to a problem can never be said to be a 'goal'. The goal
is solving the problem. And what the problem is, is not clear.
0123454321001234543210
**012343210012343210**
****01232100123210****
******0121001210******
********010010********
**********00**********
**********00**********
********010010********
******0121001210******
****01232100123210****
**012343210012343210**
0123454321001234543210

Obviously, I'm having trouble with the exact specs, but I'm looking for
guidance in the spacing aspect - I'm close with the digits (my code producesone half of the display)- Thanks


Again, which spacing aspect? What do you *want* it to do?
#include <iostream>
#include <string>
using namespace std;

void Fun1 (int);
void Fun2 (int, int);
void Fun3 (int);


It's okay to use names like FunX(int) when what the function is or
does is irrelevant to the subject matter. Here your functions ARE the
subject matter, so they should be named in a way that clarifies what
they are meant to do.
int main()
{
cout <<"Enter an integer between 1 and 9 : ";
int Value;
cin >> Value;

int count = 0;
while (Value > count)
{
Fun2 (0, Value);
Fun1 (Value);
Fun2 (0, Value);


Incomprehensible with such names.
cout <<endl;
Fun1 (Value);
// Fun3 (Value);
--Value;
}

return 0;
}

void Fun1 (int n)
{
char c = ' ';
string space = "";


If you want to initialize 'space' to a space, you'd want to put a
space between the quotes. If you don't, then you shouldn't call it
'space'. In any case, you never use the variable in the function.
Furthermore, if you use an std::string it usually means that you want
to do something more sophisticated than what you could achieve by
simply declaring a char,

char space = ' ';
int i = n;
n = n - (i - 1);


You mean, n = 1 ?
while (i <= n)
{
cout<<n;
++i;
}
}
/*
string Reverse (int Number)
{
string s = "";
string c;
for(int bit; Number != 0;)


Why would you name an int 'bit' ???!!!
Might as well declare a string and call it 'char'.. ;-)
Maybe 'bit_mask'?

Anyways, I give up. Too hard to follow. Clean it up, and then repost
it; explaining what you want to do, in the first place.
{
bit = Number % 2;
Number = Number / 2;
c = char ('0' + bit);
s = s + c;
}
cout <<s<<endl<<endl;
return s;
}
*/
void Fun2 (int current, int stopval)
{
cout <<current;
if (current < stopval)
{
Fun2(current + 1, stopval);
cout << current;
}
}
/*void Fun3 (int descend)
{
while (descend >= 0)
{
cout <<descend;
descend--;
}
cout << endl;
}
*/

Jul 22 '05 #4

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

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: 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,...
4
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. ...
9
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...
2
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; ...
3
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...
3
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 ...
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.