G
Guest
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
012343210012343210
01232100123210
0121001210
0101010
1Press any key to continue
It should display
012343210012343210
01232100123210
0121001210
010010
00
00
010010
0121001210
01232100123210
012343210012343210
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(current + 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;
}
*/
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
012343210012343210
01232100123210
0121001210
0101010
1Press any key to continue
It should display
012343210012343210
01232100123210
0121001210
010010
00
00
010010
0121001210
01232100123210
012343210012343210
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(current + 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;
}
*/