Skip to content

Instantly share code, notes, and snippets.

@kylebgorman
Created June 10, 2012 23:08
Show Gist options
  • Save kylebgorman/2907636 to your computer and use it in GitHub Desktop.
Save kylebgorman/2907636 to your computer and use it in GitHub Desktop.
Recursive martini class
// A cool way to draw a martini glass; this was on the final of CS 115 at U of I
#include <stdio.h>
void drawcup(int N, int offset) {
// base case
if (offset > N)
return;
int i;
for (i = 0; i < offset; i ++)
printf(" ");
for (/* i = offset */; i < N; i++)
printf("*");
printf("\n");
// recursive case
drawcup(N - 1, offset + 1);
}
void draw(int N) {
// "cup"
drawcup(N, 0);
// "stem"
int i, j;
int halfset = N / 2; // truncated
for (j = 0; j < halfset; j++) {
for (i = 0; i < halfset; i++)
printf(" ");
printf("*\n");
}
// "base"
for (i = 0; i < N; i++)
printf("*");
printf("\n");
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: martini N\n");
return 1;
}
int N = atoi(argv[1]);
if (N % 2 != 1) {
fprintf(stderr, "N must be odd\n");
return 2;
}
// git 'er dun
draw(N);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment