Skip to content

Instantly share code, notes, and snippets.

@Demonstrandum
Last active June 20, 2017 21:06
Show Gist options
  • Save Demonstrandum/f21e3d44b3ccd091997a23504367e2a9 to your computer and use it in GitHub Desktop.
Save Demonstrandum/f21e3d44b3ccd091997a23504367e2a9 to your computer and use it in GitHub Desktop.
Prints a cross of stars (`*`)
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void starCross(int n) {
float half = (float)n * 0.5;
for (int i = 1; i <= floor(half); i++) {
for (int j = 1; j < i; j++) {
cout << " ";
}
cout << "*";
for (int j = 1; j < (n + 1) - 2*i; j++) {
cout << " ";
}
cout << "*" << endl;
}
if (n % 2 == 1) {
for (int i = 0; i < floor(half); i++) {
cout << " ";
}
cout << "*" << endl;
}
for (int i = floor(half); i >= 1; i--) {
for (int j = 1; j < i; j++) {
cout << " ";
}
cout << "*";
for (int j = 1; j < (n + 1) - 2*i; j++) {
cout << " ";
}
cout << "*\n";
}
}
int main(int argc, char** argv) {
if (argc < 2) {
cout << "Please supply a size for the cross." << endl;
return 1;
}
int size = stoi(argv[1]);
starCross(size);
return 0;
}
@Demonstrandum
Copy link
Author

Sample output:

$ > git clone https://gist.github.com/f21e3d44b3ccd091997a23504367e2a9.git # clone the gist
$ > cd f21e* # enter the gist dir

$ > g++ -g -Wall -O3 -o cross cross.cpp # compile it
$ > ./cross 9 # run it, odd numbers look best
*       *         #1
 *     *          #2
  *   *           #3
   * *            #4
    *             #5
   * *            #6
  *   *           #7
 *     *          #8
*       *         #9

$ > ./cross 12 # with an even number
*          *
 *        *
  *      *
   *    *
    *  *
     **
     **
    *  *
   *    *
  *      *
 *        *
*          *       #...12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment