0

So this teacher assigned us to write 2 code that one of them prints out a triangle-like pattern, with character '*'!And he told us to do so, using the loop for.I managed to do it with the following code:

#include <iostream>
using namespace std;


int main()
{
int i,j;
for(i=1;i<11;i++)
{
    cout<<endl;
    for(j=1;i>j;j++)
    {
        cout<<'*';
    }
}
for(i=10;i>0;i--)
{
    cout<<endl;
    for(j=1;i>j;j++)
    {
        cout<<'*';
    }
}
cin.get();
return 0;   
}

And it ran flawless. But my mind got frozen when I saw the second one! He's said to write a code that prints out a pattern like this:

   *
  ***
 *****
*******
 *****
  ***
   *

I have no idea how to even start writing it! Can someone help?

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
Javad Arjmandi
  • 331
  • 1
  • 3
  • 12
  • 2
    You have a program that prints the right-hand half of what you want. How would you modify it to print the left-hand half? Now, how would you stick them together? – zwol Nov 01 '13 at 19:58
  • http://stackoverflow.com/questions/10456654/print-star-diamond-in-c-with-nested-loops http://stackoverflow.com/questions/2094366/c-printing-ascii-heart-and-diamonds-with-platform-independent http://stackoverflow.com/questions/8598345/print-a-diamond-shape-with-a-borderusing-asterisks-in-c-c http://stackoverflow.com/questions/1541487/print-an-ascii-art-diamond etc etc... Also @Kunal, be nice, please. – indiv Nov 01 '13 at 19:59
  • I always solve this kind of problem like this: `for (auto *p = " *\n ***\n *****\n*******\n *****\n ***\n *\n"; *p; std::cout << *p++);` – ecatmur Nov 01 '13 at 20:00
  • @Zack Many thanks! Learning someone how to fish huh? xD@indiv sorry about that, I am an absolute beginner! takes some time to get used to! – Javad Arjmandi Nov 01 '13 at 20:04
  • What is homework for, if not learning how to fish? ;-) This isn't a good question *for this site*, but do not feel ashamed to have asked it. We all were new at this once. – zwol Nov 02 '13 at 02:08

1 Answers1

-1
    #include <iostream>
using namespace std;


int main()
{
int i,j;
for(i=1;i<11;i++)
{
    cout<<endl;
    for (j=11; i<j; j--)
    {
        cout<< ' ';
    }
    for(j=1;i>j;j++)
    {
        cout<<'*';
    }
for(j=1;i>j;j++)
    {
        cout<<'*';
    }
}
for(i=10;i>0;i--)
{
    cout<<endl;
    for (j=11;i<j; j--)
    {
        cout<< ' ';
    }
    for(j=1;i>j;j++)
    {
        cout<<'*';
    }
    for(j=1;i>j;j++)
    {
        cout<<'*';
    }

}
cin.get();
return 0;
}