Looping C++

SOLVED THANKS
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   const int arm = 4;        // change back to 2 when desired
   int diam = 2 * arm + 1;
   vector<string> star( diam, string( diam, ' ' ) + '\n' );
   for ( int i = 0; i < diam; i++ ) star[i][i] = star[i][arm] = star[i][diam-1-i] = star[arm][i] = '*';
   for ( string s : star ) cout << s;
}


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



If you don't like std::string or std::vector ...
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
   const int arm = 4;        // change back to 2 when desired
   int diam = 2 * arm + 1;
   for ( int i = 0; i < diam; i++ )
   {
      for ( int j = 0; j < diam; j++ ) cout << ( j == i || j == diam - 1 - i || i == arm || j == arm ? '*' : ' ' );
      cout << '\n';
   }
}
Last edited on
thank you so much @lastchance, I really appreciate that
Topic archived. No new replies allowed.