Skip to content

Instantly share code, notes, and snippets.

@wheelercj
Last active August 17, 2023 06:10
Show Gist options
  • Save wheelercj/5267868307d626165fac8c2f2d04adca to your computer and use it in GitHub Desktop.
Save wheelercj/5267868307d626165fac8c2f2d04adca to your computer and use it in GitHub Desktop.
Solutions to "practice: printing shapes with loops" https://wheelercj.github.io/notes/pages/20200522233055.html
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
// *****
for (int i = 0; i < 5; i++)
cout << '*';
cout << "\n\n";
/*
*
*
*
*
*
*/
for (int i = 0; i < 5; i++)
cout << '*' << endl;
cout << endl;
/*
*****
*****
*****
*****
*****
*/
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
cout << '*';
cout << endl;
}
cout << endl;
/*
*
**
***
****
*****
*/
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < i + 1; j++)
cout << '*';
cout << endl;
}
cout << endl;
/*
*****
****
***
**
*
*/
for (int i = 5; i > 0; i--)
{
for (int j = i; j > 0; j--)
cout << '*';
cout << endl;
}
cout << endl;
/*
*
*
*
*
*
*/
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
cout << ' ';
cout << '*' << endl;
}
cout << endl;
/*
*
**
***
****
*****
*/
for (int i = 0; i < 5; i++)
{
for (int j = 4; j > i; j--)
cout << ' ';
for (int j = 0; j <= i; j++)
cout << '*';
cout << endl;
}
cout << endl;
/*
*****
****
***
**
*
*/
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < i; j++)
cout << ' ';
for (int j = 5; j > i; j--)
cout << '*';
cout << endl;
}
cout << endl;
/*
*****
* *
* *
* *
*****
*/
for (int i = 0; i < 5; i++)
{
int k = 2;
if (i == 0 || i == 4)
k = 5;
for (int j = 0; j < k; j++)
{
cout << '*';
if (k == 2 && j == 0)
{
for (int m = 0; m < 3; m++)
cout << ' ';
}
}
cout << endl;
}
cout << endl;
/*
*
**
***
****
*****
****
***
**
*
*/
for (int i = 0; i < 9; i++)
{
for (int j = 0; j <= -abs(i - 4) + 4; j++)
cout << '*';
cout << endl;
}
cout << endl;
/*
*
***
*****
*******
*********
*******
*****
***
*
*/
for (int i = 9; i > 0; i--)
{
for (int j = 0; j < abs(i - 5); j++)
cout << ' ';
for (int j = 0; j < -2 * abs(i - 5) + 9; j++)
cout << '*';
cout << endl;
}
cout << endl;
/*
*
*********
** **
** **
* *
* *
** **
* *
* *
* *
** **
* *
* *
* *
** **
* *
* *
** **
** **
*********
*
*/
int center_x = 30;
int center_y = 20;
int radius = 10;
for (int degrees = 0; degrees < 360; degrees++) {
double radians = degrees / 360.0 * 2 * 3.14159265358979323846;
int x = int(radius * cos(radians)) + center_x;
int y = int(radius * sin(radians)) + center_y;
cout << "\x1b[" << y << ";" << x << "H";
cout << "*";
}
cout << "\n\n\n\n\n\n\n\n\n\n\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment