By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,232 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

recursive function help (pls)

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
<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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.