From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 05_squares/README | 48 +++++ 05_squares/ans_3_5_8_2.txt | 10 ++ 05_squares/ans_5_2_4_6.txt | 10 ++ 05_squares/ans_9_2_3_4.txt | 9 + 05_squares/grade.txt | 437 +++++++++++++++++++++++++++++++++++++++++++++ 05_squares/squares.c | 82 +++++++++ 6 files changed, 596 insertions(+) create mode 100644 05_squares/README create mode 100644 05_squares/ans_3_5_8_2.txt create mode 100644 05_squares/ans_5_2_4_6.txt create mode 100644 05_squares/ans_9_2_3_4.txt create mode 100644 05_squares/grade.txt create mode 100644 05_squares/squares.c (limited to '05_squares') diff --git a/05_squares/README b/05_squares/README new file mode 100644 index 0000000..b239759 --- /dev/null +++ b/05_squares/README @@ -0,0 +1,48 @@ + 1. Open the file "squares.c" and look at the squares function. + You will find that I have already done steps 1--4 + for an algorithm which draws two (possibly overlapping) + squares (one of #s and one of *s). + + 2. Read the generalized steps that I have written as comments + in this file. Note that whenever I indicate a range + (count from x to y, or between x and y), the range is + inclusive of the lower bound, and exclusive of the upper bound. + All counting is "count up by one." Determine if there are + any parts of these steps that you will want to abstract + out into a separate function. + + 3. Implement this algorithm by translating my steps into code. + You may abstract any pieces you want out into separate functions. + We've provided two helpers with specification we think may be helpful. + + 4. We have provided squares_test.o, which has a main function + that takes four command line arguments (size1, x_offset, y_offset, size2), + and calls your squares function with those arguments. + You can compile your code and link it with this object file + to make a program: + gcc -o squares -Wall -Werror -std=gnu99 --pedantic squares.c squares_test.o + Then you can run it like this: + ./square 4 1 2 3 + which would call your squares function with + size1=4 + x_offset=1 + y_offset=2 + size2=3 + + which should produce output that looks like this: +#### +# # +#*** +#*#* + *** +We have also provided 3 files which show the correct output for three inputs +(./squares 3 5 8 2,./squares 5 2 4 6,./squares 9 2 3 4) in the files +whose names starts with ans_ (and then has the parameter values in its name, +separated by _s). + +Use "diff" like you just learned to compare your program's output +to the correct output. + + 5. Submit your modified squares.c file (git commit/git push/grade). + + diff --git a/05_squares/ans_3_5_8_2.txt b/05_squares/ans_3_5_8_2.txt new file mode 100644 index 0000000..82f65f6 --- /dev/null +++ b/05_squares/ans_3_5_8_2.txt @@ -0,0 +1,10 @@ +### +# # +### + + + + + + ** + ** diff --git a/05_squares/ans_5_2_4_6.txt b/05_squares/ans_5_2_4_6.txt new file mode 100644 index 0000000..8585a26 --- /dev/null +++ b/05_squares/ans_5_2_4_6.txt @@ -0,0 +1,10 @@ +##### +# # +# # +# # +##****** + * * + * * + * * + * * + ****** diff --git a/05_squares/ans_9_2_3_4.txt b/05_squares/ans_9_2_3_4.txt new file mode 100644 index 0000000..26cc452 --- /dev/null +++ b/05_squares/ans_9_2_3_4.txt @@ -0,0 +1,9 @@ +######### +# # +# # +# **** # +# * * # +# * * # +# **** # +# # +######### diff --git a/05_squares/grade.txt b/05_squares/grade.txt new file mode 100644 index 0000000..18c9364 --- /dev/null +++ b/05_squares/grade.txt @@ -0,0 +1,437 @@ +Grading at Sun 28 Nov 2021 08:44:52 PM UTC +Trying to compile your code and link with squares_test.o +Testing ./squares 1 0 0 1 +PASSED + - Correct +Testing ./squares 1 0 0 5 +PASSED + - Correct +Testing ./squares 1 0 0 9 +PASSED + - Correct +Testing ./squares 1 0 1 1 +PASSED + - Correct +Testing ./squares 1 0 1 5 +PASSED + - Correct +Testing ./squares 1 0 1 9 +PASSED + - Correct +Testing ./squares 1 0 3 1 +PASSED + - Correct +Testing ./squares 1 0 3 5 +PASSED + - Correct +Testing ./squares 1 0 3 9 +PASSED + - Correct +Testing ./squares 1 0 8 1 +PASSED + - Correct +Testing ./squares 1 0 8 5 +PASSED + - Correct +Testing ./squares 1 0 8 9 +PASSED + - Correct +Testing ./squares 1 4 0 1 +PASSED + - Correct +Testing ./squares 1 4 0 5 +PASSED + - Correct +Testing ./squares 1 4 0 9 +PASSED + - Correct +Testing ./squares 1 4 1 1 +PASSED + - Correct +Testing ./squares 1 4 1 5 +PASSED + - Correct +Testing ./squares 1 4 1 9 +PASSED + - Correct +Testing ./squares 1 4 3 1 +PASSED + - Correct +Testing ./squares 1 4 3 5 +PASSED + - Correct +Testing ./squares 1 4 3 9 +PASSED + - Correct +Testing ./squares 1 4 8 1 +PASSED + - Correct +Testing ./squares 1 4 8 5 +PASSED + - Correct +Testing ./squares 1 4 8 9 +PASSED + - Correct +Testing ./squares 1 7 0 1 +PASSED + - Correct +Testing ./squares 1 7 0 5 +PASSED + - Correct +Testing ./squares 1 7 0 9 +PASSED + - Correct +Testing ./squares 1 7 1 1 +PASSED + - Correct +Testing ./squares 1 7 1 5 +PASSED + - Correct +Testing ./squares 1 7 1 9 +PASSED + - Correct +Testing ./squares 1 7 3 1 +PASSED + - Correct +Testing ./squares 1 7 3 5 +PASSED + - Correct +Testing ./squares 1 7 3 9 +PASSED + - Correct +Testing ./squares 1 7 8 1 +PASSED + - Correct +Testing ./squares 1 7 8 5 +PASSED + - Correct +Testing ./squares 1 7 8 9 +PASSED + - Correct +Testing ./squares 4 0 0 1 +PASSED + - Correct +Testing ./squares 4 0 0 5 +PASSED + - Correct +Testing ./squares 4 0 0 9 +PASSED + - Correct +Testing ./squares 4 0 1 1 +PASSED + - Correct +Testing ./squares 4 0 1 5 +PASSED + - Correct +Testing ./squares 4 0 1 9 +PASSED + - Correct +Testing ./squares 4 0 3 1 +PASSED + - Correct +Testing ./squares 4 0 3 5 +PASSED + - Correct +Testing ./squares 4 0 3 9 +PASSED + - Correct +Testing ./squares 4 0 8 1 +PASSED + - Correct +Testing ./squares 4 0 8 5 +PASSED + - Correct +Testing ./squares 4 0 8 9 +PASSED + - Correct +Testing ./squares 4 4 0 1 +PASSED + - Correct +Testing ./squares 4 4 0 5 +PASSED + - Correct +Testing ./squares 4 4 0 9 +PASSED + - Correct +Testing ./squares 4 4 1 1 +PASSED + - Correct +Testing ./squares 4 4 1 5 +PASSED + - Correct +Testing ./squares 4 4 1 9 +PASSED + - Correct +Testing ./squares 4 4 3 1 +PASSED + - Correct +Testing ./squares 4 4 3 5 +PASSED + - Correct +Testing ./squares 4 4 3 9 +PASSED + - Correct +Testing ./squares 4 4 8 1 +PASSED + - Correct +Testing ./squares 4 4 8 5 +PASSED + - Correct +Testing ./squares 4 4 8 9 +PASSED + - Correct +Testing ./squares 4 7 0 1 +PASSED + - Correct +Testing ./squares 4 7 0 5 +PASSED + - Correct +Testing ./squares 4 7 0 9 +PASSED + - Correct +Testing ./squares 4 7 1 1 +PASSED + - Correct +Testing ./squares 4 7 1 5 +PASSED + - Correct +Testing ./squares 4 7 1 9 +PASSED + - Correct +Testing ./squares 4 7 3 1 +PASSED + - Correct +Testing ./squares 4 7 3 5 +PASSED + - Correct +Testing ./squares 4 7 3 9 +PASSED + - Correct +Testing ./squares 4 7 8 1 +PASSED + - Correct +Testing ./squares 4 7 8 5 +PASSED + - Correct +Testing ./squares 4 7 8 9 +PASSED + - Correct +Testing ./squares 8 0 0 1 +PASSED + - Correct +Testing ./squares 8 0 0 5 +PASSED + - Correct +Testing ./squares 8 0 0 9 +PASSED + - Correct +Testing ./squares 8 0 1 1 +PASSED + - Correct +Testing ./squares 8 0 1 5 +PASSED + - Correct +Testing ./squares 8 0 1 9 +PASSED + - Correct +Testing ./squares 8 0 3 1 +PASSED + - Correct +Testing ./squares 8 0 3 5 +PASSED + - Correct +Testing ./squares 8 0 3 9 +PASSED + - Correct +Testing ./squares 8 0 8 1 +PASSED + - Correct +Testing ./squares 8 0 8 5 +PASSED + - Correct +Testing ./squares 8 0 8 9 +PASSED + - Correct +Testing ./squares 8 4 0 1 +PASSED + - Correct +Testing ./squares 8 4 0 5 +PASSED + - Correct +Testing ./squares 8 4 0 9 +PASSED + - Correct +Testing ./squares 8 4 1 1 +PASSED + - Correct +Testing ./squares 8 4 1 5 +PASSED + - Correct +Testing ./squares 8 4 1 9 +PASSED + - Correct +Testing ./squares 8 4 3 1 +PASSED + - Correct +Testing ./squares 8 4 3 5 +PASSED + - Correct +Testing ./squares 8 4 3 9 +PASSED + - Correct +Testing ./squares 8 4 8 1 +PASSED + - Correct +Testing ./squares 8 4 8 5 +PASSED + - Correct +Testing ./squares 8 4 8 9 +PASSED + - Correct +Testing ./squares 8 7 0 1 +PASSED + - Correct +Testing ./squares 8 7 0 5 +PASSED + - Correct +Testing ./squares 8 7 0 9 +PASSED + - Correct +Testing ./squares 8 7 1 1 +PASSED + - Correct +Testing ./squares 8 7 1 5 +PASSED + - Correct +Testing ./squares 8 7 1 9 +PASSED + - Correct +Testing ./squares 8 7 3 1 +PASSED + - Correct +Testing ./squares 8 7 3 5 +PASSED + - Correct +Testing ./squares 8 7 3 9 +PASSED + - Correct +Testing ./squares 8 7 8 1 +PASSED + - Correct +Testing ./squares 8 7 8 5 +PASSED + - Correct +Testing ./squares 8 7 8 9 +PASSED + - Correct +Testing ./squares 11 0 0 1 +PASSED + - Correct +Testing ./squares 11 0 0 5 +PASSED + - Correct +Testing ./squares 11 0 0 9 +PASSED + - Correct +Testing ./squares 11 0 1 1 +PASSED + - Correct +Testing ./squares 11 0 1 5 +PASSED + - Correct +Testing ./squares 11 0 1 9 +PASSED + - Correct +Testing ./squares 11 0 3 1 +PASSED + - Correct +Testing ./squares 11 0 3 5 +PASSED + - Correct +Testing ./squares 11 0 3 9 +PASSED + - Correct +Testing ./squares 11 0 8 1 +PASSED + - Correct +Testing ./squares 11 0 8 5 +PASSED + - Correct +Testing ./squares 11 0 8 9 +PASSED + - Correct +Testing ./squares 11 4 0 1 +PASSED + - Correct +Testing ./squares 11 4 0 5 +PASSED + - Correct +Testing ./squares 11 4 0 9 +PASSED + - Correct +Testing ./squares 11 4 1 1 +PASSED + - Correct +Testing ./squares 11 4 1 5 +PASSED + - Correct +Testing ./squares 11 4 1 9 +PASSED + - Correct +Testing ./squares 11 4 3 1 +PASSED + - Correct +Testing ./squares 11 4 3 5 +PASSED + - Correct +Testing ./squares 11 4 3 9 +PASSED + - Correct +Testing ./squares 11 4 8 1 +PASSED + - Correct +Testing ./squares 11 4 8 5 +PASSED + - Correct +Testing ./squares 11 4 8 9 +PASSED + - Correct +Testing ./squares 11 7 0 1 +PASSED + - Correct +Testing ./squares 11 7 0 5 +PASSED + - Correct +Testing ./squares 11 7 0 9 +PASSED + - Correct +Testing ./squares 11 7 1 1 +PASSED + - Correct +Testing ./squares 11 7 1 5 +PASSED + - Correct +Testing ./squares 11 7 1 9 +PASSED + - Correct +Testing ./squares 11 7 3 1 +PASSED + - Correct +Testing ./squares 11 7 3 5 +PASSED + - Correct +Testing ./squares 11 7 3 9 +PASSED + - Correct +Testing ./squares 11 7 8 1 +PASSED + - Correct +Testing ./squares 11 7 8 5 +PASSED + - Correct +Testing ./squares 11 7 8 9 +PASSED + - Correct +You got all cases right + +Overall Grade: PASSED diff --git a/05_squares/squares.c b/05_squares/squares.c new file mode 100644 index 0000000..9c3eff3 --- /dev/null +++ b/05_squares/squares.c @@ -0,0 +1,82 @@ +#include +#include + + +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"); + } + +} + -- cgit v1.2.3