summaryrefslogtreecommitdiff
path: root/05_squares/squares.c
blob: 9c3eff3cb15f12b7db64536e6913b5c53358d93f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>


int max(int num1, int num2){
  if (num1 > num2) {
    return num1;
  }
  return num2;
}
/* 
 * Determines if coord is in range between
 * offset (INCLUSIVE) and offset + size (EXCLUSIVE)
 */
int isInRange(int coord, int offset, int size) {
  // if coord is in range, return 1
  if (coord >= offset && coord < offset + size) {
    return 1;
  }
  // else, return 0
  return 0;
}

/*
 * Determines if coord is at border of offset or
 * offset + size
 */
int isAtBorder(int coord, int offset, int size) {
  // if coord is equal to offest or offset + size
  if (coord == offset || coord == offset + size) {
    return 1;
  }
  // return 1, else return 0
  return 0;
}

void squares(int size1, int x_offset, int y_offset, int size2) {
  //compute the max of size1 and (x_offset + size2).  Call this w
  int w = max(size1, x_offset + size2);

  //compute the max of size1 and (y_offset + size2).  Call this h
  int h = max(size1, y_offset + size2);

  //count from 0 to h. Call the number you count with y
  for (int y = 0; y < h; y++) {

    //count from 0 to w. Call the number you count with x
    for (int x = 0; x < w; x++) {

      //check if  EITHER
      //    ((x is between x_offset  and x_offset +size2) AND 
      //     y is equal to either y_offset OR y_offset + size2 - 1 )
      //  OR
      //    ((y is between y_offset and y_offset + size2) AND
      //     x is equal to either x_offset OR x_offset + size2 -1)
      // if so, print a *
      if (((isInRange(x, x_offset, size2) == 1) && (isAtBorder(y, y_offset, size2 -1) == 1)) || ((isInRange(y, y_offset, size2) == 1) && (isAtBorder(x, x_offset, size2-1) == 1))) {
        printf("*");
      } else {

      //if not,
      // check if EITHER
      //    x is less than size1 AND (y is either 0 or size1-1)
      // OR
      //    y is less than size1 AND (x is either 0 or size1-1)
      //if so, print a #
      if ((x < size1 && (y == 0 ||y==size1 -1 )) ||(y < size1 &&(x == 0||x == size1 -1) ) ) {
        printf("#");
      }

      //else print a space
      else {printf(" ");
      }
      }
    }
    //when you finish counting x from 0 to w, 
    //print a newline
    printf("\n");
  }

}