Skip to content

Instantly share code, notes, and snippets.

@sdwvit
Last active March 19, 2022 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdwvit/94c027179da8545258e9c06ba6e6261e to your computer and use it in GitHub Desktop.
Save sdwvit/94c027179da8545258e9c06ba6e6261e to your computer and use it in GitHub Desktop.
mario
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
int getHeight(void) {
int height;
do {
height = get_int("Height: ");
} while (
height <= 0 || height > 8
);
return height;
}
int getLowerBound(int height, int gap_width, int j) {
return height - j - 1;
}
int getUpperBound(int height, int gap_width, int j) {
return height + j + gap_width;
}
int isGap(int height, int gap_width, int i) {
return i > height - 1 && i < height + gap_width;
}
int main(void)
{
int height = getHeight();
int gap_width = 2;
int line_len;
for (int j = 0; j < height; j++) {
line_len = getUpperBound(height, gap_width, j) + 1;
char line[line_len + 1];
for (int i = 0; i < line_len; i++) {
line[i] = ' ';
if (!(i >= getLowerBound(height, gap_width, j) && i <= getUpperBound(height, gap_width, j))) {
continue;
}
if (isGap(height, gap_width, i)) {
continue;
}
line[i] = '#';
}
line[line_len] = '\0'; // terminate char array: wtf c, that's why noone uses you
printf("%s\n", line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment