Advertisement
teplomilka

Brick Wall

Jan 26th, 2021 (edited)
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct {
  6.     int width;
  7.     int height;
  8.     bool *bricks;
  9. } wall_t;
  10.  
  11. void build_wall(wall_t *wall);
  12. void print_wall(wall_t *wall);
  13.  
  14. int main() {
  15.     wall_t wall;
  16.     if (scanf("%d %d", &wall.width, &wall.height) != 2) {
  17.         fprintf(stderr, "Error: Wrong input!");
  18.         return 100;
  19.     }
  20.    
  21.     build_wall(&wall);
  22.     print_wall(&wall);
  23.  
  24.     free(wall.bricks);
  25.     return 0;
  26. }
  27.  
  28. void build_wall(wall_t *wall) {
  29.     int pixels = wall->width * wall->height;
  30.     wall->bricks = malloc(pixels * sizeof(bool));
  31.  
  32.     int w = wall->width;
  33.     for (int r = 0; r < wall->height; r++) {
  34.         for (int c = 0; c < w; c++) {
  35.             if (r%3 == 0 || r%3 == 1) {
  36.                 if (r%6 == 0 || r%6 == 1) {
  37.                     if (c%5 == 4) {
  38.                         wall->bricks[r*w + c] = false;
  39.                     }
  40.                     else {
  41.                         wall->bricks[r*w + c] = true;
  42.                     }
  43.                 }
  44.                 else if (r%6 == 3 || r%6 == 4) {
  45.                     if (c%5 == 2) {
  46.                         wall->bricks[r*w + c] = false;
  47.                     }
  48.                     else {
  49.                         wall->bricks[r*w + c] = true;
  50.                     }
  51.                 }
  52.             }
  53.             else if (r%3 == 2) {
  54.                 wall->bricks[r*w + c] = false;
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. void print_wall(wall_t *wall) {
  61.     int brick = wall->width * wall->height;
  62.     int w = wall->width;
  63.  
  64.     for (int b = 0; b < brick; b++) {
  65.         if (wall->bricks[b] == true) printf("##");
  66.         else printf("  ");
  67.  
  68.         if (b%w == w-1) printf("\n");
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement