diff --git a/pset4/5-squares.c b/pset4/5-squares.c new file mode 100644 index 0000000..c92ae81 --- /dev/null +++ b/pset4/5-squares.c @@ -0,0 +1,25 @@ +#include + +// Function prototypes +int dividable(int n); + +int main(void) +{ + // Initialize variables and get input + int n, sum = 0; + scanf("%d", &n); + + for (int i = 1; i < n; i++) { + if (dividable(i)) { + sum += i; + } + } + + printf("%d\n", sum * sum); + + return 0; +} + +int dividable(int n) { + return (n % 3 == 0 && n % 7 == 0) ? 1 : 0; +} diff --git a/pset4/6-graph.c b/pset4/6-graph.c new file mode 100644 index 0000000..478bc7f --- /dev/null +++ b/pset4/6-graph.c @@ -0,0 +1,71 @@ +#include + +// Function prototypes +void print_block(int n, int m); // Print the pattern +// n: columns +// m: rows +void print_row(int n, int m); // Print a line of row, called bu print_block +// n: number of columns m: position of row +// n: number of columns, see print_column +// m: ranging from 0 to 3 +// m == 0: |*****|*****|*****| +// m == 1: | | | | | | | +// m == 2: |--+--|--+--|--+--| +// m == 3: | | | | | | | +void print_column(int n, + int m); // Print a single character, called by print_row +// n: type of char in a single line +// m: type of char in multiple lines +// m == 0: |***** n is defined by the position in the line with particular m +// m == 1: | | +// m == 2: |--+-- +// m == 3: | | + +int main(void) { + // Initialize variables and read input + int row, col; + scanf("%d", &row); + scanf("%d", &col); + + // The magic starts here + print_block(col, row); + + return 0; +} + +void print_block(int n, int m) { + for (int i = 0; i < m * 4; i++) { + print_row(n, i % 4); + } + print_row(n, 0); // The last line +} + +void print_row(int n, int m) { + for (int i = 0; i < n * 6; i++) { + print_column(i % 6, m); + } + print_column(0, m); // The last char + printf("\n"); +} + +void print_column(int n, int m) { + if (n == 0) { + printf("|"); + } else if (n == 1 || n == 2 || n == 4 || n == 5) { + if (m == 0) { + printf("*"); + } else if (m == 2) { + printf("-"); + } else { + printf(" "); + } + } else if (n == 3) { + if (m == 0) { + printf("*"); + } else if (m == 2) { + printf("+"); + } else { + printf("|"); + } + } +}