#include <cstdlib>
#include <iostream>
usingnamespace std;
void starPut(int, int);
int main(int argc, char *argv[])
{
starPut(4, 7);
system("PAUSE");
return EXIT_SUCCESS;
}
void starPut(int a, int b)
{
int i;
while (a < b) { //... until it equals b
if (i = 1, i <= a, i++)
cout << "*"; //puts one star per value until int a is reached
else
cout << endl; //goes to the next line
starPut((a + 1), b); //adds one to a and repeat... (see while declaration)
}
}
What I want is something that looks like this (using the values I had given starPut in main):
1 2 3 4
****
*****
******
*******
But I instead get infinitely repeating asterisks. So! Any help?
void starPut(int a, int b)
{
if(a < ((2 * b) - 1))
{
constint c = ((a >= b) ? (((2 * b) - a) - 1) : (a + 1));
for(int i = 0; i < c; ++i)
{
cout << "*";
}
cout << endl;
starPut(a + 1, b);
}
}
if you starPut(4, 7); it starts at the 5. line. This starPut(0, 7); would start at the first line
The if...else statement was originally a for loop. I didn't change all I needed to. Plus, it looks like I was right the first time. Ah well. I didn't realize it was going to be this complicated; I usually overthink these things.
I actually has starPut accept two integers like that on purpose in case somebody had a burning urge to start it on a different row (the original assignment was to make a whole half diamond, though this isn't a formal homework assignment, so I would put my own twists on their exercises).
void stars(int count=1)
{
//print count stars;
if (count<5) stars(count+1);
//add something here,
//so that the five star line
//is printed only once
//print count stars;
}
int main()
{
stars();
return 0;
}
Sorry, I totally misread the original post...
I thought you wanted the half diamond and ended up with the other thing,
but it turns out you actually wanted that other thing...
Think about it for a while, how would you do this if you were to do it with loops?
It would be something like this, right?
#include <iostream>
usingnamespace std;
void loop(int a, int b);
void rec_func_wrapper(int a, int b);
void actual_rec_func(int a, int b, int i);
int main()
{
loop(2,5);
rec_func_wrapper(2,5);
cout << "\nhit enter to quit...\n";
cin.get();
return 0;
}
void loop(int a, int b)
{
for (int i=a; i<=b; i++)
cout << i << ' ';
cout << endl;
}
void rec_func_wrapper(int a, int b)
{
actual_rec_func(a,b,a);
}
void actual_rec_func(int a, int b, int i)
{
if (i>b) return;
cout << i << ' ';
actual_rec_func(a,b,i+1);
if (i==a) cout << endl;
}