void triangle(ostream &outs, unsigned int m, unsigned int n)
{
if ( m <= n)
{
int NumberOfLines = (2 * (( n - m) + 1));
int Mid = (NumberOfLines / 2);
for (int i = 0; i < NumberOfLines; i++)
{
if (i < Mid)
{
for (int j = i; j < n; j++)
outs << "*";
}
else
{
for (int j = 0; j < (m + (i - Mid)); j++)
outs << "*";
}
outs << endl;
}
}
else
outs << "Value m cannot be greater than n." << endl;
}

it comes out opposite of what i want it to do.
it looks like this.
****
***
**
**
***
****
but I want it to look like this.
**
***
****
****
***
**

Recommended Answers

All 3 Replies

but I want it to look like this.
**
***
****
****
***
**

1) Make a function called : printStar(int num);

Where it prints num of stars :

For example a call to printStar(4) would produce :
****

or a call to printStar(2) would produce : ***

After you make that function you can make your loop start at
2 end at 4 then decrease to 2.

So

for(int i = 2; i <= 4; i++)
         printStars(i); //prints i amount of stars with a newline
for(int j = 4; j >= 2; --j)
     printStars(j); //prints j amount of starts with a newline

Then thats it!

I actually figured it out. But I need to use recurrsion, and it's not coming to me. Unless what I have is considered recursion, but I need to call back triangle.

void triangle(ostream &outs, unsigned int m, unsigned int n){
     if (m <= n){
         for (int i = m; i <= n; i++){
             for (int j = 1; j <= i; j++)
                 cout << '*';
                 cout << endl;
                 }
                 }
         for (int i = n; i >= m; i--){
             for (int j = 1; j <= i; j++)
                 cout << '*';
                 cout << endl;
                 }
}

Tthats not recursion.

Heres a small example, counting down from a number to 1.

void recur(int x){
if(x == 0) return;
cout<<x;
void recur(x-1);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.