Recursive func help (pls)

Given any int (1-9), my program should display the following: 2
0121001210
010010
00
00
010010
0121001210

There are to be recursive funcs - all using same int & NO loops: 1. asceding seq, 012 2. descending seq, 210 3. spaces

Main can only call funcs and have new lines, but can have loops.

I'm stuck - any suggestions to any part will be welcomed. Here
[code]
#include
#include
using namespace std;

void Fun1 (int);
void Fun2 (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 (Value);
Fun3 (Value);
Fun1 (Value);
--Value;
}

return 0;
}
void Fun1 (int x)
{
char space = ' ';
int count = 1;
while (count <= x)
{
cout << space;
++count;
}
}
void Fun2 (int ascend)
{
if (ascend == 0)
cout <<"00";
else
count = 0;
}
void Fun3 (int descend)
{
while (descend >= 0)
{
cout <<descend;
descend--;
}
cout << endl;
}
[code]

Comments

  • [b][red]This message was edited by tom_sw at 2003-11-30 23:10:8[/red][/b][hr]
    : Given any int (1-9), my program should display the following: 2
    : 0121001210
    : 010010
    : 00
    : 00
    : 010010
    : 0121001210
    :
    : There are to be recursive funcs - all using same int & NO loops: 1. asceding seq, 012 2. descending seq, 210 3. spaces
    :
    : Main can only call funcs and have new lines, but can have loops.
    :
    : I'm stuck - any suggestions to any part will be welcomed. Here
    : [code]
    : #include
    : #include
    : using namespace std;
    :
    : void Fun1 (int);
    : void Fun2 (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 (Value);
    : Fun3 (Value);
    : Fun1 (Value);
    : --Value;
    : }
    :
    : return 0;
    : }
    : void Fun1 (int x)
    : {
    : char space = ' ';
    : int count = 1;
    : while (count <= x)
    : {
    : cout << space;
    : ++count;
    : }
    : }
    : void Fun2 (int ascend)
    : {
    : if (ascend == 0)
    : cout <<"00";
    : else
    : count = 0;
    : }
    : void Fun3 (int descend)
    : {
    : while (descend >= 0)
    : {
    : cout <<descend;
    : descend--;
    : }
    : cout << endl;
    : }
    : [/code]
    :
    :
    How about something like this. It uses 2 values, the current in the ascending sequence and the final value.
    the section in main would look like:

    cout <<"Enter an integer between 1 and 9 : ";
    int Value;
    cin >> Value;
    pattern(0, Value);

    [code]void pattern(int current, int stopval)
    {
    cout << current; // print the current value
    if (current < stopval) // if at the end, do no more
    {
    pattern(current + 1, stopval); // call for next digit in sequence
    cout << current; // output current value (descending side)
    }
    }[/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion