├── 00_hello ├── grade.txt └── hello.txt ├── 01_apple ├── fruit.txt └── grade.txt ├── 02_code1 ├── code1.c ├── grade.txt └── test.sh ├── 03_code2 ├── README ├── code2.c ├── grade.txt └── test.sh ├── 04_compile ├── README ├── a.out ├── grade.txt └── hello.c ├── 05_squares ├── README ├── ans_3_5_8_2.txt ├── ans_5_2_4_6.txt ├── ans_9_2_3_4.txt ├── grade.txt ├── squares.c └── squares_test.o ├── 06_rect ├── Makefile ├── README ├── grade.txt ├── rectangle ├── rectangle.c └── rectangle_ans.txt ├── 07_retirement ├── Makefile ├── README ├── grade.txt ├── retirement ├── retirement.c └── retirement_ans.txt ├── 08_testing ├── README ├── grade.txt ├── input.1 ├── input.2 ├── input.3 ├── input.4 ├── isPrime-broken1 ├── isPrime-broken2 ├── isPrime-broken3 ├── isPrime-broken4 └── isPrime-correct ├── 09_testing2 ├── README ├── grade.txt ├── run_all.sh └── tests.txt ├── 10_gdb ├── README ├── game ├── game.c ├── grade.txt └── input.txt ├── 11_read_ptr1 ├── Makefile ├── README ├── answer.txt ├── grade.txt └── test.c ├── 12_read_ptr2 ├── Makefile ├── README ├── answer.txt ├── grade.txt └── test.c ├── 13_read_arr1 ├── Makefile ├── README ├── answer.txt ├── grade.txt └── test.c ├── 14_array_max ├── Makefile ├── README ├── arrayMax.c └── grade.txt ├── 15_tests_subseq ├── README ├── grade.txt ├── next-README ├── run_all.sh └── test-subseq.c ├── 16_subseq ├── README ├── grade.txt └── maxSeq.c ├── 17_read_arr2 ├── Makefile ├── README ├── answer.txt ├── grade.txt └── test.c ├── 18_reverse_str ├── Makefile ├── README ├── grade.txt ├── reverse.c └── reverse_ans.txt ├── 19_bits_arr ├── Makefile ├── README ├── bits_ans.txt ├── grade.txt └── numToBits.c ├── 20_rot_matrix ├── Makefile ├── README ├── grade.txt ├── read-matrix.o ├── rotate.c ├── sample.out └── sample.txt ├── 21_read_rec1 ├── README ├── answer.txt ├── grade.txt └── test.c ├── 22_tests_power ├── README ├── grade.txt ├── next-README ├── run_all.sh └── test-power.c ├── 23_power_rec ├── README ├── grade.txt └── power.c ├── 24_read_arr3 ├── README ├── answer.txt ├── grade.txt └── test.c ├── 25_break_encr ├── Makefile ├── README ├── breaker.c └── grade.txt ├── 26_tests_matrix_input ├── README ├── grade.txt ├── input.txt ├── input1.txt ├── input2.txt ├── input3.txt ├── input4.txt ├── input5.txt ├── next-README ├── run_all.sh └── tests.txt ├── 27_matrix_input ├── README ├── grade.txt ├── rotateMatrix.c ├── sample.out └── sample.txt ├── 28_fix_vg_encr ├── Makefile ├── README ├── encrypt.c ├── grade.txt ├── input.txt └── input.txt.enc ├── 29_outname ├── Makefile ├── README ├── grade.txt ├── outname.c ├── outname.h └── outname_test.c ├── 30_sort_lines ├── Makefile ├── README ├── grade.txt └── sortLines.c ├── 31_minesweeper ├── Makefile ├── README ├── grade.txt └── minesweeper.c ├── 32_kvs ├── Makefile ├── README ├── grade.txt ├── kv.c ├── kv.h ├── kv_test.c └── test.txt ├── 33_counts ├── Makefile ├── README ├── counts.c ├── counts.h ├── counts_test.c └── grade.txt ├── 34_put_together ├── Makefile ├── README ├── counts.c ├── counts.h ├── grade.txt ├── kv.c ├── kv.h ├── kvs1.txt ├── kvs2.txt ├── list1a.txt ├── list1a.txt.ans ├── list1b.txt ├── list1b.txt.ans ├── list2a.txt ├── list2a.txt.ans ├── list2b.txt ├── list2b.txt.ans ├── list2c.txt ├── list2c.txt.ans ├── main.c ├── outname.c └── outname.h ├── README ├── and_beyond └── README ├── c2prj1_cards ├── Makefile ├── README ├── cards.c ├── cards.h ├── cards.o ├── deck-c4.o ├── deck.o ├── eval-c4.o ├── eval.o ├── future.o ├── grade.txt ├── input.o ├── main.o ├── my-test-main.c ├── my-test-main.o └── poker ├── c2prj2_testing ├── README ├── example.txt ├── grade.txt ├── run_all.sh ├── test-eval └── tests.txt ├── c3prj1_deck ├── Makefile ├── README ├── cards.c ├── cards.h ├── cards.o ├── deck-c4.o ├── deck.c ├── deck.h ├── deck.o ├── grade.txt └── test-deck.o ├── c3prj2_eval ├── Makefile ├── README ├── cards.c ├── cards.h ├── cards.o ├── deck-c4.o ├── deck.c ├── deck.h ├── deck.o ├── eval-c4.o ├── eval.c ├── eval.h ├── eval.o ├── future.o ├── grade.txt ├── input.o ├── test-eval.o └── tests.txt ├── c4prj1_deck ├── README ├── cards.c ├── cards.h ├── deck.c ├── deck.h ├── eval.c ├── eval.h └── grade.txt ├── c4prj2_input ├── README ├── cards.c ├── cards.h ├── deck.c ├── deck.h ├── eval.c ├── eval.h ├── future.c ├── future.h ├── grade.txt ├── input.c └── input.h └── c4prj3_finish ├── Makefile ├── README ├── cards.c ├── cards.h ├── deck.c ├── deck.h ├── eval.c ├── eval.h ├── future.c ├── future.h ├── grade.txt ├── input.c ├── input.h ├── main.c └── provided-tests ├── answers.txt ├── test01.txt ├── test02.txt ├── test03.txt ├── test04.txt ├── test05.txt ├── test06.txt ├── test07.txt ├── test08.txt ├── test09.txt ├── test10.txt ├── test11.txt ├── test12.txt ├── test13.txt ├── test14.txt ├── test15.txt └── test16.txt /00_hello/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Jul 22 21:36:09 UTC 2018 2 | Your file matched the expected output 3 | 4 | Overall Grade: PASSED 5 | -------------------------------------------------------------------------------- /00_hello/hello.txt: -------------------------------------------------------------------------------- 1 | hello 2 | -------------------------------------------------------------------------------- /01_apple/fruit.txt: -------------------------------------------------------------------------------- 1 | apple 2 | -------------------------------------------------------------------------------- /01_apple/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Mon Jul 23 22:34:11 UTC 2018 2 | Your file matched the expected output 3 | 4 | Overall Grade: PASSED 5 | -------------------------------------------------------------------------------- /02_code1/code1.c: -------------------------------------------------------------------------------- 1 | int max (int num1, int num2) { 2 | //check if num1 is greater than num2 3 | //if so, your answer is num1 4 | //otherwise, your answer is num2 5 | if (num1 > num2 )return num1; 6 | else return num2; 7 | } 8 | 9 | int main(void) { 10 | printf("max(42, -69) is %d\n", max(42, -69)); 11 | printf("max(33, 0) is %d\n", max(33, 0)); 12 | printf("max(0x123456, 123456) is %d\n", max(0x123456, 123456)); 13 | printf("max(0x451215AF, 0x913591AF) is %d\n",max( 0x451215AF , 0x913591AF)); 14 | //compute the max of 0x451215AF and 0x913591AF and print it out as a decimal number 15 | 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /02_code1/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Tue Jul 24 11:41:18 UTC 2018 2 | Checking code1.c for legal syntax 3 | Checking for int max (int num1, int num2) 4 | Found on line 3, column 1 5 | Checking for int main(void) 6 | Found on line 11, column 1 7 | Trying to run the code.. 8 | Your file matched the expected output 9 | Removing your main() and replacing it with our own to run more tests... 10 | Testing max(-999, -2147483648) ... Correct 11 | Testing max(-999, 123) ... Correct 12 | Testing max(-999, 567) ... Correct 13 | Testing max(-999, 891) ... Correct 14 | Testing max(-999, 0) ... Correct 15 | Testing max(-999, 1) ... Correct 16 | Testing max(-999, -999) ... Correct 17 | Testing max(-999, 123123123) ... Correct 18 | Testing max(-87, -2147483648) ... Correct 19 | Testing max(-87, 123) ... Correct 20 | Testing max(-87, 567) ... Correct 21 | Testing max(-87, 891) ... Correct 22 | Testing max(-87, 0) ... Correct 23 | Testing max(-87, 1) ... Correct 24 | Testing max(-87, -999) ... Correct 25 | Testing max(-87, 123123123) ... Correct 26 | Testing max(0, -2147483648) ... Correct 27 | Testing max(0, 123) ... Correct 28 | Testing max(0, 567) ... Correct 29 | Testing max(0, 891) ... Correct 30 | Testing max(0, 0) ... Correct 31 | Testing max(0, 1) ... Correct 32 | Testing max(0, -999) ... Correct 33 | Testing max(0, 123123123) ... Correct 34 | Testing max(1, -2147483648) ... Correct 35 | Testing max(1, 123) ... Correct 36 | Testing max(1, 567) ... Correct 37 | Testing max(1, 891) ... Correct 38 | Testing max(1, 0) ... Correct 39 | Testing max(1, 1) ... Correct 40 | Testing max(1, -999) ... Correct 41 | Testing max(1, 123123123) ... Correct 42 | Testing max(240, -2147483648) ... Correct 43 | Testing max(240, 123) ... Correct 44 | Testing max(240, 567) ... Correct 45 | Testing max(240, 891) ... Correct 46 | Testing max(240, 0) ... Correct 47 | Testing max(240, 1) ... Correct 48 | Testing max(240, -999) ... Correct 49 | Testing max(240, 123123123) ... Correct 50 | Testing max(345, -2147483648) ... Correct 51 | Testing max(345, 123) ... Correct 52 | Testing max(345, 567) ... Correct 53 | Testing max(345, 891) ... Correct 54 | Testing max(345, 0) ... Correct 55 | Testing max(345, 1) ... Correct 56 | Testing max(345, -999) ... Correct 57 | Testing max(345, 123123123) ... Correct 58 | Testing max(999999, -2147483648) ... Correct 59 | Testing max(999999, 123) ... Correct 60 | Testing max(999999, 567) ... Correct 61 | Testing max(999999, 891) ... Correct 62 | Testing max(999999, 0) ... Correct 63 | Testing max(999999, 1) ... Correct 64 | Testing max(999999, -999) ... Correct 65 | Testing max(999999, 123123123) ... Correct 66 | Testing max(2147483647, -2147483648) ... Correct 67 | Testing max(2147483647, 123) ... Correct 68 | Testing max(2147483647, 567) ... Correct 69 | Testing max(2147483647, 891) ... Correct 70 | Testing max(2147483647, 0) ... Correct 71 | Testing max(2147483647, 1) ... Correct 72 | Testing max(2147483647, -999) ... Correct 73 | Testing max(2147483647, 123123123) ... Correct 74 | 75 | Overall Grade: A 76 | -------------------------------------------------------------------------------- /02_code1/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat > temp.c < 4 | #include 5 | EOF 6 | cat code1.c >> temp.c 7 | gcc -Wall -Werror -pedantic -std=gnu99 temp.c -o code1 2>errors.txt 8 | if [ "$?" = "0" ] 9 | then 10 | echo "Your code appears to have legal syntax!" 11 | echo "Here is what I get when I run it..." 12 | echo "-----------------" 13 | ./code1 14 | echo "-----------------" 15 | echo "Here is what I would expect the answers to be:" 16 | echo "----------------" 17 | cat < temp.c < 4 | #include 5 | EOF 6 | cat code2.c >> temp.c 7 | gcc -Wall -Werror -pedantic -std=gnu99 temp.c -o code2 2>errors.txt 8 | if [ "$?" = "0" ] 9 | then 10 | echo "Your code appears to have legal syntax!" 11 | echo "Here is what I get when I run it..." 12 | echo "-----------------" 13 | ./code2 14 | echo "-----------------" 15 | echo "Here is what I would expect the answers to be:" 16 | echo "----------------" 17 | cat < 2 | #include 3 | 4 | 5 | int main (){ 6 | printf("Hello World\n"); 7 | return EXIT_SUCCESS; 8 | } 9 | -------------------------------------------------------------------------------- /05_squares/README: -------------------------------------------------------------------------------- 1 | 1. Open the file "squares.c". Inside this file, 2 | you will find that I have already done steps 1--4 3 | for an algorithm which draws two (possibly overlapping) 4 | squares (one of #s and one of *s). 5 | 6 | 2. Read the generalized steps that I have written as comments 7 | in this file. Note that whenever I indicate a range 8 | (count from x to y, or between x and y), the range is 9 | inclusive of the lower bound, and exclusive of the upper bound. 10 | All counting is "count up by one." Determine if there are 11 | any parts of these steps that you will want to abstract 12 | out into a separate function. 13 | 14 | 3. Implement this algorithm by translating my steps into code. 15 | You may abstract any pieces you want out into separate functions. 16 | If you do so, write these new functions above the "squares" function 17 | when you finish translating it. 18 | 19 | 4. We have provided squares_test.o, which has a main function 20 | that takes four command line arguments (size1, x_offset, y_offset, size2), 21 | and calls your squares function with those arguments. 22 | You can compile your code and link it with this object file 23 | to make a program: 24 | gcc -o squares -Wall -Werror -std=gnu99 --pedantic squares.c squares_test.o 25 | Then you can run it like this: 26 | ./square 4 1 2 3 27 | which would call your squares function with 28 | size1=4 29 | x_offset=1 30 | y_offset=2 31 | size2=3 32 | 33 | which should produce output that looks like this: 34 | #### 35 | # # 36 | #*** 37 | #*#* 38 | *** 39 | We have also provided 3 files which show the correct output for three inputs 40 | (./squares 3 5 8 2,./squares 5 2 4 6,./squares 9 2 3 4) in the files 41 | whose names starts with ans_ (and then has the parameter values in its name, 42 | separated by _s). 43 | 44 | Use "diff" like you just learned to compare your program's output 45 | to the correct output. 46 | 47 | 5. Submit your modified squares.c file (git commit/git push/grade). 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /05_squares/ans_3_5_8_2.txt: -------------------------------------------------------------------------------- 1 | ### 2 | # # 3 | ### 4 | 5 | 6 | 7 | 8 | 9 | ** 10 | ** 11 | -------------------------------------------------------------------------------- /05_squares/ans_5_2_4_6.txt: -------------------------------------------------------------------------------- 1 | ##### 2 | # # 3 | # # 4 | # # 5 | ##****** 6 | * * 7 | * * 8 | * * 9 | * * 10 | ****** 11 | -------------------------------------------------------------------------------- /05_squares/ans_9_2_3_4.txt: -------------------------------------------------------------------------------- 1 | ######### 2 | # # 3 | # # 4 | # **** # 5 | # * * # 6 | # * * # 7 | # **** # 8 | # # 9 | ######### 10 | -------------------------------------------------------------------------------- /05_squares/squares.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int max (int x ,int y){ 5 | if (x>y)return x ; 6 | else return y;} 7 | 8 | 9 | 10 | void squares(int size1, int x_offset, int y_offset, int size2) { 11 | //compute the max of size1 and (x_offset + size2). Call this w 12 | int w,h,x,y; 13 | int con1,con2,con3,con4; 14 | x=y=0; 15 | w= max(size1,x_offset + size2); 16 | h=max(size1,y_offset + size2); 17 | con1=(((x >= x_offset )&&(x < x_offset + size2 ))&&((y == y_offset)||(y == y_offset+size2 -1))); 18 | con2=(((y >= y_offset )&&(y < y_offset + size2 ))&&((x == x_offset)||(x == x_offset+size2 -1))); 19 | con3=((x= x_offset )&&(x < x_offset + size2 ))&&((y == y_offset)||(y == y_offset+size2 -1))); 28 | con2=(((y >= y_offset )&&(y < y_offset + size2 ))&&((x == x_offset)||(x == x_offset+size2 -1))); 29 | con3=((x 2 | #include 3 | //I've provided "min" and "max" functions in 4 | //case they are useful to you 5 | int min (int a, int b) { 6 | if (a < b) { 7 | return a; 8 | } 9 | return b; 10 | } 11 | int max (int a, int b) { 12 | if (a > b) { 13 | return a; 14 | } 15 | return b; 16 | } 17 | 18 | //Declare your rectangle structure here! 19 | struct rect { 20 | int x; 21 | int y; 22 | int width; 23 | int height; 24 | }; 25 | typedef struct rect rectangle; 26 | 27 | rectangle canonicalize(rectangle r) { 28 | //WRITE THIS FUNCTION 29 | if(r.width < 0) { 30 | r.x = r.x + r.width; 31 | r.width=0-r.width;} 32 | 33 | 34 | if (r.height < 0){ 35 | 36 | r.y=r.y+r.height; 37 | r.height=0-r.height; 38 | } 39 | return r; 40 | } 41 | rectangle intersection(rectangle r1, rectangle r2) { 42 | //WRITE THIS FUNCTION 43 | rectangle ans; 44 | 45 | r1=canonicalize(r1); 46 | r2=canonicalize(r2); 47 | ans.x=max(r1.x,r2.x); 48 | ans.y=max(r1.y,r2.y); 49 | int tem; 50 | int temy; 51 | tem= min(r1.x,r2.x); 52 | temy=min(r1.y,r2.y); 53 | if (((tem == r1.x)&&(r2.x > r1.x + r1.width))||((temy==r1.y)&&(r2.y > r1.y + r1.height))){ 54 | ans.width=0; 55 | ans.height=0; 56 | return ans;} 57 | if(((tem == r2.x)&&(r1.x > r2.x + r2.width))||((temy==r2.y)&&(r1.y > r2.y + r2.height))){ 58 | ans.width=0; 59 | ans.height=0; 60 | return ans;} 61 | 62 | 63 | 64 | int minheight=min(r1.y+r1.height,r2.y+r2.height); 65 | int minwidth=min(r1.x+r1.width,r2.x+r2.width); 66 | ans.height=minheight - ans.y; 67 | if(minheight <0) ans.height=minheight + ans.y; 68 | 69 | ans.width=minwidth-ans.x; 70 | if (minwidth < 0 ) ans.width=minwidth+ans.x; 71 | return ans; 72 | } 73 | 74 | //You should not need to modify any code below this line 75 | void printRectangle(rectangle r) { 76 | r = canonicalize(r); 77 | if (r.width == 0 && r.height == 0) { 78 | printf("\n"); 79 | } 80 | else { 81 | printf("(%d,%d) to (%d,%d)\n", r.x, r.y, 82 | r.x + r.width, r.y + r.height); 83 | } 84 | } 85 | 86 | int main (void) { 87 | rectangle r1; 88 | rectangle r2; 89 | rectangle r3; 90 | rectangle r4; 91 | 92 | r1.x = 2; 93 | r1.y = 3; 94 | r1.width = 5; 95 | r1.height = 6; 96 | printf("r1 is "); 97 | printRectangle(r1); 98 | 99 | r2.x = 4; 100 | r2.y = 5; 101 | r2.width = -5; 102 | r2.height = -7; 103 | printf("r2 is "); 104 | printRectangle(r2); 105 | 106 | r3.x = -2; 107 | r3.y = 7; 108 | r3.width = 7; 109 | r3.height = -10; 110 | printf("r3 is "); 111 | printRectangle(r3); 112 | 113 | r4.x = 0; 114 | r4.y = 7; 115 | r4.width = -4; 116 | r4.height = 2; 117 | printf("r4 is "); 118 | printRectangle(r4); 119 | 120 | //test everything with r1 121 | rectangle i = intersection(r1,r1); 122 | printf("intersection(r1,r1): "); 123 | printRectangle(i); 124 | 125 | i = intersection(r1,r2); 126 | printf("intersection(r1,r2): "); 127 | printRectangle(i); 128 | 129 | i = intersection(r1,r3); 130 | printf("intersection(r1,r3): "); 131 | printRectangle(i); 132 | 133 | i = intersection(r1,r4); 134 | printf("intersection(r1,r4): "); 135 | printRectangle(i); 136 | 137 | //test everything with r2 138 | i = intersection(r2,r1); 139 | printf("intersection(r2,r1): "); 140 | printRectangle(i); 141 | 142 | i = intersection(r2,r2); 143 | printf("intersection(r2,r2): "); 144 | printRectangle(i); 145 | 146 | i = intersection(r2,r3); 147 | printf("intersection(r2,r3): "); 148 | printRectangle(i); 149 | 150 | i = intersection(r2,r4); 151 | printf("intersection(r2,r4): "); 152 | printRectangle(i); 153 | 154 | //test everything with r3 155 | i = intersection(r3,r1); 156 | printf("intersection(r3,r1): "); 157 | printRectangle(i); 158 | 159 | i = intersection(r3,r2); 160 | printf("intersection(r3,r2): "); 161 | printRectangle(i); 162 | 163 | i = intersection(r3,r3); 164 | printf("intersection(r3,r3): "); 165 | printRectangle(i); 166 | 167 | i = intersection(r3,r4); 168 | printf("intersection(r3,r4): "); 169 | printRectangle(i); 170 | 171 | //test everything with r4 172 | i = intersection(r4,r1); 173 | printf("intersection(r4,r1): "); 174 | printRectangle(i); 175 | 176 | i = intersection(r4,r2); 177 | printf("intersection(r4,r2): "); 178 | printRectangle(i); 179 | 180 | i = intersection(r4,r3); 181 | printf("intersection(r4,r3): "); 182 | printRectangle(i); 183 | 184 | i = intersection(r4,r4); 185 | printf("intersection(r4,r4): "); 186 | printRectangle(i); 187 | 188 | 189 | return EXIT_SUCCESS; 190 | 191 | } 192 | -------------------------------------------------------------------------------- /06_rect/rectangle_ans.txt: -------------------------------------------------------------------------------- 1 | r1 is (2,3) to (7,9) 2 | r2 is (-1,-2) to (4,5) 3 | r3 is (-2,-3) to (5,7) 4 | r4 is (-4,7) to (0,9) 5 | intersection(r1,r1): (2,3) to (7,9) 6 | intersection(r1,r2): (2,3) to (4,5) 7 | intersection(r1,r3): (2,3) to (5,7) 8 | intersection(r1,r4): 9 | intersection(r2,r1): (2,3) to (4,5) 10 | intersection(r2,r2): (-1,-2) to (4,5) 11 | intersection(r2,r3): (-1,-2) to (4,5) 12 | intersection(r2,r4): 13 | intersection(r3,r1): (2,3) to (5,7) 14 | intersection(r3,r2): (-1,-2) to (4,5) 15 | intersection(r3,r3): (-2,-3) to (5,7) 16 | intersection(r3,r4): (-2,7) to (0,7) 17 | intersection(r4,r1): 18 | intersection(r4,r2): 19 | intersection(r4,r3): (-2,7) to (0,7) 20 | intersection(r4,r4): (-4,7) to (0,9) 21 | -------------------------------------------------------------------------------- /07_retirement/Makefile: -------------------------------------------------------------------------------- 1 | retirement: retirement.c 2 | gcc -o retirement -pedantic -std=gnu99 -Wall -Werror retirement.c 3 | -------------------------------------------------------------------------------- /07_retirement/README: -------------------------------------------------------------------------------- 1 | For this problem, you will be writing a "retirement savings" calculator. 2 | We'll remove the effects of inflation by keeping everything in 3 | "today's dollars" and using a "Rate of return" that is in terms of 4 | "more than inflation." 5 | 6 | 1. Create a file called "retirement.c". Include the usual header 7 | files (stdlib.h and stdio.h) at the top. 8 | 9 | 2. We're going to model both savings (while working) and expenditure 10 | (while retired). It turns out that both of these require the 11 | same basic information, so we will make a struct to represent that. 12 | Declare a struct _retire_info which has three fields: 13 | (1) an int called "months" for the number of months it is applicable to, 14 | (2) a double called "contribution" for how many dollars 15 | are contributed (or spent if negative) from the account per month 16 | (3) a double called "rate_of_return" for the rate of returns 17 | (which we will assume to be "after inflation"). 18 | 19 | After you have declared this struct, use typedef to make "retire_info" 20 | another name for this struct. 21 | 22 | 3. Write the function 23 | void retirement (int startAge, //in months 24 | double initial, //initial savings in dollars 25 | retire_info working, //info about working 26 | retire_info retired) //info about being retired 27 | 28 | This function should perform two tasks (which are similar---look 29 | for a chance to abstract something out into a function!). 30 | 31 | First, it should compute your retirement account balance each 32 | month while you are working. To do this, you need to calculate 33 | the account balance increase from returns (balance * rate of return), 34 | and add that to the current balance. You then need to add the 35 | monthly contribution to the balance. 36 | For example, if you have $1,000 in the account, earn a 0.5% rate of 37 | return per month, and contribute $100 per month, you would 38 | cmopute 1000 * 0.005 = $5 in interest earned. You would then 39 | add this plus the monthly contribution to the balance to end up 40 | with $1105 in the account at the end of the month. 41 | 42 | At the start of each month (before the balance changes), you should 43 | print out the current balance with the following format: 44 | "Age %3d month %2d you have $%.2lf\n" 45 | The first two format conversions are the savers age in years and months. 46 | The third format conversion is the account balance 47 | This calculation goes on for the number of months specified 48 | in the "working" retire_info structure. 49 | 50 | 51 | Second, you should perform a very similar calculation for each 52 | month of retirment. The difference here is that you will use the 53 | information in the "retired" retire_info structure instead 54 | of the information in the "working" structure. As with 55 | working, you should print out the same information as before. 56 | 57 | [Hint: since you are performing a very similar computation, 58 | think about how you can abstract that part out into a function, 59 | and re-use it, rather than re-writing it] 60 | 61 | 4. Write a main function which computes the retirement assuming 62 | Working: 63 | -------- 64 | Months: 489 65 | Per Month Savings: $1000 66 | Rate of Return: 4.5% per year ( 0.045/12 per month) 67 | [above inflation] 68 | Retired: 69 | -------- 70 | Months: 384 71 | Per Month Spending: -4000 72 | Rate of Return: 1% per year ( 0.01/12 per month) 73 | [above inflation] 74 | Starting conditions: 75 | ------------------- 76 | Age: 327 months (27 years, 3 months) 77 | Savings: $21,345 78 | 5. Compile your code (we provided a Makefile) and test 79 | it (we provided the output: retirement_ans.txt). 80 | 81 | 6. Submit retirement.c 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /07_retirement/retirement: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/07_retirement/retirement -------------------------------------------------------------------------------- /07_retirement/retirement.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | struct _retire_info{ 6 | int months; 7 | double contribution; 8 | double rate_of_return; 9 | }; 10 | typedef struct _retire_info retire_info; 11 | 12 | double cal(double acc , retire_info st ){ 13 | return acc*st.rate_of_return + st.contribution + acc; 14 | } 15 | void retirement (int startAge ,double initial , retire_info working,retire_info retired){ 16 | double temp; 17 | temp= initial; 18 | for (int i=0;i&1` 9 | IFS=" " broken=`$prog $line 2>&1` 10 | if [ "$broken" != "$correct" ] 11 | then 12 | return 0 13 | fi 14 | done 15 | return 1 16 | } 17 | 18 | found=0 19 | notfound=0 20 | for i in /usr/local/l2p/match5/match5-* 21 | do 22 | run_test $i tests.txt 23 | x="$?" 24 | if [ "$x" != "0" ] 25 | then 26 | echo "Your test cases did not identify the problem with `basename $i`" 27 | let notfound=${notfound}+1 28 | else 29 | let found=${found}+1 30 | fi 31 | done 32 | echo "Test cases identified $found problems" 33 | echo "Test cases failed to identify $notfound problems" 34 | -------------------------------------------------------------------------------- /09_testing2/tests.txt: -------------------------------------------------------------------------------- 1 | 2 | baaaa aaaaa 3 | aaaaa baaaa 4 | aaaas aaaaa 5 | aaaaa aaaas 6 | aaaaa aaaaa 7 | aaaaa bbbbb 8 | aaasa aaagh 9 | aaagh aaasa 10 | aadfg aghjk 11 | aghjk aadfg 12 | aadda aafga 13 | aafga aadda 14 | aaakk aakkh 15 | asdfg jkl;, 16 | aa1aa aaaaa 17 | aaaaa aa2aa 18 | aa aaaaa 19 | aaaaa aa 20 | aaaaa 21 | aaaaa 22 | dddddd kkkkk 23 | kkkkk llllll 24 | hhhhhs ssssss 25 | !!!&& $$$ff 26 | Aaaaa aaaaa 27 | AAabb abaab 28 | asdfg qwert 29 | hhhhh 30 | aaaaa 31 | aadaa addaa addda 32 | 33 | -------------------------------------------------------------------------------- /10_gdb/README: -------------------------------------------------------------------------------- 1 | For this assignment, you will be practicing with the basics 2 | of gdb. You will want to use gdb extensively during the rest 3 | of the semester, as it is an incredibly useful tool to gather 4 | information during the debugging process. For now, we 5 | are just going to get started with some basic commands. 6 | Be sure you have done the readings and watched 7 | the video on gdb. 8 | 9 | Enclosed, you will find game.c and the compiled 10 | binary game, for the most boring guessing game 11 | ever. In the first round, the program thinks 12 | of a secret number (it is the same every single time), 13 | and asks you to guess it. You get exactly one try. 14 | Note that it thinks of this number by calling 15 | getSecretNumber, which is not included in the source. 16 | 17 | If you get that right, in the second round, 18 | it thinks of another number. It does this by repeatedly 19 | calling getOtherSN (also not shown in the source) and 20 | passing in different numbers. It combines these results 21 | together, and does some math to combine the results into 22 | "total". Afterwards, it asks you for your guess, 23 | and again, you have one chance. 24 | 25 | Before you proceed, try to "play" the game once by running: 26 | 27 | ./game 28 | 29 | 30 | For example, I ran it and guessed 4: 31 | $ ./game 32 | I'm thinking of a number... 33 | What number do you guess? 34 | 4 35 | I'm sorry, that is not right. You lose 36 | 37 | It would be pretty boring to play this game 38 | until you actually guess the right number and win, 39 | but fortunately, that isn't the point. The 40 | point is to practice with gdb. 41 | 42 | In emacs, do 43 | 44 | ESC-x gdb 45 | 46 | Emacs will prompt you for how to run gdb (the default should be fine), 47 | and then give you the gdb prompt. 48 | Use the "start" commmand to begin execution, then use "next" 49 | and "print" to find the secret number for round 1. 50 | When the program prompts you for this number, you should be able 51 | to guess the right one from the information you gathered. 52 | 53 | For round 2, you do not want to step through 5000+ iterations of the 54 | loop, so set a breakpoint after the loop, continue execution until you 55 | reach it, and the print out the variable "total". Now you should 56 | be able to win round 2 instantly as well! 57 | 58 | Once you have found the two secret numbers, create a file called input.txt 59 | and place them in that file, one per line (round 1's secret number 60 | on the first line, and round 2's on the second line). 61 | 62 | You should be able to run 63 | 64 | ./game < input.txt 65 | 66 | and "win" automatically. That is, you should see this output (without 67 | having to type anything else): 68 | 69 | $ ./game < input.txt 70 | I'm thinking of a number... 71 | What number do you guess? 72 | Correct! You win round1! 73 | Ok, time for round 2. I have another secret number. 74 | Your guess: 75 | You win round 2 also! 76 | 77 | 78 | When you finish, add input.txt to git, then commit, push, and grade. 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /10_gdb/game: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/10_gdb/game -------------------------------------------------------------------------------- /10_gdb/game.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int getSecretNumber(void); //prototype, implemented elsewhere. 5 | 6 | int getOtherSN(int which); //prototype, implemented elsewhere. 7 | 8 | int main(void) { 9 | int guessesMade = 0; 10 | int yourGuess; 11 | char buffer[1024]; 12 | int myNumber = getSecretNumber(); 13 | 14 | printf("I'm thinking of a number...\n"); 15 | printf("What number do you guess?\n"); 16 | if(fgets(buffer, 1024, stdin) == NULL) { 17 | printf("Oh no, you are giving up? You lose...\n"); 18 | return EXIT_FAILURE; 19 | } 20 | yourGuess = atoi(buffer); 21 | if(yourGuess != myNumber) { 22 | printf("I'm sorry, that is not right. You lose\n"); 23 | return EXIT_FAILURE; 24 | } 25 | printf("Correct! You win round1!\n"); 26 | 27 | int total = 0; 28 | for (int i = 0; i <= 5678; i++) { 29 | total = total ^ getOtherSN(i); 30 | } 31 | printf("Ok, time for round 2. I have another secret number.\n"); 32 | printf("Your guess:\n"); 33 | if(fgets(buffer, 1024, stdin) == NULL) { 34 | printf("Oh no, you are giving up? You lose...\n"); 35 | return EXIT_FAILURE; 36 | } 37 | yourGuess = atoi(buffer); 38 | if (yourGuess == total) { 39 | printf("You win round 2 also!\n"); 40 | return EXIT_SUCCESS; 41 | } 42 | printf("Sorry, you did not win the second round\n"); 43 | return EXIT_FAILURE; 44 | } 45 | -------------------------------------------------------------------------------- /10_gdb/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Jul 28 15:31:13 UTC 2018 2 | Your file matched the expected output 3 | 4 | Overall Grade: PASSED 5 | -------------------------------------------------------------------------------- /10_gdb/input.txt: -------------------------------------------------------------------------------- 1 | 464384013 2 | 938257400 3 | -------------------------------------------------------------------------------- /11_read_ptr1/Makefile: -------------------------------------------------------------------------------- 1 | test: test.o 2 | gcc -o test test.o 3 | test.o: test.c 4 | gcc -std=gnu99 -pedantic -Wall -c test.c 5 | -------------------------------------------------------------------------------- /11_read_ptr1/README: -------------------------------------------------------------------------------- 1 | 1. Execute the code in "test.c" by hand, and write the output 2 | into a file called "answer.txt" 3 | 4 | 2. Create a Makefile to compile test.c into a program called "test" 5 | 6 | 3. Run test and use its output to check your work. 7 | 8 | 4. Submit your Makefile and your answer.txt file 9 | 10 | 11 | -------------------------------------------------------------------------------- /11_read_ptr1/answer.txt: -------------------------------------------------------------------------------- 1 | In f, *a = 3, b = 4 2 | In g, x = 7, *y = 8 3 | Back in f, *a = 7, b = 0 4 | In main: x = 7, y = 4 5 | -------------------------------------------------------------------------------- /11_read_ptr1/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Fri Aug 3 12:11:47 UTC 2018 2 | Attempting to compile test.c 3 | gcc -std=gnu99 -pedantic -Wall -c test.c 4 | gcc -o test test.o 5 | compiled 6 | Your file matched the expected output 7 | Your output matched what we expected 8 | 9 | Overall Grade: PASSED 10 | -------------------------------------------------------------------------------- /11_read_ptr1/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void g(int x, int * y) { 5 | printf("In g, x = %d, *y = %d\n", x, *y); 6 | x++; 7 | *y = *y - x; 8 | y = &x; 9 | } 10 | 11 | void f(int * a, int b) { 12 | printf("In f, *a = %d, b = %d\n", *a, b); 13 | *a += b; 14 | b *= 2; 15 | g(*a, &b); 16 | printf("Back in f, *a = %d, b = %d\n", *a, b); 17 | } 18 | 19 | 20 | int main(void) { 21 | int x = 3; 22 | int y = 4; 23 | f(&x, y); 24 | printf("In main: x = %d, y = %d\n", x, y); 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /12_read_ptr2/Makefile: -------------------------------------------------------------------------------- 1 | test: test.o 2 | gcc -o test test.o 3 | test.o: test.c 4 | gcc -std=gnu99 -pedantic -Wall -c test.c 5 | -------------------------------------------------------------------------------- /12_read_ptr2/README: -------------------------------------------------------------------------------- 1 | 1. Execute the code in "test.c" by hand, and write the output 2 | into a file called "answer.txt" 3 | 4 | 2. Create a Makefile to compile test.c into a program called "test" 5 | 6 | 3. Run test and use its output to check your work. 7 | 8 | 4. Submit your Makefile and your answer.txt file 9 | 10 | 11 | -------------------------------------------------------------------------------- /12_read_ptr2/answer.txt: -------------------------------------------------------------------------------- 1 | **r = 12 2 | **s = 80 3 | x = 92 4 | *p = -7 5 | *q = 75 6 | a = 75 7 | b = -7 8 | -------------------------------------------------------------------------------- /12_read_ptr2/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 4 00:07:03 UTC 2018 2 | Attempting to compile test.c 3 | gcc -std=gnu99 -pedantic -Wall -c test.c 4 | gcc -o test test.o 5 | compiled 6 | Your file matched the expected output 7 | Your output matched what we expected 8 | 9 | Overall Grade: PASSED 10 | -------------------------------------------------------------------------------- /12_read_ptr2/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int f(int ** r, int ** s) { 5 | int temp = ** r; 6 | int temp2 = **s; 7 | int * z = *r; 8 | *r = *s; 9 | *s = z; 10 | printf("**r = %d\n",**r); 11 | printf("**s = %d\n",**s); 12 | *z += 3; 13 | **s -= 8; 14 | **r -= 19; 15 | return temp + temp2; 16 | } 17 | 18 | int main(void) { 19 | int a = 80; 20 | int b = 12; 21 | int * p = &a; 22 | int * q = &b; 23 | int x = f(&p, &q); 24 | printf("x = %d\n", x); 25 | printf("*p = %d\n", *p); 26 | printf("*q = %d\n", *q); 27 | printf("a = %d\n", a); 28 | printf("b = %d\n", b); 29 | return EXIT_SUCCESS; 30 | } 31 | -------------------------------------------------------------------------------- /13_read_arr1/Makefile: -------------------------------------------------------------------------------- 1 | test: test.o 2 | gcc -o test test.o 3 | test.o: test.c 4 | gcc -std=gnu99 -pedantic -Wall -c test.c 5 | -------------------------------------------------------------------------------- /13_read_arr1/README: -------------------------------------------------------------------------------- 1 | 2 | 1. Execute the code in "test.c" by hand, and write the output 3 | into a file called "answer.txt" 4 | 5 | 2. Create a Makefile to compile test.c into a program called "test" 6 | 7 | 3. Run test and use its output to check your work. 8 | 9 | 4. Submit your Makefile and your answer.txt file. 10 | 11 | -------------------------------------------------------------------------------- /13_read_arr1/answer.txt: -------------------------------------------------------------------------------- 1 | *p = 5 2 | Now *p = 16 3 | anArray[0] = 5 4 | anArray[1] = 42 5 | anArray[2] = 9 6 | anArray[3] = 12 7 | -------------------------------------------------------------------------------- /13_read_arr1/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 4 13:16:27 UTC 2018 2 | Attempting to compile test.c 3 | gcc -std=gnu99 -pedantic -Wall -c test.c 4 | gcc -o test test.o 5 | compiled 6 | Your file matched the expected output 7 | Your output matched what we expected 8 | 9 | Overall Grade: A 10 | -------------------------------------------------------------------------------- /13_read_arr1/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | int anArray[] = {5,16,33,99}; 7 | int * p = anArray; 8 | printf("*p = %d\n", *p); 9 | p++; 10 | printf("Now *p = %d\n", *p); 11 | int * q = &anArray[3]; 12 | int ** x = &q; 13 | **x = 12; 14 | *x = p; 15 | **x = 42; 16 | q[1] = 9; 17 | for (int i =0; i < 4; i++){ 18 | printf("anArray[%d] = %d\n",i, anArray[i]); 19 | } 20 | return EXIT_SUCCESS; 21 | } 22 | -------------------------------------------------------------------------------- /14_array_max/Makefile: -------------------------------------------------------------------------------- 1 | arrayMax: arrayMax.c 2 | gcc -o arrayMax -pedantic -std=gnu99 -Wall -Werror arrayMax.c 3 | -------------------------------------------------------------------------------- /14_array_max/README: -------------------------------------------------------------------------------- 1 | 1. Open the provided arrayMax.c file and write 2 | the function: 3 | int * arrayMax(int * array, int n); 4 | which returns a pointer to the largest element 5 | in the array passed in (whose length is n). 6 | If the array has no elements (n is 0), this function 7 | should return NULL. 8 | 9 | 2. Compile and test your code. 10 | We have provided a main function which will print test cases 11 | and your function's answer for them. You should get 12 | 99, -3, 425, NULL, and NULL for the 5 test provided. 13 | 14 | 3. Submit your code. 15 | 16 | 17 | -------------------------------------------------------------------------------- /14_array_max/arrayMax.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int * arrayMax(int * array, int n) { 5 | if (n == 0) return NULL; 6 | int * larg; 7 | larg = &array[0]; 8 | for (int i=1 ; i < n ;i++){ 9 | if (array[i] > *larg) larg = &array[i]; 10 | } 11 | return larg; 12 | } 13 | 14 | void doTest(int * array, int n) { 15 | printf("arrayMax("); 16 | if (array == NULL) { 17 | printf("NULL"); 18 | } 19 | else { 20 | printf("{"); 21 | for (int i =0; i < n; i++) { 22 | printf("%d", array[i]); 23 | if (i < n -1) { 24 | printf(", "); 25 | } 26 | } 27 | printf("}"); 28 | } 29 | printf(", %d) is \n", n); 30 | int * p = arrayMax (array, n); 31 | if (p == NULL) { 32 | printf("NULL\n"); 33 | } 34 | else { 35 | printf("%d\n", *p); 36 | } 37 | } 38 | 39 | int main(void) { 40 | int array1[] = { 77, 33, 19, 99, 42, 6, 27, 4}; 41 | int array2[] = { -3, -42, -99, -1000, -999, -88, -77}; 42 | int array3[] = { 425, 59, -3, 77, 0, 36}; 43 | 44 | doTest (array1, 8); 45 | doTest (array2, 7); 46 | doTest (array3, 6); 47 | doTest (NULL, 0); 48 | doTest (array1, 0); 49 | 50 | return EXIT_SUCCESS; 51 | } 52 | -------------------------------------------------------------------------------- /14_array_max/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 4 13:30:30 UTC 2018 2 | Attempting to compile arrayMax.c 3 | ################################################# 4 | testcase1: 5 | Your file matched the expected output 6 | Your output matched what we expected 7 | Removing your main() and replacing it with our own to run more tests... 8 | ################################################# 9 | testcase2: 10 | array size:0 was Correct 11 | ################################################# 12 | testcase3: 13 | array size:1 was Correct 14 | ################################################# 15 | testcase4: 16 | array size:100 was Correct 17 | ################################################# 18 | testcase5: 19 | array size:5000 was Correct 20 | 21 | Overall Grade: A 22 | -------------------------------------------------------------------------------- /15_tests_subseq/README: -------------------------------------------------------------------------------- 1 | For this assignment, you will be writing testcases for 2 | your next assignment (as usual, the instructions for that assignment 3 | can be found in next-README). 4 | 5 | As usual, one correct and many broken implementations can be found 6 | in /usr/local/l2p/subseq. 7 | 8 | As with power, these are provided as compiled object files, and you 9 | should write a C program (in test-subseq.c) whose main function tests 10 | the maxSeq function. As before, it should exit with EXIT_SUCCESS if all 11 | tests pass, and EXIT_FAILURE if any test fails. Note that you will 12 | need to write the prototype for maxSeq: 13 | 14 | size_t maxSeq(int * array, size_t n); 15 | 16 | in your test-subseq.c file, so that the compiler knows about the 17 | maxSeq function. The correct place to put it is after you #include 18 | any .h files you need, but before any other code you write. 19 | 20 | We have provided run_all.sh to help you run your test cases against 21 | all implementations. 22 | 23 | -------------------------------------------------------------------------------- /15_tests_subseq/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 5 09:49:25 UTC 2018 2 | **Testing broken implementation 10 ** 3 | ------------------------------------- 4 | 5 | 6 | **Testing broken implementation 11 ** 7 | ------------------------------------- 8 | 9 | 10 | **Testing broken implementation 1 ** 11 | ------------------------------------- 12 | 13 | 14 | **Testing broken implementation 2 ** 15 | ------------------------------------- 16 | 17 | 18 | **Testing broken implementation 3 ** 19 | ------------------------------------- 20 | 21 | 22 | **Testing broken implementation 4 ** 23 | ------------------------------------- 24 | 25 | 26 | **Testing broken implementation 5 ** 27 | ------------------------------------- 28 | 29 | 30 | **Testing broken implementation 6 ** 31 | ------------------------------------- 32 | 33 | 34 | **Testing broken implementation 7 ** 35 | ------------------------------------- 36 | 37 | 38 | **Testing broken implementation 8 ** 39 | ------------------------------------- 40 | 41 | 42 | **Testing broken implementation 9 ** 43 | ------------------------------------- 44 | 45 | 46 | **Testing correct implementation ** 47 | ------------------------------------- 48 | 49 | 50 | All test programs were handled correctly 51 | 52 | Overall Grade: PASSED 53 | -------------------------------------------------------------------------------- /15_tests_subseq/next-README: -------------------------------------------------------------------------------- 1 | 2 | 1. Create a file called maxSeq.c and write the function: 3 | size_t maxSeq(int * array, size_t n); 4 | 5 | which returns the length of the maximum increasing contiguous 6 | subsequence in the array. The parameter n specifies the length 7 | of the array For example, if the array passed in were 8 | 9 | { 1, 2, 1, 3, 5, 7, 2, 4, 6, 9} 10 | 11 | this function would return 4 because the longest sequence 12 | of (strictly) increasing numbers in that array is 1, 3, 5, 7 13 | which has length 4. Note that 1,3,5,7,9 is an increasing 14 | subsequence, but is not contiguous (finding discontiguous 15 | ones efficiently takes techniques we haven't learned yet). 16 | 17 | Note that the subseqence does not need to increase at a 18 | constant rate (or follow any other pattern besides being strictly 19 | increasing). 2, 4, 67, 93, 94, 102 would be a valid increasing 20 | sequence of length 6. 21 | 22 | 23 | 2. Compile and test your code using the test-subseq.c you wrote 24 | previously. (as before, compile the .c files separately, and link 25 | them together). 26 | 27 | 3. Submit your code for maxSeq.c 28 | 29 | 30 | Hint: 31 | Can you abstract a complex step out into a simple function? -------------------------------------------------------------------------------- /15_tests_subseq/run_all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in /usr/local/l2p/subseq/subseq*.o 4 | do 5 | test=`basename $i | sed 's/subseq//' | sed 's/.o//'` 6 | if [ "$test" == "" ] 7 | then 8 | echo "**Testing correct implementation **" 9 | else 10 | echo "**Testing broken implementation ${test} **" 11 | fi 12 | echo "-------------------------------------" 13 | echo "" 14 | 15 | gcc -o test-subseq test-subseq.c $i 16 | if [ "$?" != "0" ] 17 | then 18 | echo "Could not compile test-subseq.c with $i" > /dev/stderr 19 | exit 1 20 | fi 21 | ./test-subseq 22 | if [ "$?" != 0 ] 23 | then 24 | if [ "$test" == "" ] 25 | then 26 | echo "Your test program falsely failed the correct implementation!" > /dev/stderr 27 | exit 1 28 | fi 29 | else 30 | if [ "$test" != "" ] 31 | then 32 | echo "Your test program did not identify $i as broken!" > /dev/stderr 33 | exit 1 34 | fi 35 | fi 36 | echo "" 37 | done 38 | -------------------------------------------------------------------------------- /15_tests_subseq/test-subseq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | size_t maxSeq(int * array, size_t n); 4 | 5 | int main (){ 6 | 7 | int arr[]={}; 8 | size_t ans1=maxSeq(arr,0); 9 | if (ans1 != 0) return EXIT_FAILURE; 10 | 11 | int arr1[]={ 1, 2, 1, 3, 5, 7, 2, 4, 6, 9}; 12 | ans1=maxSeq(arr1,10); 13 | if (ans1 != 4 ) return EXIT_FAILURE; 14 | 15 | int arr2[]={1,2,3,4}; 16 | size_t ans2=maxSeq(arr2,4); 17 | if (ans2 != 4) return EXIT_FAILURE; 18 | 19 | int arr5[]={1,2,2,4}; 20 | ans2=maxSeq(arr5,4); 21 | if (ans2 != 2 ) return EXIT_FAILURE; 22 | 23 | int ar[]={3,-4,5,6}; 24 | ans2=maxSeq(ar,4); 25 | if (ans2 != 3) return EXIT_FAILURE; 26 | 27 | int ar1[]={10,9,7,6}; 28 | ans2=maxSeq(ar1,4); 29 | if (ans2 != 1) return EXIT_FAILURE; 30 | 31 | return EXIT_SUCCESS; 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /16_subseq/README: -------------------------------------------------------------------------------- 1 | For this assignment, you will be writing testcases for 2 | your next assignment (as usual, the instructions for that assignment 3 | can be found in next-README). 4 | 5 | As usual, one correct and many broken implementations can be found 6 | in /usr/local/l2p/subseq. 7 | 8 | As with power, these are provided as compiled object files, and you 9 | should write a C program (in test-subseq.c) whose main function tests 10 | the maxSeq function. As before, it should exit with EXIT_SUCCESS if all 11 | tests pass, and EXIT_FAILURE if any test fails. Note that you will 12 | need to write the prototype for maxSeq: 13 | 14 | size_t maxSeq(int * array, size_t n); 15 | 16 | in your test-subseq.c file, so that the compiler knows about the 17 | maxSeq function. The correct place to put it is after you #include 18 | any .h files you need, but before any other code you write. 19 | 20 | We have provided run_all.sh to help you run your test cases against 21 | all implementations. 22 | 23 | -------------------------------------------------------------------------------- /16_subseq/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 5 11:56:48 UTC 2018 2 | Attempting to compile maxSeq.c 3 | Linking your object file with our test main 4 | ################################################# 5 | testcase2: 6 | array size:0 was Correct 7 | ################################################# 8 | testcase3: 9 | array size:1 was Correct 10 | ################################################# 11 | testcase4: 12 | array size:100 was Correct 13 | ################################################# 14 | testcase5: 15 | array size:5000 was Correct 16 | 17 | Overall Grade: A 18 | -------------------------------------------------------------------------------- /16_subseq/maxSeq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | size_t maxSeq(int * array, size_t n){ 4 | size_t lenth= 1; 5 | size_t maxlen=0; 6 | if (n == 0 ) return 0; 7 | 8 | for(size_t i=1 ; i array[i-1]){lenth += 1;} 10 | else { 11 | if (lenth > maxlen) maxlen = lenth; 12 | lenth=1; 13 | } 14 | } 15 | if (lenth > maxlen) maxlen = lenth; 16 | return maxlen; 17 | } 18 | -------------------------------------------------------------------------------- /17_read_arr2/Makefile: -------------------------------------------------------------------------------- 1 | test: test.o 2 | gcc -o test test.o 3 | test.o: test.c 4 | gcc -std=gnu99 -pedantic -Wall -c test.c 5 | -------------------------------------------------------------------------------- /17_read_arr2/README: -------------------------------------------------------------------------------- 1 | 1. Execute the code in "test.c" by hand, and write the output 2 | into a file called "answer.txt" 3 | 4 | Hint: you may need to consult the man pages for functions 5 | that you are not familiar with. 6 | 7 | 2. Create a Makefile to compile test.c into a program called "test" 8 | 9 | 3. Run test and use its output to check your work. 10 | 11 | 4. Submit your Makefile and answer.txt 12 | 13 | -------------------------------------------------------------------------------- /17_read_arr2/answer.txt: -------------------------------------------------------------------------------- 1 | p 2 | l 3 | n 4 | a 5 | bannana has nana 3 characters into it! 6 | r 7 | o 8 | fduurw 9 | -------------------------------------------------------------------------------- /17_read_arr2/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 5 12:53:03 UTC 2018 2 | Attempting to compile test.c 3 | gcc -std=gnu99 -pedantic -Wall -c test.c 4 | gcc -o test test.o 5 | compiled 6 | Your file matched the expected output 7 | Your output matched what we expected 8 | 9 | Overall Grade: A 10 | -------------------------------------------------------------------------------- /17_read_arr2/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NSTRS 3 6 | int main(void) { 7 | const char * strs[NSTRS] = {"apple", "bannana", "carrot"}; 8 | 9 | for (int i = 0; i < NSTRS; i++) { 10 | const char * a = strchr(strs[i], 'a'); 11 | a++; 12 | printf("%c\n", *a); 13 | printf("%c\n", a[2]); 14 | a = strstr(strs[i], "nana"); 15 | if (a != NULL) { 16 | printf("%s has %s %ld characters into it!\n", strs[i], a, a - strs[i]); 17 | } 18 | } 19 | const char * ptr = strs[2]; 20 | while (*ptr != '\0') { 21 | char x = *ptr + 3; 22 | printf("%c", x); 23 | ptr++; 24 | } 25 | printf("\n"); 26 | return EXIT_SUCCESS; 27 | } 28 | -------------------------------------------------------------------------------- /18_reverse_str/Makefile: -------------------------------------------------------------------------------- 1 | reverse: reverse.c 2 | gcc -o reverse -pedantic -std=gnu99 -Wall -Werror reverse.c 3 | -------------------------------------------------------------------------------- /18_reverse_str/README: -------------------------------------------------------------------------------- 1 | For this problem, you will write the function 2 | 3 | void reverse(char * str) 4 | 5 | in the provided reverse.c file. This function should reverse the string 6 | passed into it (that is, if given "abc", it should change the contents 7 | of that string to read "cba"). Note that this function's return type 8 | is "void"---it modifies the string passed into it in place. 9 | 10 | We have provided a main function which you can use to test your 11 | reverse function (do you recognize the quotations?). The correct 12 | output for your program can be found in reverse_ans.txt, so you can 13 | diff your program's output against it. 14 | 15 | Submit your reverse.c file for grading. 16 | -------------------------------------------------------------------------------- /18_reverse_str/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 5 14:20:48 UTC 2018 2 | Attempting to compile reverse.c 3 | Your file matched the expected output 4 | Your output matched what we expected 5 | Removing your main() and replacing it with our own to run more tests... 6 | ################################################# 7 | testcase2: 8 | nullptr################################################# 9 | testcase3: 10 | Your file matched the expected output 11 | ################################################# 12 | testcase4: 13 | Your file matched the expected output 14 | ################################################# 15 | testcase5: 16 | Your file matched the expected output 17 | 18 | Overall Grade: A 19 | -------------------------------------------------------------------------------- /18_reverse_str/reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void reverse(char* str) { 6 | if (str == NULL) return; 7 | size_t len = strlen(str); 8 | char x ; 9 | for(size_t i=0 ; i < len/2 ; i++){ 10 | x=str[i]; 11 | str[i]=str[len-i-1]; 12 | str[len-1-i]=x; 13 | } 14 | return; 15 | } 16 | 17 | int main(void) { 18 | char str0[] = ""; 19 | char str1[] = "123"; 20 | char str2[] = "abcd"; 21 | char str3[] = "Captain's log, Stardate 42523.7"; 22 | char str4[] = "Hello, my name is Inigo Montoya."; 23 | char str5[] = "You can be my wingman anyday!"; 24 | char str6[] = "Executor Selendis! Unleash the full power of your forces! There may be no tomorrow!"; 25 | char * array[] = {str0, str1, str2, str3, str4, str5, str6}; 26 | for (int i = 0; i < 7; i++) { 27 | reverse(array[i]); 28 | printf("%s\n", array[i]); 29 | } 30 | return EXIT_SUCCESS; 31 | } 32 | -------------------------------------------------------------------------------- /18_reverse_str/reverse_ans.txt: -------------------------------------------------------------------------------- 1 | 2 | 321 3 | dcba 4 | 7.32524 etadratS ,gol s'niatpaC 5 | .ayotnoM oginI si eman ym ,olleH 6 | !yadyna namgniw ym eb nac uoY 7 | !worromot on eb yam erehT !secrof ruoy fo rewop lluf eht hsaelnU !sidneleS rotucexE 8 | -------------------------------------------------------------------------------- /19_bits_arr/Makefile: -------------------------------------------------------------------------------- 1 | numToBits: numToBits.c 2 | gcc -o numToBits -pedantic -std=gnu99 -Wall -Werror numToBits.c 3 | -------------------------------------------------------------------------------- /19_bits_arr/README: -------------------------------------------------------------------------------- 1 | For this problem, you will be splitting numbers (a 32 bit integer) 2 | up into their individual bits. I have provided a function for you 3 | (getNthBit) which will return a specific bit from a number. For example, 4 | getNthBit(x,0) would return the first (0th) bit, getNthBit(x,1) would 5 | return the next bit, and so on. 6 | 7 | While we normally use "int" for our numbers, we are using "uint32_t". 8 | This is just like "int" except that they are unsigned ints which are 9 | guaranteed to be 32 bits long (by contrast, the normal "int" type 10 | is signed, and there are no guarantees as to how many bits are in it). 11 | We are doing this to be precise and correct: you can use a uint32_t 12 | in much the same way as an int. 13 | 14 | 1. Open the file numToBits.c 15 | Find the function 16 | 17 | void numToBits(uint32_t * nums, int nNums, int * bits, int nBits) ; 18 | 19 | This function takes in two arrays: nums (of length nNums), and 20 | bits (of length nBits). This function should: 21 | - Check that there is enough space in bits to hold all the bits 22 | of "nums". Note that each number in "nums" will results in 32 23 | bits in "bits". If this is not true, your function should 24 | print a message with the format: 25 | "Invalid call to numToBits! nBits is %d, nNums is %d\n", 26 | (where the first %d is nBits, and the second %d is nNums) 27 | then return without doing anything else. 28 | 29 | - Put the individual bits of each number into the "bits" array. 30 | The bits put into this array should be ordered so that the first 31 | 32 bits represent nums[0], the next 32 bits are nums[1], and so 32 | on. Within each number, the most significant bit (bit 31) should 33 | come first, and the least significant bit (bit 0) should come last. 34 | That is, bits[0] should be bit 31 of nums[0], bits[1] should 35 | be bit 30 of nums[0], and so on. 36 | 37 | 38 | 39 | 2. Compile and test your code. 40 | We have provided a main function which will print test cases 41 | and your function's answer for them. We have provided the 42 | correct output in bits_ans.txt 43 | 44 | 3. Submit as usual 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /19_bits_arr/bits_ans.txt: -------------------------------------------------------------------------------- 1 | 1 ( 1) => 00000000000000000000000000000001 2 | 2 ( 2) => 00000000000000000000000000000010 3 | 3 ( 3) => 00000000000000000000000000000011 4 | 4 ( 4) => 00000000000000000000000000000100 5 | 5 ( 5) => 00000000000000000000000000000101 6 | 15 ( F) => 00000000000000000000000000001111 7 | 109 ( 6D) => 00000000000000000000000001101101 8 | 123456789 ( 75BCD15) => 00000111010110111100110100010101 9 | 987654321 (3ADE68B1) => 00111010110111100110100010110001 10 | Invalid call to numToBits! nBits is 223, nNums is 7 11 | -------------------------------------------------------------------------------- /19_bits_arr/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 5 22:29:54 UTC 2018 2 | Attempting to compile numToBits.c 3 | Your file matched the expected output 4 | Your output matched what we expected 5 | Removing your main() and replacing it with our own to run more tests... 6 | ################################################# 7 | testcase2: 8 | array size:0 was Correct 9 | ################################################# 10 | testcase3: 11 | array size:1 was Correct 12 | ################################################# 13 | testcase4: 14 | array size:100 was Correct 15 | ################################################# 16 | testcase5: 17 | array size:5000 was Correct 18 | 19 | Overall Grade: A 20 | -------------------------------------------------------------------------------- /19_bits_arr/numToBits.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int getNthBit(uint32_t number, int bit) { 6 | if (bit <0 || bit >= 32) { 7 | printf("Bit %d is invalid\n", bit); 8 | exit (EXIT_FAILURE); 9 | } 10 | return (number & (1< nBits){ 15 | printf("Invalid call to numToBits! nBits is %d, nNums is %d\n",nBits,nNums); 16 | return;} 17 | for (uint32_t i =0;i < nNums;i++){ 18 | for (int j=0 ; j <32 ;j ++){ 19 | bits[i*32 + j ]=getNthBit(nums[i],31-j); 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void doTest(uint32_t * nums, int n) { 26 | int bits[n *32]; 27 | numToBits(nums, n, bits, n*32); 28 | for (int i =0; i < n; i++) { 29 | printf(" %9d (%8X) => ", nums[i], nums[i]); 30 | for (int j = 0; j < 32; j++) { 31 | printf("%d", bits[i*32 + j]); 32 | } 33 | printf("\n"); 34 | } 35 | } 36 | 37 | int main(void) { 38 | uint32_t array1[] = { 1, 2, 3, 4, 5, 15, 109}; 39 | uint32_t array2[] = { 123456789, 987654321 }; 40 | int bits[7*32-1]; 41 | doTest (array1, 7); 42 | doTest (array2, 2); 43 | numToBits(array1,7, bits , 7*32-1); 44 | return EXIT_SUCCESS; 45 | } 46 | -------------------------------------------------------------------------------- /20_rot_matrix/Makefile: -------------------------------------------------------------------------------- 1 | rotate-matrix: read-matrix.o rotate.o 2 | gcc -o rotate-matrix read-matrix.o rotate.o 3 | rotate.o: rotate.c 4 | gcc -std=gnu99 -pedantic -Wall -c rotate.c 5 | 6 | -------------------------------------------------------------------------------- /20_rot_matrix/README: -------------------------------------------------------------------------------- 1 | For this problem, you will be writing a function which 2 | performs a 90 degree clockwise rotation of a 10x10 matrix. 3 | There is nothing special about a 10x10 matrix---we are just 4 | fixing the size so that you can read the input in a future 5 | assignment after you have learned about reading files, 6 | but before you have learned about dynamic memory allocation. 7 | 8 | In particular, you should write 9 | 10 | void rotate(char matrix[10][10]) 11 | 12 | in a file called rotate.c 13 | 14 | This function takes a 10 by 10 matrix of characters and rotates 15 | it 90 degrees clockwise, updating the matrix that was passed in 16 | (remember that arrays are pointers, so you will modify 17 | the array in the frame where it was created). 18 | 19 | As you hav enot yet learned to read from files, we have 20 | provided a compiled object file, read-matrix.o. This 21 | object file has a main function which will read 22 | the input file (specified as a command line arugments 23 | to your program), call your rotate function, and 24 | then print the result. 25 | 26 | If you compiled your code (and linked with read-matrix.o) 27 | into a program called rotate-matrix, you might run it as 28 | 29 | ./rotate-matrix sample.txt 30 | 31 | It will then print the resulting matrix, which in this case 32 | should look like the contents of the file sample.out. 33 | (Remember that you can use > to redirect the output 34 | of a program to a file, and use diff to compare 35 | the contents of two files). 36 | -------------------------------------------------------------------------------- /20_rot_matrix/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Mon Aug 6 13:37:13 UTC 2018 2 | Attempting to compile rotate.c 3 | Running testcase 1 4 | Your file matched the expected output 5 | testcase1 passed 6 | Running testcase 1 7 | Your file matched the expected output 8 | testcase1 passed 9 | Running testcase 1 10 | Your file matched the expected output 11 | testcase1 passed 12 | 13 | Overall Grade: A 14 | -------------------------------------------------------------------------------- /20_rot_matrix/read-matrix.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/20_rot_matrix/read-matrix.o -------------------------------------------------------------------------------- /20_rot_matrix/rotate.c: -------------------------------------------------------------------------------- 1 | #include 2 | void rotate(char matrix[10][10]){ 3 | for (int i = 0; i < 10 ; i++) { 4 | for (int j = i; j < 10; j++) { 5 | char temp =matrix[i][j]; 6 | matrix[i][j]=matrix[j][i]; 7 | matrix[j][i]=temp; 8 | 9 | } 10 | 11 | 12 | 13 | } 14 | for (int i = 0; i < 10 ; i++) { 15 | 16 | for (int j = 0; j < 5; j++) { 17 | 18 | char temp =matrix[i][j]; 19 | 20 | matrix[i][j]=matrix[i][9-j]; 21 | 22 | matrix[i][9-j]=temp; 23 | 24 | } 25 | 26 | } 27 | 28 | return ; 29 | } 30 | -------------------------------------------------------------------------------- /20_rot_matrix/sample.out: -------------------------------------------------------------------------------- 1 | CH.....*a0 2 | oe....*.b1 3 | dl...*..c2 4 | il..*...d3 5 | no.*....e4 6 | gW*.....f5 7 | o......g6 8 | Fr......h7 9 | ul......i8 10 | nd......j9 11 | -------------------------------------------------------------------------------- /20_rot_matrix/sample.txt: -------------------------------------------------------------------------------- 1 | 0123456789 2 | abcdefghij 3 | *......... 4 | .*........ 5 | ..*....... 6 | ...*...... 7 | ....*..... 8 | .....*.... 9 | HelloWorld 10 | Coding Fun 11 | -------------------------------------------------------------------------------- /21_read_rec1/README: -------------------------------------------------------------------------------- 1 | Execute the code in test.c (by hand). Write the resulting output in a file 2 | called "answer.txt". 3 | 4 | Check your work, and submit as usual. 5 | -------------------------------------------------------------------------------- /21_read_rec1/answer.txt: -------------------------------------------------------------------------------- 1 | a=29, b=7 2 | a=2, b=9 3 | a=0, b=2 4 | 297 5 | -------------------------------------------------------------------------------- /21_read_rec1/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Tue Aug 7 13:45:20 UTC 2018 2 | Your file matched the expected output 3 | Your output matched what we expected 4 | 5 | Overall Grade: A 6 | -------------------------------------------------------------------------------- /21_read_rec1/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void printDigits(int x) { 5 | if (x == 0) { 6 | printf("0"); 7 | } 8 | else if (x < 0) { 9 | printf("-"); 10 | printDigits(-x); 11 | } 12 | else { 13 | int a = x/10; 14 | int b = x %10; 15 | printf("a=%d, b=%d\n",a,b); 16 | if (a != 0) { 17 | printDigits(a); 18 | } 19 | printf("%d",b); 20 | } 21 | } 22 | 23 | 24 | int main(void) { 25 | printDigits(297); 26 | printf("\n"); 27 | return EXIT_SUCCESS; 28 | } 29 | -------------------------------------------------------------------------------- /22_tests_power/README: -------------------------------------------------------------------------------- 1 | For this assignment, you will be writing test cases for 2 | 3 | unsigned power (unsigned x, unsigned y); 4 | 5 | which you will be writing in the next assignment (if you want to read 6 | the instructions for the next assignment, they are provided in next-README). 7 | 8 | Unlike your previous testing assignments, you will be writing 9 | C code to perform the tests. In particular, you should create a file 10 | called test-power.c, which has a main function that performs the tests. 11 | 12 | If the power function passes all test cases, your program should exit 13 | with EXIT_SUCCESS. If the power function fails any test case, your program 14 | should exit with EXIT_FAILURE. Note that your program's exit status is the 15 | return value from main, if main returns. However, you can make your program 16 | exit immediately (wherever it is) by calling exit, passing in either EXIT_SUCCESS 17 | or EXIT_FAILURE, e.g., 18 | 19 | exit(EXIT_FAILURE); 20 | 21 | A few notes about doing this assignment: 22 | (1) You will want to write the prototype for power in your test-power.c 23 | file before you call power, to let the compiler know the signature 24 | of this function, and that the implementation will be found elsewhere. 25 | (2) One correct and many broken implementations of power are provided 26 | in the form of compiled object files in /usr/local/l2p/power/. 27 | Of these, power.o is correct, and the others are broken. 28 | (3) We have provided run_all.sh, which iterates over the object 29 | files in /usr/local/l2p/power, and compiles your test code 30 | and links it with each object file. It will make sure that 31 | the correct implementation passes all of your test cases, 32 | and that each broken implementation fails at least one test case. 33 | (4) When I did this, I wrote the following helper function: 34 | void run_check(unsigned x, unsigned y, unsigned expected_ans) 35 | which calls power, checks that the result is expected_ans, 36 | and if not, prints a message and calls exit(EXIT_FAILURE). 37 | (5) You need your own way to provide the expected answers. 38 | You might come up with them by hand, or find some other 39 | means to produce them. However, there is no direct way 40 | to invoke the correct implementation. 41 | (6) As before, these broken implementations were created by making 42 | small modifications to the correct implementation. All of them 43 | can be found by reasonably crafted tests. The one that is likely 44 | to be the most tricky arises due to the programmer using a variable 45 | of an incorrect type somewhere (hint)... 46 | You should submit test-power.c when you are finished. 47 | -------------------------------------------------------------------------------- /22_tests_power/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Wed Aug 8 00:06:51 UTC 2018 2 | **Testing broken implementation 10 ** 3 | ------------------------------------- 4 | 5 | 6 | **Testing broken implementation 11 ** 7 | ------------------------------------- 8 | 9 | 10 | **Testing broken implementation 1 ** 11 | ------------------------------------- 12 | 13 | 14 | **Testing broken implementation 2 ** 15 | ------------------------------------- 16 | 17 | 18 | **Testing broken implementation 3 ** 19 | ------------------------------------- 20 | 21 | 22 | **Testing broken implementation 4 ** 23 | ------------------------------------- 24 | 25 | 26 | **Testing broken implementation 5 ** 27 | ------------------------------------- 28 | 29 | 30 | **Testing broken implementation 6 ** 31 | ------------------------------------- 32 | 33 | 34 | **Testing broken implementation 7 ** 35 | ------------------------------------- 36 | 37 | 38 | **Testing broken implementation 8 ** 39 | ------------------------------------- 40 | 41 | 42 | **Testing broken implementation 9 ** 43 | ------------------------------------- 44 | 45 | 46 | **Testing correct implementation ** 47 | ------------------------------------- 48 | 49 | 50 | All tests were ok. 51 | 52 | 53 | Overall Grade: PASSED 54 | -------------------------------------------------------------------------------- /22_tests_power/next-README: -------------------------------------------------------------------------------- 1 | Do problem 7.6 in AoP: 2 | 3 | Write a recursive function unsigned power(unsigned x, unsigned y) which computes x 4 | to the y power. Note that while 0 to the 0 is undefined in mathematics, 5 | we specify that 0 to the 0 shall be 1 for this function. 6 | 7 | Save it into a file called power.c 8 | 9 | You MUST use recursion (no iteration). 10 | 11 | 12 | Use your test-power.c from your previous assignment to test your implementation 13 | of power. You can symlink the file from the previous assignment into this directory: 14 | 15 | ln -s ../16_tests_power/test-power.c ./ 16 | 17 | You should then write a Makefile which will compile each C file separately 18 | to an object file, and like them together. 19 | 20 | 21 | -------------------------------------------------------------------------------- /22_tests_power/run_all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in /usr/local/l2p/power/power*.o 4 | do 5 | test=`basename $i | sed 's/power//' | sed 's/.o//'` 6 | if [ "$test" == "" ] 7 | then 8 | echo "**Testing correct implementation **" 9 | else 10 | echo "**Testing broken implementation ${test} **" 11 | fi 12 | echo "-------------------------------------" 13 | echo "" 14 | gcc -o test-power test-power.c $i 15 | if [ "$?" != "0" ] 16 | then 17 | echo "Could not compile test-power.c with $i" > /dev/stderr 18 | exit 1 19 | fi 20 | ./test-power 21 | if [ "$?" != 0 ] 22 | then 23 | if [ "$test" == "" ] 24 | then 25 | echo "Your test program falsely failed the correct implementation!" > /dev/stderr 26 | exit 1 27 | fi 28 | else 29 | if [ "$test" != "" ] 30 | then 31 | echo "Your test program did not identify $i as broken!" > /dev/stderr 32 | exit 1 33 | fi 34 | fi 35 | echo "" 36 | done 37 | -------------------------------------------------------------------------------- /22_tests_power/test-power.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | 6 | 7 | unsigned power (unsigned x, unsigned y); 8 | 9 | int main(void){ 10 | 11 | unsigned ans ; 12 | 13 | ans = power(0,0); 14 | if (ans != 1) return EXIT_FAILURE; 15 | 16 | ans = power(5,0); 17 | if (ans != 1) return EXIT_FAILURE; 18 | 19 | ans = power(0,5); 20 | if (ans != 0) return EXIT_FAILURE; 21 | 22 | ans = power(5,2); 23 | if (ans != 25) return EXIT_FAILURE; 24 | 25 | 26 | ans = power(2*3,2); 27 | 28 | if (ans != 36) return EXIT_FAILURE; 29 | ans = power(5,1); 30 | if (ans != 5) return EXIT_FAILURE; 31 | 32 | ans = power(2,10); 33 | if (ans != 1024) return EXIT_FAILURE; 34 | 35 | ans = power(10,2); 36 | if (ans != 100) return EXIT_FAILURE; 37 | 38 | ans = power(10,5); 39 | if (ans !=100000)return EXIT_FAILURE; 40 | 41 | ans = power(1,0); 42 | if (ans !=1)return EXIT_FAILURE; 43 | 44 | 45 | ans = power(0,1); 46 | if (ans !=0)return EXIT_FAILURE; 47 | 48 | 49 | 50 | 51 | 52 | return EXIT_SUCCESS; 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /23_power_rec/README: -------------------------------------------------------------------------------- 1 | Write a recursive function unsigned power(unsigned x, unsigned y) which computes x 2 | to the y power. Note that while 0 to the 0 is undefined in mathematics, 3 | we specify that 0 to the 0 shall be 1 for this function. 4 | 5 | Save it into a file called power.c 6 | 7 | You MUST use recursion (no iteration). 8 | 9 | 10 | Use your test-power.c from your previous assignment to test your implementation 11 | of power. You can symlink the file from the previous assignment into this directory: 12 | 13 | ln -s ../22_tests_power/test-power.c ./ 14 | 15 | You should then write a Makefile which will compile each C file separately 16 | to an object file, and like them together. 17 | 18 | 19 | -------------------------------------------------------------------------------- /23_power_rec/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Wed Aug 8 11:06:07 UTC 2018 2 | Attempting to compile power.c 3 | Attempting to compile power.o with our main 4 | Checking for unsigned power (unsigned x, unsigned y) 5 | Found on line 3, column 1 6 | Checking for no iteration (do, while, for) 7 | Checking that power is recursive 8 | 0^0 was Correct 9 | 0^1 was Correct 10 | 0^2 was Correct 11 | 0^6 was Correct 12 | 0^8 was Correct 13 | 0^11 was Correct 14 | 1^0 was Correct 15 | 1^1 was Correct 16 | 1^2 was Correct 17 | 1^6 was Correct 18 | 1^8 was Correct 19 | 1^11 was Correct 20 | 4^0 was Correct 21 | 4^1 was Correct 22 | 4^2 was Correct 23 | 4^6 was Correct 24 | 4^8 was Correct 25 | 4^11 was Correct 26 | 5^0 was Correct 27 | 5^1 was Correct 28 | 5^2 was Correct 29 | 5^6 was Correct 30 | 5^8 was Correct 31 | 5^11 was Correct 32 | 9^0 was Correct 33 | 9^1 was Correct 34 | 9^2 was Correct 35 | 9^6 was Correct 36 | 9^8 was Correct 37 | 9^11 was Correct 38 | 12^0 was Correct 39 | 12^1 was Correct 40 | 12^2 was Correct 41 | 12^6 was Correct 42 | 12^8 was Correct 43 | 12^11 was Correct 44 | 45 | Overall Grade: A 46 | -------------------------------------------------------------------------------- /23_power_rec/power.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | unsigned power (unsigned x, unsigned y){ 4 | 5 | if (y==0) return 1; 6 | 7 | if (x==0) return 0; 8 | 9 | return x*power ( x, y-1); 10 | } 11 | -------------------------------------------------------------------------------- /24_read_arr3/README: -------------------------------------------------------------------------------- 1 | Execute the code in test.c by hand, and place the resulting 2 | output in a file called "answer.txt". 3 | As always, compile and run test.c to check your work, and submit. 4 | -------------------------------------------------------------------------------- /24_read_arr3/answer.txt: -------------------------------------------------------------------------------- 1 | x = 1 2 | *p = 7 3 | **q= 4 4 | 1 5 | 2 6 | 3 7 | 99 8 | 5 9 | 6 10 | 42 11 | 8 12 | 9 13 | *q=9 14 | -------------------------------------------------------------------------------- /24_read_arr3/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 18 00:00:21 UTC 2018 2 | Your file matched the expected output 3 | Your output matched what we expected 4 | 5 | Overall Grade: A 6 | -------------------------------------------------------------------------------- /24_read_arr3/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int* aFunction(int x, int *p, int ** q) { 5 | printf("x = %d\n", x); 6 | printf("*p = %d\n", *p); 7 | printf("**q= %d\n", **q); 8 | *p = 42; 9 | **q = 99; 10 | *q = &p[1]; 11 | return &p[2]; 12 | } 13 | 14 | int main (void) { 15 | int anArray[3][3] = { {1,2,3}, 16 | {4,5,6}, 17 | {7,8,9} }; 18 | 19 | int * p = anArray[1]; 20 | int * q = aFunction(anArray[0][0], 21 | anArray[2], 22 | &p); 23 | for (int i =0; i < 3; i++) { 24 | for (int j = 0; j < 3; j++) { 25 | printf("%d\n", anArray[i][j]); 26 | } 27 | } 28 | printf("*q=%d\n", *q); 29 | 30 | return EXIT_SUCCESS; 31 | } 32 | -------------------------------------------------------------------------------- /25_break_encr/Makefile: -------------------------------------------------------------------------------- 1 | breaker: breaker.c 2 | gcc -o breaker breaker.c 3 | -------------------------------------------------------------------------------- /25_break_encr/README: -------------------------------------------------------------------------------- 1 | In the last problem, you saw an implementation of a simple 2 | "encryption" program. In this problem, you will write a program 3 | that breaks that encryption---that is, it will take as input 4 | a text file encrypted with the encryption program you just 5 | saw, and prints out the key used to encrypt it! 6 | 7 | Breaking the Caesar Cipher uses a technique called "frequency counting." 8 | This technique relies on the fact that the distribution of letters 9 | in the English alphabet is far from uniform: 'e' is by far the most 10 | common letter (~13%), followed by 't' (9%), and 'a' (8%). Note 11 | that the average frequency is 100/26 ~= 4%. 12 | 13 | This frequency distribution means that if you know (or suspect) 14 | that a file contains English text encrypted with a Caesar Cipher, 15 | you can simply count the frequency of all letters in it, and guess 16 | that the letter which occurs most often is 'e'. Once you know 17 | which letter is 'e', you can backsolve for the key and decrypt 18 | the file. Note that in practice this requires a large enough 19 | text that "the law of large numbers" applies---and while it is 20 | not guaranteed to work, it typically does. 21 | 22 | 23 | Requirements: 24 | ============= 25 | 26 | - Your program will take one command line argument: the name 27 | of a file to read as input 28 | - Your program will then perform frequency counting on the letters 29 | in that text file. Your program should ignore all non-letter 30 | characters (those un-modified by your problem 1 program). 31 | - Your program should use the frequency count information to 32 | determine which letter is 'e', and solve for the key. 33 | - On success, your program should print a single line of output to 34 | stdout, which should contain one decimal (base 10) integer (followed by a 35 | newline character). This number should be the encryption key used 36 | on the text. It should be in the range [0,26). That is, the 37 | number you print should obey 0 <= answer < 26. 38 | - On failure, your program should print an appropriate error 39 | message to stderr, then exit with EXIT_FAILURE. 40 | - Provide a Makefile which compiles your program into 41 | a binary called "breaker" 42 | Hints: 43 | - Divide this problem into sub-problems. You should probably 44 | write at least two functions (other than main) to do this. 45 | - Think about how you want to represent your data. An array 46 | might be handy somewhere. 47 | - You may find a function you wrote in a previous classwork 48 | useful for this assignment. Feel free to use or adapt it. 49 | - Remember that everything (including characters) are numbers. 50 | You might look back at the "Read encryption" assignment 51 | to see an example of doing math on characters. 52 | -------------------------------------------------------------------------------- /25_break_encr/breaker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int findkey(FILE *f){ 6 | int arr[26]={0}; 7 | int c; 8 | 9 | while((c=fgetc(f)) != EOF){ 10 | if (isalpha(c)) { 11 | c=tolower(c); 12 | c -= 'a'; 13 | arr[c] ++; 14 | } 15 | } 16 | int max=0; 17 | int j=0; 18 | for (int i=0 ;i<26; i++){ 19 | if(arr[i] > max){ 20 | max=arr[i]; 21 | j=i;} 22 | } 23 | int k = 0; 24 | if (j >= ('e' - 'a')) k=j-('e'-'a') ; 25 | else k=(26-('e'-'a'))+ j; 26 | return k ; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | int main(int argc,char** argv){ 34 | if (argc != 2 ){ 35 | fprintf(stderr,"not enough arguments\n"); 36 | return EXIT_FAILURE; 37 | } 38 | 39 | FILE * f= fopen(argv[1] ,"r"); 40 | 41 | if (f == NULL) { 42 | perror("Could not open file"); 43 | return EXIT_FAILURE; 44 | } 45 | 46 | int key= findkey(f); 47 | printf("%d\n",key); 48 | 49 | if (fclose(f) != 0) { 50 | perror("Failed to close the input file!"); 51 | return EXIT_FAILURE; 52 | } 53 | 54 | return EXIT_SUCCESS; 55 | 56 | } 57 | 58 | -------------------------------------------------------------------------------- /25_break_encr/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 18 10:59:04 UTC 2018 2 | Attempting to compile breaker.c 3 | gcc -o breaker breaker.c 4 | testcase1 passed 5 | ################################################# 6 | testcase2: 7 | testcase2 passed 8 | ################################################# 9 | testcase3: 10 | testcase3 passed 11 | ################################################# 12 | testcase4: 13 | testcase4 passed 14 | 15 | Overall Grade: A 16 | -------------------------------------------------------------------------------- /26_tests_matrix_input/README: -------------------------------------------------------------------------------- 1 | For this assignment, you will be writing test cases for your next 2 | assignment. As usual, the instructions for the next assignment are 3 | in next-README. 4 | 5 | For this assignment, we have created compiled binaries (in 6 | /usr/local/l2p/rot_matrix/ where rotateMatrix is correct, 7 | and the numbered ones are each broken in some way). 8 | 9 | As with 09_testing2, you will write a file called tests.txt, 10 | which will list the command line arguments you want to use to 11 | run the programs. However, unlike 09_testing2, you will 12 | also want to create a wide variety of input files. You can 13 | name them anything you want, as long as you save them in the 14 | current (20_tests_matrix_input) directory, and submit them 15 | along with tests.txt. 16 | 17 | As usual, we have provided run_all.sh 18 | 19 | Hint 1: think about various error cases that the programmer 20 | might have forgotten! 21 | 22 | Hint 2: The trickiest of these is one in which the programmer 23 | did not pay attention to a rather subtle, but common mistake 24 | pointed out in AOP! 25 | 26 | Hint 3: If you find yourself needing to create an input 27 | file with non-typable/non-printable characters in it, 28 | you will want to do a few things. 29 | 30 | First, (after you have the file you want to edit open), you 31 | will want to force emacs to change the encoding it uses 32 | (so that it won't try to rewrite things in Unicode, for example): 33 | 34 | M-x revert-buffer-with-coding-system 35 | raw-text 36 | 37 | Once you have done this, you can do 38 | 39 | M-x hexl-mode 40 | 41 | to put emacs in hex editor mode. 42 | 43 | Then you will see hex values on the left, and their printable 44 | interpreations (or . for non-printable characters) on the right. 45 | Move the point to where you want to put a particular value, and do 46 | 47 | C-M-x 48 | 49 | then type the hex value (one byte, so two hex digits) that you want, 50 | and hit enter. It will overwrite the current character with that value. 51 | Then you can save the file. 52 | -------------------------------------------------------------------------------- /26_tests_matrix_input/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 19 23:00:30 UTC 2018 2 | Checking rotateMatrix1 3 | Your tests identified the problem with rotateMatrix1 4 | Checking rotateMatrix2 5 | Your tests identified the problem with rotateMatrix2 6 | Checking rotateMatrix3 7 | Your tests identified the problem with rotateMatrix3 8 | Checking rotateMatrix4 9 | Your tests identified the problem with rotateMatrix4 10 | Checking rotateMatrix5 11 | Your tests identified the problem with rotateMatrix5 12 | Checking rotateMatrix6 13 | Your tests identified the problem with rotateMatrix6 14 | Checking rotateMatrix7 15 | Your tests identified the problem with rotateMatrix7 16 | Checking rotateMatrix8 17 | Your tests identified the problem with rotateMatrix8 18 | Your tests identified problems with all broken programs 19 | 20 | Overall Grade: PASSED 21 | -------------------------------------------------------------------------------- /26_tests_matrix_input/input.txt: -------------------------------------------------------------------------------- 1 | 1 1 2 2 3 3 4 4 5 5 2 2 | 1 1 2 2 3 3 4 4 5 5 6 3 | 1 1 2 2 3 3 4 4 5 5 6 4 | 1 1 2 2 3 3 4 4 5 5 6 5 | 1 1 2 2 3 3 4 4 5 5 6 6 | 1 1 2 2 3 3 4 4 5 5 6 7 | 1 1 2 2 3 3 4 4 5 5 6 8 | 1 1 2 2 3 3 4 4 5 5 6 9 | 1 1 2 2 3 3 4 4 5 5 6 10 | 1 1 2 2 3 3 4 4 5 5 6 11 | -------------------------------------------------------------------------------- /26_tests_matrix_input/input1.txt: -------------------------------------------------------------------------------- 1 | 1234567890 2 | 1234567890 3 | 1234567890 4 | 1234567890 5 | 1234567890 6 | 1234567890 7 | 1234567890 8 | 1234567890 9 | 1234567890 10 | 1234567890 11 | -------------------------------------------------------------------------------- /26_tests_matrix_input/input2.txt: -------------------------------------------------------------------------------- 1 | 1 * # @ !5 ~6 7 8 9 0 2 | 1 2 3 4 5 6 7 8 99999999999 0 3 | 1 2 3 4 5 6 7 8 9 0 4 | 1 2 3 4 5 6 7 8 9 0 5 | 1 2 3 4 5 6 7 8 9 0 6 | 1 2 3 4 5 6 7 8 9 0 7 | 1 2 3 4 5 6 7 8 9 0 8 | 1 2 3 4 5 6 7 8 9 0 9 | 1 2 3 4 5 6 7 8 9 0 10 | 1 2 3 4 5 6 7 8 9 0 11 | -------------------------------------------------------------------------------- /26_tests_matrix_input/input3.txt: -------------------------------------------------------------------------------- 1 | 1234567890 2 | asdfghjkl; 3 | dfgh"\n"gh 4 | zxcvbnm,./ 5 | zxcvbnm,./ 6 | zxcvbnm,./ 7 | zxcvbnm,./ 8 | zxcvbnm,./ 9 | zxcvbnm,./ 10 | zxcvbnm,./ 11 | 12 | -------------------------------------------------------------------------------- /26_tests_matrix_input/input4.txt: -------------------------------------------------------------------------------- 1 | 2 | asdfghjkl; 3 | zxcvbnm,./ 4 | "%s"tYuiop 5 | asdftyuikl 6 | xcvbhgfder 7 | jdjdjdhdye 8 | rrrrrrrrrr 9 | hehehehehe 10 | ssssssssss 11 | aaaaaaaaaa 12 | 13 | -------------------------------------------------------------------------------- /26_tests_matrix_input/input5.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/26_tests_matrix_input/input5.txt -------------------------------------------------------------------------------- /26_tests_matrix_input/next-README: -------------------------------------------------------------------------------- 1 | In a previous function, you write a function 2 | 3 | void rotate(char matrix[10][10]) 4 | 5 | which performed 90 degree clockwise rotation of a 10x10 6 | matrix. In that assignment, we gave you a compiled 7 | object file which read the input matrix from a file, 8 | called your function to do the rotation, and then 9 | printed the result to the screen. 10 | 11 | In this assignment, you will write the code that we 12 | previously gave you, making the complete program on your own. 13 | You are encouraged to make use of your previously written 14 | rotate function in this assignment. 15 | 16 | For this problem, you will be writing a program which 17 | performs a 90 degree clockwise rotation of a 10x10 matrix. 18 | There is nothing special about a 10x10 matrix---I just need 19 | to fix the matrix size, since we have not learned about dynamic 20 | memory allocation yet, so we do not have the knowledge needed 21 | to read in any size of matrix. 22 | 23 | To keep the input processing simple, the matrix will be a matrix 24 | of characters (so you will have something like 25 | char matrix[10][10] 26 | in your program), which will be read from a file. Each line 27 | in the input file should contain 10 characters (plus a newline). 28 | 29 | Requirements: 30 | ============= 31 | - Create a file called rotateMatrix.c 32 | - Your program will take one command line argument, a string 33 | specifying the input file to read. 34 | - The input file should contain 10 lines, each of which 35 | have 10 (non-newline) characters (plus a newline). 36 | - Your program should then rotate this 90 degrees clockwise, 37 | and print the result on stdout. 38 | Note that sample.txt provides sample input, and 39 | sample.out provides sample output. 40 | - If there are any errors, your program should print an 41 | appropriate message to stderr, and exit with EXIT_FAILURE. 42 | 43 | Hints: 44 | ------ 45 | - You may find the strchr useful for error checking that 46 | you read a proper line (10 non-newline characters, then a newline). 47 | -------------------------------------------------------------------------------- /26_tests_matrix_input/run_all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | run_test() { 3 | prog="$1" 4 | testfile="$2" 5 | IFS=$'\n' 6 | for line in `cat $testfile | sed 's/^$/ /'` 7 | do 8 | IFS=" " correct=`/usr/local/l2p/rot_matrix/rotateMatrix $line 2>&1` 9 | IFS=" " broken=`$prog $line 2>&1` 10 | if [ "$broken" != "$correct" ] 11 | then 12 | return 0 13 | fi 14 | done 15 | return 1 16 | } 17 | 18 | for i in /usr/local/l2p/rot_matrix/rotateMatrix* 19 | do 20 | if [ "$i" != "/usr/local/l2p/rot_matrix/rotateMatrix" ] 21 | then 22 | echo "Checking `basename $i`" 23 | run_test $i tests.txt 24 | x="$?" 25 | if [ "$x" != "0" ] 26 | then 27 | echo "***Your tests failed to show that `basename $i` was broken!" 28 | fi 29 | fi 30 | done 31 | -------------------------------------------------------------------------------- /26_tests_matrix_input/tests.txt: -------------------------------------------------------------------------------- 1 | inpyt7.txt 2 | input5.txt 3 | input1.txt 4 | input2.txt 5 | input3.txt 6 | 7 | input.tx 8 | 2 input.txt 9 | input4.txt 10 | -------------------------------------------------------------------------------- /27_matrix_input/README: -------------------------------------------------------------------------------- 1 | In a previous function, you write a function 2 | 3 | void rotate(char matrix[10][10]) 4 | 5 | which performed 90 degree clockwise rotation of a 10x10 6 | matrix. In that assignment, we gave you a compiled 7 | object file which read the input matrix from a file, 8 | called your function to do the rotation, and then 9 | printed the result to the screen. 10 | 11 | In this assignment, you will write the code that we 12 | previously gave you, making the complete program on your own. 13 | You are encouraged to make use of your previously written 14 | rotate function in this assignment. 15 | 16 | For this problem, you will be writing a program which 17 | performs a 90 degree clockwise rotation of a 10x10 matrix. 18 | There is nothing special about a 10x10 matrix---I just need 19 | to fix the matrix size, since we have not learned about dynamic 20 | memory allocation yet, so we do not have the knowledge needed 21 | to read in any size of matrix. 22 | 23 | To keep the input processing simple, the matrix will be a matrix 24 | of characters (so you will have something like 25 | char matrix[10][10] 26 | in your program), which will be read from a file. Each line 27 | in the input file should contain 10 characters (plus a newline). 28 | 29 | Requirements: 30 | ============= 31 | - Create a file called rotateMatrix.c 32 | - Your program will take one command line argument, a string 33 | specifying the input file to read. 34 | - The input file should contain 10 lines, each of which 35 | have 10 (non-newline) characters (plus a newline). 36 | - Your program should then rotate this 90 degrees clockwise, 37 | and print the result on stdout. 38 | Note that sample.txt provides sample input, and 39 | sample.out provides sample output. 40 | - If there are any errors, your program should print an 41 | appropriate message to stderr, and exit with EXIT_FAILURE. 42 | 43 | Hints: 44 | ------ 45 | - You may find the strchr useful for error checking that 46 | you read a proper line (10 non-newline characters, then a newline). 47 | -------------------------------------------------------------------------------- /27_matrix_input/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Mon Aug 20 02:38:14 UTC 2018 2 | Attempting to compile rotateMatrix.c 3 | testcase1 passed 4 | ################################################# 5 | testcase2: NonExistentFile 6 | (should indicate an error) 7 | testcase2 passed 8 | ################################################# 9 | testcase3: blank.txt 10 | (should indicate an error) 11 | testcase3 passed 12 | ################################################# 13 | testcase4: short-line.txt 14 | (should indicate an error) 15 | testcase4 passed 16 | ################################################# 17 | testcase5: short-file.txt 18 | (should indicate an error) 19 | testcase5 passed 20 | ################################################# 21 | testcase6: long-line.txt 22 | (should indicate an error) 23 | testcase6 passed 24 | ################################################# 25 | testcase7: long-file.txt 26 | (should indicate an error) 27 | testcase7 passed 28 | ################################################# 29 | testcase8: long-line-2.txt 30 | (should indicate an error) 31 | testcase8 passed 32 | ################################################# 33 | testcase9: normal1.txt normal2.txt 34 | (should indicate an error) 35 | testcase9 passed 36 | ################################################# 37 | testcase10: normal1.txt 38 | Your file matched the expected output 39 | testcase10 passed 40 | ################################################# 41 | testcase10: normal2.txt 42 | Your file matched the expected output 43 | testcase10 passed 44 | ################################################# 45 | testcase10: normal3.txt 46 | Your file matched the expected output 47 | testcase10 passed 48 | ################################################# 49 | testcase10: eof.txt 50 | Your file matched the expected output 51 | testcase10 passed 52 | 53 | Overall Grade: A 54 | -------------------------------------------------------------------------------- /27_matrix_input/rotateMatrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int checkinput(FILE * f){ 6 | int c=0; 7 | int col =0; 8 | c=fgetc(f); 9 | while (!(c == EOF)){ 10 | for(int i=0 ;i<10;i++){ 11 | if(c == '\n'){ 12 | fprintf(stderr,"char error"); 13 | return 0; 14 | } 15 | c=fgetc(f); 16 | } 17 | if (c != '\n'){ 18 | fprintf(stderr,"row err"); 19 | return 0; 20 | } 21 | c=fgetc(f); 22 | col ++; 23 | } 24 | if(col == 10) return 1; 25 | else { 26 | fprintf(stderr,"column err"); 27 | return 0;} 28 | 29 | } 30 | 31 | void rotate(char matrix[10][10]){ 32 | for (int i = 0; i < 10 ; i++) { 33 | for (int j = i; j < 10; j++) { 34 | char temp =matrix[i][j]; 35 | matrix[i][j]=matrix[j][i]; 36 | matrix[j][i]=temp; 37 | } 38 | } 39 | for (int i = 0; i < 10 ; i++) { 40 | for (int j = 0; j < 5; j++) { 41 | char temp =matrix[i][j]; 42 | matrix[i][j]=matrix[i][9-j]; 43 | matrix[i][9-j]=temp; 44 | } 45 | } 46 | return ; 47 | } 48 | 49 | int main(int argc,char** argv){ 50 | if (argc != 2){ 51 | fprintf(stderr,"not correct number of argument \n"); 52 | return EXIT_FAILURE; 53 | } 54 | FILE * f =fopen(argv[1],"r"); 55 | if (f == NULL) { 56 | perror("Could not open file"); 57 | return EXIT_FAILURE; 58 | } 59 | if (!(checkinput(f))) return EXIT_FAILURE; 60 | 61 | if (fclose(f) != 0) { 62 | perror("Failed to close the input file!"); 63 | return EXIT_FAILURE; } 64 | f =fopen(argv[1],"r"); 65 | if (f == NULL) { 66 | perror("Could not open file"); 67 | return EXIT_FAILURE; 68 | } 69 | 70 | char matrix[10][10]; 71 | int c =0; 72 | for (int i=0;i<10;i++){ 73 | for (int j=0;j<10;j++){ 74 | c=fgetc(f); 75 | if (c == '\n') c=fgetc(f); 76 | matrix[i][j]=c; 77 | }} 78 | 79 | rotate(matrix); 80 | 81 | for (int x=0;x<10;x++){ 82 | for (int y=0;y<10;y++){ 83 | printf("%c",matrix[x][y]); 84 | } 85 | printf("\n"); 86 | } 87 | 88 | if (fclose(f) != 0) { 89 | perror("Failed to close the input file!"); 90 | return EXIT_FAILURE; } 91 | 92 | return EXIT_SUCCESS; 93 | 94 | } 95 | -------------------------------------------------------------------------------- /27_matrix_input/sample.out: -------------------------------------------------------------------------------- 1 | CH.....*a0 2 | oe....*.b1 3 | dl...*..c2 4 | il..*...d3 5 | no.*....e4 6 | gW*.....f5 7 | o......g6 8 | Fr......h7 9 | ul......i8 10 | nd......j9 11 | -------------------------------------------------------------------------------- /27_matrix_input/sample.txt: -------------------------------------------------------------------------------- 1 | 0123456789 2 | abcdefghij 3 | *......... 4 | .*........ 5 | ..*....... 6 | ...*...... 7 | ....*..... 8 | .....*.... 9 | HelloWorld 10 | Coding Fun 11 | -------------------------------------------------------------------------------- /28_fix_vg_encr/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-ggdb3 -Wall -Werror -std=gnu99 -pedantic 2 | encrypt: encrypt.c 3 | gcc $(CFLAGS) -o encrypt encrypt.c 4 | clean: 5 | rm -f encrypt *~ 6 | -------------------------------------------------------------------------------- /28_fix_vg_encr/README: -------------------------------------------------------------------------------- 1 | In a previous problem, you executed a Ceaser Cipher encryption program by hand. 2 | That program read an input file, and wrote to standard output. 3 | 4 | In this problem, you are going to work on a slightly modified version of that program, 5 | which is is included in encrypt.c. The major difference between this version 6 | of the program and the previous one is that rather than printing to stdout, 7 | this program appends ".enc" to the input file name, and writes its output to that 8 | file (for example, if your input file is called "input.txt", it will write to "input.txt.enc"). 9 | Additionally, rather than using fgetc to read one character at a time, this version 10 | of the program uses getline to read an entire line at a time. 11 | 12 | This program has the basic algorithm correct, but makes a variety of errors---all of which 13 | valgrind will detect. Your job for this problem is to fix the program by making it 14 | valgrind cleanly. 15 | 16 | Hint: Start from the first valgrind error. Read and understand the error. It will 17 | tell you on what line of code valgrind detected the problem. Understand the 18 | error, and why it is occuring (drawing some pictures will likely help here). 19 | Fix the error, make, and re-run. Repeat the process until all valgrind errors are 20 | gone. Don't forget that gdb may be useful as well. 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /28_fix_vg_encr/encrypt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void encrypt(FILE * f, int key, FILE * outfile){ 7 | char * line=NULL; 8 | size_t sz=0; 9 | while (getline(&line,&sz, f) >= 0) { 10 | char * ptr = line; 11 | while (*ptr != '\0') { 12 | int c = *ptr; 13 | if (isalpha(c)) { 14 | c = tolower(c); 15 | c -= 'a'; 16 | c += key; 17 | c %= 26; 18 | c += 'a'; 19 | } 20 | *ptr = c; 21 | ptr++; 22 | } 23 | fprintf(outfile, "%s", line); 24 | 25 | } 26 | free(line); 27 | } 28 | 29 | int main(int argc, char ** argv) { 30 | if (argc != 3) { 31 | fprintf(stderr,"Usage: encrypt key inputFileName\n"); 32 | return EXIT_FAILURE; 33 | } 34 | int key = atoi(argv[1]); 35 | if (key == 0) { 36 | fprintf(stderr,"Invalid key (%s): must be a non-zero integer\n", argv[1]); 37 | return EXIT_FAILURE; 38 | } 39 | FILE * f = fopen(argv[2], "r"); 40 | if (f == NULL) { 41 | perror("Could not open file"); 42 | return EXIT_FAILURE; 43 | } 44 | //outfileNAme is argv[2] + ".txt", so add 4 to its length. 45 | char * outFileName = malloc((strlen(argv[2]) + 4) * sizeof(*outFileName)); 46 | strcpy(outFileName, argv[2]); 47 | outFileName=realloc(outFileName, (strlen(argv[2])+14)*(sizeof(*outFileName))); 48 | strcat(outFileName, ".enc"); 49 | FILE * outFile = fopen(outFileName, "w"); 50 | encrypt(f,key, outFile); 51 | if (fclose(outFile) != 0) { 52 | perror("Failed to close the input file!"); 53 | return EXIT_FAILURE; 54 | } 55 | if (fclose(f) != 0) { 56 | perror("Failed to close the input file!"); 57 | return EXIT_FAILURE; 58 | } 59 | free(outFileName); 60 | 61 | return EXIT_SUCCESS; 62 | } 63 | -------------------------------------------------------------------------------- /28_fix_vg_encr/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Fri Aug 24 14:49:14 UTC 2018 2 | Attempting to compile encrypt.c 3 | testcase1: 4 | your output was correct 5 | - Valgrind was clean (no errors, no memory leaks) 6 | valgrind was clean 7 | ################################################# 8 | testcase2: 9 | - Valgrind was clean (no errors, no memory leaks) 10 | valgrind was clean 11 | ################################################# 12 | testcase3: 13 | - Valgrind was clean (no errors, no memory leaks) 14 | valgrind was clean 15 | ################################################# 16 | testcase4: 17 | - Valgrind was clean (no errors, no memory leaks) 18 | valgrind was clean 19 | ################################################# 20 | testcase5: 21 | - Valgrind was clean (no errors, no memory leaks) 22 | valgrind was clean 23 | ################################################# 24 | testcase6: 25 | - Valgrind was clean (no errors, no memory leaks) 26 | valgrind was clean 27 | 28 | Overall Grade: A 29 | -------------------------------------------------------------------------------- /28_fix_vg_encr/input.txt: -------------------------------------------------------------------------------- 1 | An example 2 | Of the input. 3 | -------------------------------------------------------------------------------- /28_fix_vg_encr/input.txt.enc: -------------------------------------------------------------------------------- 1 | dq hadpsoh 2 | ri wkh lqsxw. 3 | -------------------------------------------------------------------------------- /29_outname/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 2 | OBJS=outname.o outname_test.o 3 | PROGRAM=outname_test 4 | 5 | $(PROGRAM): $(OBJS) 6 | gcc $(CFLAGS) -o $@ $(OBJS) 7 | 8 | %.o: %.c outname.h 9 | gcc -c $(CFLAGS) $< 10 | 11 | clean: 12 | rm -f $(OBJS) $(PROGRAM) *~ -------------------------------------------------------------------------------- /29_outname/README: -------------------------------------------------------------------------------- 1 | In a future assignment, you are going to write a program which reads 2 | some input, does some manipulations, and produces output in another file. 3 | Right now, you are going to write a function which will be useful to you 4 | in that assignment, which takes theinput filename, and produces the output 5 | file name. The output file name should be the same as the input file name 6 | with ".counts" on the end. So, for example, if teh input filename (inputName) 7 | is "input.txt", your function should return "input.txt.counts". 8 | 9 | Note that your function needs to use malloc to allocate memory to hold 10 | the string that it will return as its answer. 11 | 12 | Your function should have the following signature: 13 | 14 | char * computeOutputFileName(const char * inputName) 15 | 16 | and you should write it in the outname.c file. 17 | You can make and test with the main function found in outname_test.c. 18 | As always, be sure your program valgrinds cleanly. 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /29_outname/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Fri Aug 24 15:32:30 UTC 2018 2 | Attempting to compile: 3 | ################################################# 4 | testcase1: 5 | Your output is correct 6 | - Valgrind was clean (no errors, no memory leaks) 7 | valgrind was clean 8 | ################################################# 9 | testcase2: 10 | Your output is correct 11 | - Valgrind was clean (no errors, no memory leaks) 12 | valgrind was clean 13 | ################################################# 14 | testcase3: 15 | Your output is correct 16 | - Valgrind was clean (no errors, no memory leaks) 17 | valgrind was clean 18 | 19 | Overall Grade: A 20 | -------------------------------------------------------------------------------- /29_outname/outname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "outname.h" 5 | 6 | char * computeOutputFileName(const char * inputName) { 7 | char * outname = malloc((strlen(inputName)+4)*sizeof(*outname)); 8 | strcpy(outname , inputName); 9 | outname= realloc(outname ,(strlen(inputName)+10)*sizeof(*outname)); 10 | strcat(outname,".counts"); 11 | return outname; 12 | //WRITE ME 13 | } 14 | -------------------------------------------------------------------------------- /29_outname/outname.h: -------------------------------------------------------------------------------- 1 | #ifndef __OUTNAME_H__ 2 | #define __OUTNAME_H__ 3 | 4 | char * computeOutputFileName(const char * inputName); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /29_outname/outname_test.c: -------------------------------------------------------------------------------- 1 | #include "outname.h" 2 | #include 3 | #include 4 | 5 | 6 | #define NUM_TESTS 3 7 | int main(void) { 8 | char * testNames[NUM_TESTS] = {"input.txt", 9 | "anotherTestFileName.txt", 10 | "somethingelse"}; 11 | 12 | for (int i = 0; i < NUM_TESTS; i++) { 13 | char * outName = computeOutputFileName(testNames[i]); 14 | printf("'%s' => '%s'\n", testNames[i], outName); 15 | free(outName); 16 | } 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /30_sort_lines/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-ggdb3 -Wall -Werror -std=gnu99 -pedantic 2 | sortLines: sortLines.c 3 | gcc $(CFLAGS) -o sortLines sortLines.c 4 | 5 | clean: 6 | rm -f sortLines *~ -------------------------------------------------------------------------------- /30_sort_lines/README: -------------------------------------------------------------------------------- 1 | For this problem, you will write a program (by modifying the provided 2 | sortLines.c file) which sorts strings from input files. As we have not 3 | yet covered sorting, we have provided the code that does the acutal 4 | sorting. You can just call this function, which will sort the data. 5 | 6 | void sortData(char ** data, size_t count); 7 | 8 | The sortData function takes two arguments. The first is an array of strings. 9 | The second is the length of that array. It will re-order the elements of the 10 | array, such that they are sorted. 11 | 12 | Your task in this problem primarily centers around reading the data into 13 | the array from the input file(s), so that it can be sorted. You will also 14 | need to print the data after it is sorted, and free all of the memory you have allocated. 15 | 16 | Specifically, this program should take 0 or more command line arguments. 17 | - If no arguments (other than its own name) are provided (argc ==1), then 18 | your program should read from standard input, sort the lines of input, 19 | print the results, free memory, and exit successfully. 20 | - If 1 or more arguments are provided (argc > 1), then your program should 21 | treat each argument as an input file name. It should open that file, 22 | read all of the lines of data in it, sort the lines, print the results, 23 | free the memory, and close the file. If any errors occur, your program 24 | should print an appropriate error message and exit with EXIT_FAILURE. 25 | If no errors occur, your program should indicate its success after processing 26 | all of the files. 27 | 28 | Note that we now place *no* restriction on the length of any individual line of input. 29 | You should therefore use the getline function to read an arbitrarily long line of 30 | input, dynamically allocating space for it as needed. See the man page for getline 31 | for more details. 32 | 33 | You should make sure your program valgrinds cleanly, including no memory leaks 34 | before you submit it. As always, submit your code for grading. 35 | 36 | Hints: 37 | - Don't forget to abstract code out into smaller function. In my solution, 38 | I wrote 3 functions other than main (plus the 2 that are provided). 39 | - Don't forget to draw pictures! They are even more important 40 | as you use pointers more and more. 41 | 42 | -------------------------------------------------------------------------------- /30_sort_lines/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Fri Aug 24 18:42:00 UTC 2018 2 | Attempting to compile sortLines.c 3 | ################################################# 4 | testcase1: 5 | testcase1 passed, your program successfully indicated a failure 6 | - Valgrind was clean (no errors, no memory leaks) 7 | valgrind was clean 8 | ################################################# 9 | testcase2: 10 | Your output is correct 11 | - Valgrind was clean (no errors, no memory leaks) 12 | valgrind was clean 13 | ################################################# 14 | testcase3: 15 | Your output is correct 16 | - Valgrind was clean (no errors, no memory leaks) 17 | valgrind was clean 18 | ################################################# 19 | testcase4: 20 | Your output is correct 21 | - Valgrind was clean (no errors, no memory leaks) 22 | valgrind was clean 23 | 24 | Overall Grade: A 25 | -------------------------------------------------------------------------------- /30_sort_lines/sortLines.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | //This function is used to figure out the ordering 7 | //of the strings in qsort. You do not need 8 | //to modify it. 9 | int stringOrder(const void * vp1, const void * vp2) { 10 | const char * const * p1 = vp1; 11 | const char * const * p2 = vp2; 12 | return strcmp(*p1, *p2); 13 | } 14 | //This function will sort and print data (whose length is count). 15 | void sortData(char ** data, size_t count) { 16 | qsort(data, count, sizeof(char *), stringOrder); 17 | } 18 | 19 | int main(int argc, char ** argv) { 20 | 21 | if (argc == 0) { 22 | fprintf(stderr,"few arguments \n"); 23 | return EXIT_FAILURE; 24 | } 25 | else if (argc == 1){ 26 | //read from stdio 27 | char** arr=NULL; 28 | char* line=NULL; 29 | size_t sz=0; 30 | int i =0; 31 | while(getline(&line ,&sz,stdin)>=0){ 32 | arr=realloc(arr,(i+1)*sizeof(*arr)); 33 | arr[i]=line; 34 | line=NULL; 35 | i++; 36 | } 37 | free(line); 38 | sortData(arr, i); 39 | for(int j=0;j=0){ 57 | arr=realloc(arr,(i+1)*sizeof(*arr)); 58 | arr[i]=line; 59 | line=NULL; 60 | i++; 61 | } 62 | free(line); 63 | sortData(arr, i); 64 | for(int j=0;jboard should be such that it is indexed 43 | b->board[y][x] 44 | where y is between 0 and the height and x is between 0 and the width. 45 | 46 | - countMines: this funciton takes a board_t, and an (x,y) coordinate. 47 | It should count the mines in the 8 squares around that (x,y) 48 | coordinate, and return that count. Note that a mine may be 49 | indicated by a square on the board either being HAS_MINE or 50 | KNOWN_MINE. You can use the IS_MINE macro to test both cases: 51 | IS_MINE(b->board[ny][nx]) 52 | (where b is the board_t, and (nx,ny) are the coordinates you 53 | want to check). Be careful not to go out of bounds of the array. 54 | 55 | - checkWin: this funciton takes a board_t and see if the game has 56 | been won. The game is won when no squares are UNKNOWN. 57 | 58 | - freeBoard: This function takes a board_t and frees all memory 59 | associated with it (including the array inside of it). That is, 60 | freeBoard should undo all the allocations made by a call to makeBoard. 61 | 62 | Note: You should NOT change any of the other provided functions! 63 | 64 | 65 | Once you have these all working, you should have a playable game of 66 | minesweeper. Note that there are a few differences in game play 67 | from the "standard" game: 68 | 69 | - You select a square by entering its x (column) and y (row) coordinate. 70 | The x coordinates are listed across the top and the y are listed 71 | down the left side to reduce counting. 72 | 73 | - The game will automatically figure out the "obvious" squares: 74 | both mines and non-mined spaces. It will reveal these too you 75 | as soon as it considers them trivial to figure out. 76 | 77 | - You cannot manually mark a square that you suspect has a mine 78 | 79 | Once your code is complete, submit minesweeper.c -------------------------------------------------------------------------------- /31_minesweeper/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 25 12:48:06 UTC 2018 2 | Attempting to compile minesweeper.c 3 | ################################################# 4 | testcase1: 5 | Your output is correct 6 | - Valgrind was clean (no errors, no memory leaks) 7 | valgrind was clean 8 | ################################################# 9 | testcase2: 10 | Your output is correct 11 | - Valgrind was clean (no errors, no memory leaks) 12 | valgrind was clean 13 | ################################################# 14 | testcase3: 15 | Your output is correct 16 | - Valgrind was clean (no errors, no memory leaks) 17 | valgrind was clean 18 | ################################################# 19 | testcase4: 20 | Your output is correct 21 | - Valgrind was clean (no errors, no memory leaks) 22 | valgrind was clean 23 | 24 | Overall Grade: A 25 | -------------------------------------------------------------------------------- /32_kvs/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 2 | OBJS=kv_test.o kv.o 3 | PROGRAM=kv_test 4 | 5 | $(PROGRAM): $(OBJS) 6 | gcc $(CFLAGS) -o $@ $(OBJS) 7 | 8 | %.o: %.c kv.h 9 | gcc -c $(CFLAGS) $< 10 | 11 | clean: 12 | rm -f $(OBJS) $(PROGRAM) *~ -------------------------------------------------------------------------------- /32_kvs/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 25 23:14:27 UTC 2018 2 | Attempting to compile: 3 | ################################################# 4 | testcase1: 5 | Your output is correct 6 | - Valgrind was clean (no errors, no memory leaks) 7 | valgrind was clean 8 | ################################################# 9 | testcase2: 10 | Your output is correct 11 | - Valgrind was clean (no errors, no memory leaks) 12 | valgrind was clean 13 | ################################################# 14 | testcase3: 15 | Your output is correct 16 | - Valgrind was clean (no errors, no memory leaks) 17 | valgrind was clean 18 | 19 | Overall Grade: A 20 | -------------------------------------------------------------------------------- /32_kvs/kv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "kv.h" 5 | 6 | kvpair_t* splitPairs(char* str){ 7 | 8 | char* value1=strchr(str,'='); 9 | *value1='\0'; 10 | value1 ++; 11 | char* n=strchr(value1,'\n'); 12 | if(n != NULL) *n='\0'; 13 | 14 | kvpair_t* pair = malloc((size_t) sizeof(*pair)); 15 | pair ->key=str; 16 | pair ->value=value1; 17 | return pair; 18 | } 19 | 20 | kvarray_t * readKVs(const char * fname) { 21 | //open the file 22 | FILE * f = fopen(fname, "r"); 23 | if (f == NULL) { 24 | perror("Could not open file"); 25 | return NULL; 26 | } 27 | 28 | //read the lines 29 | char* line= NULL; 30 | size_t sz=0; 31 | kvpair_t* curr_pair = NULL; 32 | 33 | kvarray_t* kv_array= malloc((size_t)(sizeof(*kv_array))); 34 | kv_array->size = 0; 35 | kv_array->arr=NULL; 36 | 37 | while (getline(&line,&sz, f) >= 0) { 38 | curr_pair=splitPairs(line); 39 | kv_array->arr=realloc(kv_array->arr,(kv_array->size+1)*sizeof(*kv_array->arr)); 40 | kv_array->arr[kv_array->size]=curr_pair; 41 | kv_array->size ++; 42 | line=NULL; 43 | } 44 | free(line); 45 | 46 | int result=fclose(f); 47 | if (result !=0)return NULL; 48 | return kv_array; 49 | } 50 | 51 | void freeKVs(kvarray_t * pairs) { 52 | //WRITE ME 53 | for(int i=0 ;isize;i++){ 54 | 55 | free(pairs->arr[i]->key); 56 | free(pairs->arr[i]); 57 | } 58 | free(pairs->arr); 59 | free(pairs); 60 | } 61 | 62 | void printKVs(kvarray_t * pairs) { 63 | 64 | kvpair_t* curr_pair; 65 | for(int i=0;isize;i++){ 66 | curr_pair=pairs->arr[i]; 67 | printf("key = '%s' value = '%s'\n" ,curr_pair->key, curr_pair->value); 68 | } 69 | //WRITE ME 70 | } 71 | 72 | char * lookupValue(kvarray_t * pairs, const char * key) { 73 | kvpair_t* curr_pair; 74 | for(int i=0;isize;i++){ 75 | curr_pair=pairs->arr[i]; 76 | if (! strcmp(curr_pair->key,key)) return curr_pair->value; 77 | } 78 | //WRITE ME 79 | return NULL; 80 | } 81 | -------------------------------------------------------------------------------- /32_kvs/kv.h: -------------------------------------------------------------------------------- 1 | #ifndef __KV_H__ 2 | #define __KV_H__ 3 | 4 | 5 | struct _kvpair_t { 6 | //DEFINE ME! 7 | char* key; 8 | char* value; 9 | }; 10 | typedef struct _kvpair_t kvpair_t; 11 | 12 | struct _kvarray_t { 13 | //DEFINE ME! 14 | kvpair_t** arr; 15 | int size; 16 | 17 | }; 18 | typedef struct _kvarray_t kvarray_t; 19 | 20 | 21 | kvarray_t * readKVs(const char * fname); 22 | 23 | void freeKVs(kvarray_t * pairs); 24 | 25 | void printKVs(kvarray_t * pairs); 26 | 27 | char * lookupValue(kvarray_t * pairs, const char * key); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /32_kvs/kv_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "kv.h" 5 | 6 | #define NUM_LOOKUPS 5 7 | int main(void) { 8 | kvarray_t * array = readKVs("test.txt"); 9 | printf("Printing all keys\n\n"); 10 | printKVs(array); 11 | char *tests[NUM_LOOKUPS] = {"banana", "grapes", "cantaloupe", "lettuce", "orange"}; 12 | for (int i = 0; i < NUM_LOOKUPS; i++) { 13 | printf("lookupValue('%s')=%s\n", tests[i], lookupValue(array,tests[i])); 14 | } 15 | freeKVs(array); 16 | return EXIT_SUCCESS; 17 | } 18 | -------------------------------------------------------------------------------- /32_kvs/test.txt: -------------------------------------------------------------------------------- 1 | apple=red 2 | banana=yellow 3 | orange=orange 4 | grapes=purple 5 | carrot=orange 6 | eggplant=purple 7 | -------------------------------------------------------------------------------- /33_counts/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 2 | OBJS=counts.o counts_test.o 3 | PROGRAM=counts_test 4 | 5 | $(PROGRAM): $(OBJS) 6 | gcc $(CFLAGS) -o $@ $(OBJS) 7 | 8 | %.o: %.c counts.h 9 | gcc -c $(CFLAGS) $< 10 | 11 | clean: 12 | rm -f $(OBJS) $(PROGRAM) *~ -------------------------------------------------------------------------------- /33_counts/README: -------------------------------------------------------------------------------- 1 | For this problem, we will address the following tasks: 2 | 3 | - print the counts to a file 4 | - free the memory for the counts 5 | 6 | We'll note that we are not going to write the part of this program where 7 | we read the input file and compute the counts until the next problem. However, we will 8 | still want to be able to test our code. We can do this, by having a main 9 | function which constructs the counts from a hard coded set of data, skipping 10 | the details of the actual program (this is an example of a test scaffold). 11 | 12 | Our test scaffold can benefit from some functionality that (if we think a bit ahead) 13 | will be useful to abstract out into a couple functions, so we can re-use that code 14 | in the next problem. (Abstracting all of this code out into function is also good because 15 | it hides the implementation details: none of the code in the main function 16 | we provide cares what the names/types of the fields in the counts_t structure 17 | are, which you will make shortly). 18 | 19 | First, go to counts.h. Here, you will find two empty struct declarations. You will 20 | need to fill these in. The first should reflect the information about one count. 21 | That is, for some particular string, how many times have we seen it so far. 22 | The second, should have an array of the first, as well as the size of that array. 23 | You should also include a field in this struct to count unknown names. 24 | 25 | Next, you should go to counts.c, and write the four functions there. 26 | 27 | The first, createCounts should allocate memory for a counts_t structure, and initialize 28 | it to represent that nothing has been counted yet. 29 | 30 | The next function, addCount, should increment the count for the corresponding name. Note 31 | that name will be NULL in the case of something that is unknown, so your code must account 32 | for this case. 33 | 34 | The third function, printCounts takes a counts_t and prints that information to 35 | the FILE outFile. Recall from the description of the entire problem, that this 36 | function should print in the format: 37 | 38 | Captain: 1 39 | Commander: 2 40 | Lt. Commander: 1 41 | : 1 42 | 43 | These should appear in the order that the name is first added, with unknown always 44 | appearing last. 45 | 46 | ***If there are no unknown values, you should not print a line for unknown. That 47 | is, you should NEVEr print 48 | : 0 49 | 50 | 51 | Finally, you should write freeCounts, which should free all the memory associated with 52 | a counts_t. 53 | 54 | We have provided a main in countsTestc which creates a counts_t (using your createCounts 55 | function), adds some names to it (using your addCount function), prints the result 56 | to stdout (using your printCounts) function, then frees the memory (using your freeCounts). 57 | 58 | Test and debug these functions before proceeding. 59 | -------------------------------------------------------------------------------- /33_counts/counts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "counts.h" 5 | counts_t * createCounts(void) { 6 | counts_t * ans= malloc(sizeof(*ans)); 7 | ans->cntArr = NULL; 8 | ans->cntSize=0; 9 | ans->unkown=0; 10 | return ans; 11 | } 12 | void addCount(counts_t * c, const char * name) { 13 | if (name == NULL) c->unkown ++; 14 | else { 15 | one_count_t* x=NULL; 16 | int h=0; 17 | for(size_t i=0 ; i< c->cntSize ; i++){ 18 | x=c->cntArr[i]; 19 | if (! strcmp(x->str,name)) { 20 | x->cnt ++; 21 | h=1;} 22 | } 23 | if (h==0) { 24 | one_count_t *x1=NULL; 25 | x1=malloc(sizeof(*x1)); 26 | x1->str=malloc(strlen(name)+8); 27 | strcpy(x1->str ,name); 28 | x1->cnt=1; 29 | c->cntArr=realloc(c->cntArr,(c->cntSize+1)*sizeof(*c->cntArr)); 30 | c->cntArr[c->cntSize]=x1; 31 | c->cntSize ++; 32 | } 33 | } 34 | //WRITE ME 35 | } 36 | void printCounts(counts_t * c, FILE * outFile) { 37 | //WRITE ME 38 | one_count_t* x=NULL; 39 | for (size_t i=0 ;icntSize;i++){ 40 | x=c->cntArr[i]; 41 | fprintf(outFile,"%s: %zd\n",x->str,x->cnt); 42 | } 43 | if (c->unkown != 0) fprintf(outFile," : %zd\n",c->unkown); 44 | } 45 | 46 | void freeCounts(counts_t * c) { 47 | //WRITE ME 48 | for (size_t i=0 ;icntSize;i++){ 49 | free(c->cntArr[i]->str); 50 | free(c->cntArr[i]); 51 | } 52 | free(c->cntArr); 53 | free(c); 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /33_counts/counts.h: -------------------------------------------------------------------------------- 1 | #ifndef __COUNTS_H__ 2 | #define __COUNTS_H__ 3 | struct _one_count_t { 4 | //DEFINE ME 5 | char* str; 6 | size_t cnt; 7 | }; 8 | typedef struct _one_count_t one_count_t; 9 | 10 | struct _counts_t { 11 | one_count_t** cntArr; 12 | size_t cntSize; 13 | size_t unkown; 14 | 15 | //DEFINE ME 16 | }; 17 | typedef struct _counts_t counts_t; 18 | 19 | counts_t * createCounts(void); 20 | void addCount(counts_t * c, const char * name); 21 | void printCounts(counts_t * c, FILE * outFile); 22 | 23 | void freeCounts(counts_t * c); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /33_counts/counts_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "counts.h" 5 | 6 | #define NUM_TESTS 12 7 | int main(void) { 8 | char * testData[NUM_TESTS] = {"apple", "banana", NULL,"apple", 9 | "frog","sword","bear",NULL, 10 | "frog","apple", "zebra", "knight"}; 11 | counts_t * testCounts= createCounts(); 12 | for(int i =0; i < NUM_TESTS; i++) { 13 | addCount(testCounts,testData[i]); 14 | } 15 | printCounts(testCounts, stdout); 16 | freeCounts(testCounts); 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /33_counts/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 26 11:40:27 UTC 2018 2 | Attempting to compile: 3 | ################################################# 4 | testcase1: 5 | Your output is correct 6 | - Valgrind was clean (no errors, no memory leaks) 7 | valgrind was clean 8 | ################################################# 9 | testcase2: 10 | Your output is correct 11 | - Valgrind was clean (no errors, no memory leaks) 12 | valgrind was clean 13 | ################################################# 14 | testcase3: 15 | Your output is correct 16 | - Valgrind was clean (no errors, no memory leaks) 17 | valgrind was clean 18 | 19 | Overall Grade: A 20 | -------------------------------------------------------------------------------- /34_put_together/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 2 | SRCS=$(wildcard *.c) 3 | OBJS=$(patsubst %.c, %.o, $(SRCS)) 4 | PROGRAM=count_values 5 | 6 | $(PROGRAM): $(OBJS) 7 | gcc $(CFLAGS) -o $@ $(OBJS) 8 | 9 | %.o: %.c 10 | gcc -c $(CFLAGS) $< 11 | 12 | clean: 13 | rm -f $(OBJS) $(PROGRAM) *~ 14 | 15 | counts.o: counts.h 16 | outname.o: outname.h 17 | kv.o: kv.h 18 | main.o: kv.h 19 | main.o: outname.h 20 | main.o: counts.h 21 | -------------------------------------------------------------------------------- /34_put_together/README: -------------------------------------------------------------------------------- 1 | We are now ready for the last piece: 2 | - compute the counts of values that appear in a particular input file 3 | 4 | And then to put all the pieces together. Before you start, notice that 5 | we have placed symlinks (short for "symbolic links") to the files you worked on 6 | in the previous problems. Symbolic links basically mean that when you open 7 | the file, it will open the file it links to (so opening counts.c will open 8 | ../27_counts/counts.c ). 9 | 10 | For this problem, you will start by writing the function: 11 | 12 | counts_t * countFile(const char * filename, kvarray_t * kvPairs) 13 | 14 | in the main.c file. Note that you will find it quite helpful to use several of the functions 15 | you wrote in the previous problems (in fact, much of the work should already be done). 16 | 17 | Once you have written countFile, it is time to write your main function. You 18 | will also write this function in the main.c file. Note that we have already 19 | written the high-level algorithm for the main function as comments, so you can 20 | just translate this algorithm to code. You will also need to add some error 21 | checking. 22 | 23 | Once you finish writing these two functions, you should have a working program! 24 | Compile, test, and debug it. 25 | 26 | We have provided two test cases, and the correct output for them. 27 | 28 | The first test case: 29 | ./count_values kvs1.txt list1a.txt list1b.txt 30 | 31 | should produce two output files (list1a.txt.counts and list1b.txt.counts). 32 | The correct contents can be found in list1a.txt.ans and list1b.txt.ans. 33 | 34 | The second test case: 35 | ./count_values kvs2.txt list2a.txt list2b.txt list2c.txt 36 | 37 | should produce 3 files (named appropriately) and the answers can be found in 38 | similarly named .ans files. 39 | 40 | Use git add . to submit your work. -------------------------------------------------------------------------------- /34_put_together/counts.c: -------------------------------------------------------------------------------- 1 | ../33_counts/counts.c -------------------------------------------------------------------------------- /34_put_together/counts.h: -------------------------------------------------------------------------------- 1 | ../33_counts/counts.h -------------------------------------------------------------------------------- /34_put_together/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sun Aug 26 14:34:45 UTC 2018 2 | Attempting to compile: 3 | rm -f outname.o counts.o main.o kv.o count_values *~ 4 | gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 outname.c 5 | gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 counts.c 6 | gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 main.c 7 | gcc -c -Wall -Werror -std=gnu99 -pedantic -ggdb3 kv.c 8 | gcc -Wall -Werror -std=gnu99 -pedantic -ggdb3 -o count_values outname.o counts.o main.o kv.o 9 | ################################################# 10 | testcase1: 11 | testcase1 passed, your program successfully indicated a failure 12 | - Valgrind was clean (no errors, no memory leaks) 13 | valgrind was clean 14 | ################################################# 15 | testcase2: 16 | Your file matched the expected output 17 | Comparing file list1a.txt.counts with answer 18 | Your output is correct 19 | Comparing file list1b.txt.counts with answer 20 | Your file matched the expected output 21 | Your output is correct 22 | - Valgrind was clean (no errors, no memory leaks) 23 | valgrind was clean 24 | ################################################# 25 | testcase3: 26 | Your file matched the expected output 27 | Comparing file list2a.txt.counts with answer 28 | Your output is correct 29 | Comparing file list2b.txt.counts with answer 30 | Your file matched the expected output 31 | Your output is correct 32 | Comparing file list2c.txt.counts with answer 33 | Your file matched the expected output 34 | Your output is correct 35 | - Valgrind was clean (no errors, no memory leaks) 36 | valgrind was clean 37 | ################################################# 38 | testcase4: 39 | Your file matched the expected output 40 | Comparing file list3a.txt.counts with answer 41 | Your output is correct 42 | - Valgrind was clean (no errors, no memory leaks) 43 | valgrind was clean 44 | 45 | Overall Grade: A 46 | -------------------------------------------------------------------------------- /34_put_together/kv.c: -------------------------------------------------------------------------------- 1 | ../32_kvs/kv.c -------------------------------------------------------------------------------- /34_put_together/kv.h: -------------------------------------------------------------------------------- 1 | ../32_kvs/kv.h -------------------------------------------------------------------------------- /34_put_together/kvs1.txt: -------------------------------------------------------------------------------- 1 | apple=red 2 | apricot=yellow 3 | avacado=green 4 | banana=yellow 5 | blueberry=blue 6 | carrot=orange 7 | cherry=red 8 | cranberry=red 9 | dates=brown 10 | eggplant=purple 11 | grapefruit=pink 12 | grapes=purple 13 | kiwi=green 14 | lime=green 15 | orange=orange 16 | peach=yellow 17 | pineapple=yellow 18 | plums=purple 19 | pomegranate=red 20 | strawberry=red 21 | tomato=red 22 | -------------------------------------------------------------------------------- /34_put_together/kvs2.txt: -------------------------------------------------------------------------------- 1 | Jean Luc Picard=Captain 2 | Will Riker=Commander 3 | Beverly Crusher=Commander 4 | Data=Lt. Commander 5 | Geordi LaForge=Lt. Commander 6 | Worf=Lt. Commander 7 | Deanna Troi=Commander 8 | Tasha Yar=Lieutenant 9 | Wesley Crusher=Ensign 10 | Ro Laren=Ensign 11 | Reginald Barclay=Lieutenant 12 | Guinan=Bartender 13 | -------------------------------------------------------------------------------- /34_put_together/list1a.txt: -------------------------------------------------------------------------------- 1 | apple 2 | avacado 3 | raspberry 4 | blueberry 5 | cherry 6 | eggplant 7 | grapes 8 | peach 9 | pineapple 10 | strawberry 11 | fig 12 | -------------------------------------------------------------------------------- /34_put_together/list1a.txt.ans: -------------------------------------------------------------------------------- 1 | red: 3 2 | green: 1 3 | blue: 1 4 | purple: 2 5 | yellow: 2 6 | : 2 7 | -------------------------------------------------------------------------------- /34_put_together/list1b.txt: -------------------------------------------------------------------------------- 1 | kiwi 2 | lime 3 | pineapple 4 | pomegranate 5 | raisins 6 | apple 7 | blueberry 8 | carrot 9 | cherry 10 | dates 11 | -------------------------------------------------------------------------------- /34_put_together/list1b.txt.ans: -------------------------------------------------------------------------------- 1 | green: 2 2 | yellow: 1 3 | red: 3 4 | blue: 1 5 | orange: 1 6 | brown: 1 7 | : 1 8 | -------------------------------------------------------------------------------- /34_put_together/list2a.txt: -------------------------------------------------------------------------------- 1 | Will Riker 2 | Worf 3 | Tasha Yar 4 | Guinan 5 | Ro Laren 6 | -------------------------------------------------------------------------------- /34_put_together/list2a.txt.ans: -------------------------------------------------------------------------------- 1 | Commander: 1 2 | Lt. Commander: 1 3 | Lieutenant: 1 4 | Bartender: 1 5 | Ensign: 1 6 | -------------------------------------------------------------------------------- /34_put_together/list2b.txt: -------------------------------------------------------------------------------- 1 | Jean Luc Picard 2 | Q 3 | Geordi LaForge 4 | Some guy in a red shirt 5 | Reginald Barclay 6 | Beverly Crusher 7 | Data 8 | Worf 9 | -------------------------------------------------------------------------------- /34_put_together/list2b.txt.ans: -------------------------------------------------------------------------------- 1 | Captain: 1 2 | Lt. Commander: 3 3 | Lieutenant: 1 4 | Commander: 1 5 | : 2 6 | -------------------------------------------------------------------------------- /34_put_together/list2c.txt: -------------------------------------------------------------------------------- 1 | Data 2 | Geordi LaForge 3 | Jean Luc Picard 4 | Tasha Yar 5 | Ro Laren 6 | Spock 7 | James T Kirk 8 | Katherine Janeway 9 | -------------------------------------------------------------------------------- /34_put_together/list2c.txt.ans: -------------------------------------------------------------------------------- 1 | Lt. Commander: 2 2 | Captain: 1 3 | Lieutenant: 1 4 | Ensign: 1 5 | : 3 6 | -------------------------------------------------------------------------------- /34_put_together/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "kv.h" 5 | #include "counts.h" 6 | #include "outname.h" 7 | 8 | counts_t * countFile(const char * filename, kvarray_t * kvPairs) { 9 | counts_t * ans=createCounts(); 10 | FILE* f =fopen( filename, "r"); 11 | if(f==NULL) return NULL; 12 | char* line= NULL; 13 | size_t sz=0; 14 | while (getline(&line,&sz, f) >= 0){ 15 | char* n=strchr(line,'\n'); 16 | if(n != NULL) *n='\0'; 17 | 18 | char *x= lookupValue(kvPairs, line); 19 | addCount(ans,x); 20 | } 21 | free(line); 22 | if (fclose(f) != 0 ) return NULL; 23 | return ans; 24 | } 25 | 26 | int main(int argc, char ** argv) { 27 | //WRITE ME (plus add appropriate error checking!) 28 | if(argc < 2){ 29 | fprintf(stderr,"no enough arguments"); 30 | return EXIT_FAILURE; 31 | } 32 | //read the key/value pairs from the file named by argv[1] (call the result kv) 33 | kvarray_t * kv=readKVs(argv[1]); 34 | 35 | //count from 2 to argc (call the number you count i) 36 | for(int i=2 ;i < argc;i++){ 37 | 38 | //count the values that appear in the file named by argv[i], using kv as the key/value pair 39 | // (call this result c) 40 | counts_t * c=countFile(argv[i], kv); 41 | if(c == NULL) return EXIT_FAILURE; 42 | 43 | //compute the output file name from argv[i] (call this outName) 44 | char *outName= computeOutputFileName(argv[i]); 45 | 46 | //open the file named by outName (call that f) 47 | FILE* f1= fopen(outName,"w"); 48 | 49 | //print the counts from c into the FILE f 50 | printCounts(c, f1); 51 | //close f 52 | if (fclose(f1) != 0 ) return EXIT_FAILURE ; 53 | 54 | //free the memory for outName and c 55 | free(outName); 56 | freeCounts(c) ; 57 | } 58 | 59 | freeKVs(kv) ; 60 | //free the memory for kv 61 | 62 | return EXIT_SUCCESS; 63 | } 64 | -------------------------------------------------------------------------------- /34_put_together/outname.c: -------------------------------------------------------------------------------- 1 | ../29_outname/outname.c -------------------------------------------------------------------------------- /34_put_together/outname.h: -------------------------------------------------------------------------------- 1 | ../29_outname/outname.h -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Welcome to the Practice Programming Environment. 2 | 3 | This is where 4 | This is your directory for the assignments in this 5 | specialization. To do the first assignment: 6 | 7 | cd 00_hello 8 | 9 | There, you will find a README with instructions. 10 | Once you do them and pass that assignment, 11 | the Practice Programming Environment will 12 | automatically give you the next assignment. 13 | You should then continue readings and videos 14 | in Coursera until you are ready for that assignment. 15 | -------------------------------------------------------------------------------- /and_beyond/README: -------------------------------------------------------------------------------- 1 | Congratulations! You have finsihed the project for Course 4 (and hopefully 2 | the entire Specialization). 3 | 4 | At this point, you have solid programming fundementals to build from. 5 | (and in fact, if you want to add more fancy features to your project 6 | [wild cards? folded hands? strange poker variants?] you could do it.) 7 | 8 | Hopefully you are hungry to learn more programming. Where can you go from here? 9 | 10 | We recommend "Object Oriented Programming in Java": 11 | 12 | https://www.coursera.org/specializations/object-oriented-programming 13 | 14 | which is a Coursera Specialization brought to you jointly by Duke 15 | and University of California San Diego. This specialization assumes 16 | you have good programming fundementals (which you do from this specialization), 17 | and teaches you Java and Object Orientated Programming (Drew is also 18 | one of the instructors on two of hte courser). 19 | 20 | If you really want to learn a LOT more programming from Drew and Geneveive, 21 | you could think about coming to Duke and studying in the ECE department. 22 | Duke ECE has great undergraduate and Masters programs with excellent 23 | courses on programming! 24 | 25 | Whatever you choose to do, we wish you good luck and happy programming! 26 | 27 | -- 28 | Drew, Genevieve, and Anne 29 | 30 | -------------------------------------------------------------------------------- /c2prj1_cards/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-ggdb3 -Wall -Werror -pedantic -std=gnu99 2 | GIVEN_OBJS=deck.o eval.o future.o input.o main.o eval-c4.o deck-c4.o 3 | 4 | test: cards.o my-test-main.o 5 | gcc -o test -ggdb3 cards.o my-test-main.o 6 | poker: $(GIVEN_OBJS) cards.o 7 | gcc -o poker -ggdb3 cards.o $(GIVEN_OBJS) 8 | clean: 9 | rm test poker cards.o my-test-main.o *~ 10 | -------------------------------------------------------------------------------- /c2prj1_cards/cards.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "cards.h" 5 | 6 | 7 | void assert_card_valid(card_t c) { 8 | int cond1,cond2; 9 | cond1=(c.value >=2 )&&(c.value <= VALUE_ACE); 10 | cond2=(c.suit == SPADES )||(c.suit == CLUBS )||(c.suit == HEARTS)||(c.suit == DIAMONDS); 11 | assert(cond1 && cond2); 12 | } 13 | 14 | const char * ranking_to_string(hand_ranking_t r) { 15 | switch (r){ 16 | case STRAIGHT_FLUSH: return "STRAIGHT_FLUSH"; 17 | case FOUR_OF_A_KIND: return"FOUR_OF_A_KIND"; 18 | case FULL_HOUSE: return"FULL_HOUSE"; 19 | case FLUSH: return"FLUSH"; 20 | case STRAIGHT: return"STRAIGHT"; 21 | case THREE_OF_A_KIND: return"THREE_OF_A_KIND"; 22 | case TWO_PAIR: return"TWO_PAIR"; 23 | case PAIR: return"PAIR"; 24 | default : return"NOTHING"; 25 | } 26 | } 27 | 28 | char value_letter(card_t c) { 29 | char x='?'; 30 | if ((c.value >= 2 )&&( c.value <=9)) x = '0' + c.value ; 31 | else { 32 | switch(c.value){ 33 | case VALUE_KING : {x='K'; break;} 34 | case 10 : { x='0'; break;} 35 | case VALUE_ACE :{ x= 'A' ; break;} 36 | case VALUE_QUEEN :{x='Q' ; break;} 37 | case VALUE_JACK :{x='J' ; break;} 38 | default :break; 39 | } 40 | } 41 | return x ; 42 | } 43 | 44 | 45 | char suit_letter(card_t c) { 46 | char x='?'; 47 | switch(c.suit){ 48 | case SPADES : {x='s'; break;} 49 | case HEARTS : { x='h'; break;} 50 | case DIAMONDS :{ x= 'd' ; break;} 51 | case CLUBS :{x='c' ; break;} 52 | default :{ x='N'; break;} 53 | } 54 | return x; 55 | 56 | } 57 | 58 | void print_card(card_t c) { 59 | char suit1= suit_letter( c); 60 | char value1= value_letter( c); 61 | printf("%c%c ",value1,suit1); 62 | } 63 | 64 | card_t card_from_letters(char value_let, char suit_let) { 65 | card_t temp; 66 | switch(value_let){ 67 | case 'K' : {temp.value= 13; break;} 68 | case '0' : { temp.value= 10; break;} 69 | case 'A' :{ temp.value= 14; break;} 70 | case 'Q' :{temp.value= 12; break;} 71 | case 'J' :{temp.value= 11; break;} 72 | default :{temp.value=value_let - '0'; 73 | break;} } 74 | 75 | switch(suit_let){ 76 | case 's' : {temp.suit =SPADES; break;} 77 | case 'h' : { temp.suit =HEARTS; break;} 78 | case 'd' :{temp.suit = DIAMONDS ; break;} 79 | case 'c':{temp.suit =CLUBS ; break;} 80 | default: {temp.suit = NUM_SUITS; break; }} 81 | assert_card_valid(temp); 82 | return temp; 83 | } 84 | 85 | card_t card_from_num(unsigned c) { 86 | card_t temp; 87 | unsigned value = c%13 + 2; 88 | temp.value = value; 89 | 90 | unsigned suit = c/13; 91 | switch(suit){ 92 | case 0 : {temp.suit =SPADES; break;} 93 | case 1 : { temp.suit =HEARTS; break;} 94 | case 2 :{temp.suit = DIAMONDS ; break;} 95 | case 3:{temp.suit =CLUBS ; break;} 96 | default: {temp.suit = NUM_SUITS; break; }} 97 | 98 | return temp; 99 | 100 | } 101 | 102 | /* 103 | #include 104 | #include 105 | #include 106 | #include "cards.h" 107 | 108 | 109 | void assert_card_valid(card_t c) { 110 | 111 | 112 | } 113 | 114 | const char * ranking_to_string(hand_ranking_t r) { 115 | return ""; 116 | } 117 | 118 | char value_letter(card_t c) { 119 | return 'x'; 120 | } 121 | 122 | 123 | char suit_letter(card_t c) { 124 | return 'x'; 125 | 126 | } 127 | 128 | void print_card(card_t c) { 129 | 130 | } 131 | 132 | card_t card_from_letters(char value_let, char suit_let) { 133 | card_t temp; 134 | return temp; 135 | } 136 | 137 | card_t card_from_num(unsigned c) { 138 | card_t temp; 139 | return temp; 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /c2prj1_cards/cards.h: -------------------------------------------------------------------------------- 1 | #ifndef CARD_H 2 | #define CARD_H 3 | #define VALUE_ACE 14 4 | #define VALUE_KING 13 5 | #define VALUE_QUEEN 12 6 | #define VALUE_JACK 11 7 | typedef enum { 8 | SPADES, 9 | HEARTS, 10 | DIAMONDS, 11 | CLUBS, 12 | NUM_SUITS 13 | } suit_t; 14 | 15 | struct card_tag { 16 | unsigned value; 17 | suit_t suit; 18 | }; 19 | typedef struct card_tag card_t; 20 | typedef enum { 21 | STRAIGHT_FLUSH, 22 | FOUR_OF_A_KIND, 23 | FULL_HOUSE, 24 | FLUSH, 25 | STRAIGHT, 26 | THREE_OF_A_KIND, 27 | TWO_PAIR, 28 | PAIR, 29 | NOTHING 30 | } hand_ranking_t; 31 | card_t card_from_num(unsigned c); 32 | void assert_card_valid(card_t c); 33 | const char * ranking_to_string(hand_ranking_t r) ; 34 | char value_letter(card_t c); 35 | char suit_letter(card_t c) ; 36 | void print_card(card_t c); 37 | card_t card_from_letters(char value_let, char suit_let); 38 | #endif 39 | -------------------------------------------------------------------------------- /c2prj1_cards/cards.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/cards.o -------------------------------------------------------------------------------- /c2prj1_cards/deck-c4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/deck-c4.o -------------------------------------------------------------------------------- /c2prj1_cards/deck.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/deck.o -------------------------------------------------------------------------------- /c2prj1_cards/eval-c4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/eval-c4.o -------------------------------------------------------------------------------- /c2prj1_cards/eval.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/eval.o -------------------------------------------------------------------------------- /c2prj1_cards/future.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/future.o -------------------------------------------------------------------------------- /c2prj1_cards/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Sat Aug 11 13:33:36 UTC 2018 2 | Compiling cards.c 3 | Testing card_from_letters 4 | Passed 5 | Testing value_letter and suit_letter 6 | Passed 7 | Testing print_card 8 | Passed 9 | Testing card_from_num 10 | Passed 11 | Testing ranking_to_string 12 | ranking_to_string(STRAIGHT_FLUSH) resulted in STRAIGHT_FLUSH (Correct) 13 | ranking_to_string(FOUR_OF_A_KIND) resulted in FOUR_OF_A_KIND (Correct) 14 | ranking_to_string(FULL_HOUSE) resulted in FULL_HOUSE (Correct) 15 | ranking_to_string(FLUSH) resulted in FLUSH (Correct) 16 | ranking_to_string(STRAIGHT) resulted in STRAIGHT (Correct) 17 | ranking_to_string(THREE_OF_A_KIND) resulted in THREE_OF_A_KIND (Correct) 18 | ranking_to_string(TWO_PAIR) resulted in TWO_PAIR (Correct) 19 | ranking_to_string(PAIR) resulted in PAIR (Correct) 20 | ranking_to_string(NOTHING) resulted in NOTHING (Correct) 21 | Passed 22 | Testing assert_card_valid 23 | assert_card_valid on a card with value =14, suit=4 did not 24 | fail an assert (it should have: those are not valid values) 25 | Failed 26 | 27 | Overall Grade: FAILED 28 | -------------------------------------------------------------------------------- /c2prj1_cards/input.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/input.o -------------------------------------------------------------------------------- /c2prj1_cards/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/main.o -------------------------------------------------------------------------------- /c2prj1_cards/my-test-main.c: -------------------------------------------------------------------------------- 1 | #include "cards.h" 2 | #include 3 | 4 | int main(void) { 5 | card_t c ; 6 | c.value=4; 7 | c.suit =2; 8 | assert_card_valid( c); // printf("%s",ranking_to_string(5)); 9 | const char *x = ranking_to_string(3); 10 | printf("%s",x); 11 | c=card_from_letters('5','h'); 12 | for (int i=0;i<52;i++){ 13 | c=card_from_num(i); 14 | print_card(c); 15 | printf("\n"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /c2prj1_cards/my-test-main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/my-test-main.o -------------------------------------------------------------------------------- /c2prj1_cards/poker: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj1_cards/poker -------------------------------------------------------------------------------- /c2prj2_testing/README: -------------------------------------------------------------------------------- 1 | The second part of this course's poker project is to write 2 | test cases for what you will do in the next course. 3 | 4 | In particular, one of the things you will do in your 5 | project in Course 3 is write the code to evaluate 6 | which poker hand wins between two complete hands. 7 | This involves writing all the code to figure out 8 | what kind of hand (straight flush, full house, etc.) 9 | is in a hand, and which 5 cards ultimately make it up. 10 | For example, if one hand is 11 | 12 | 0c 0s 8s 7d 6d 6s 5c 13 | 14 | and the other is 15 | 16 | Js 0s 8s 7d 6s 5c 4c 17 | 18 | your code in the next course will figure out that 19 | the first hand has two pairs (10s and 6s) with an 8 20 | as the tie-breaking card (called the "kicker"), 21 | and that the second hand has a straight (8 7 6 5 4). 22 | The straight beats the two pairs, so hand 2 wins. 23 | 24 | We have provided you with test-eval, which reads 25 | a file containing lines with one pair of hands 26 | on each line and prints out the details of evaluating 27 | that hand. Each line has one hand, a semi-colon, 28 | then the other hand, so the input for the above would 29 | be: 30 | 31 | 0c 0s 8s 7d 6d 6s 5c; Js 0s 8s 7d 6s 5c 4c 32 | 33 | We have put this in example.txt. If you run 34 | 35 | ./test-eval example.txt 36 | 37 | Then you will get the following output: 38 | 39 | Hand 1: 40 | -------- 41 | 0c 0s 8s 7d 6d 6s 5c 42 | - No flush 43 | - The most of a kind is 2 of a kind (at index 0 / value 0) 44 | - Secondary pair at index 4 (value 6) 45 | - evaluate_hand's ranking: TWO_PAIR 46 | - 5 cards used for hand: 0c 0s 6d 6s 8s 47 | Hand 2: 48 | -------- 49 | Js 0s 8s 7d 6s 5c 4c 50 | - No flush 51 | - Straight at index 2 52 | - The most of a kind is 1 of a kind (at index 0 / value J) 53 | - No secondary pair 54 | - evaluate_hand's ranking: STRAIGHT 55 | - 5 cards used for hand: 8s 7d 6s 5c 4c 56 | Comparison : 57 | -------------- 58 | Hand 2 wins! 59 | ============================ 60 | 61 | You can see that for each hand, this program not only prints 62 | the overall ranking (STRAIGHT, TWO_PAIR, etc), but also the 63 | results of various parts of the evaluation that went into 64 | the decision: Was there a flush? Was there a straight (if 65 | so, where?) How many of a kind were there? etc. 66 | 67 | As with other testing assignments, we have written 68 | some broken implementations and placed them in 69 | /usr/local/l2p/poker/ 70 | 71 | Your goal is to write testcases in tests.txt 72 | that test this problem sufficiently to identify 73 | the problem in each broken implementation we provided. 74 | 75 | You can use the run_all.sh script that we provided 76 | to run your test cases against all implementations. 77 | 78 | Here are some hints: 79 | ==================== 80 | 81 | - Straights are tricky. Think about various ways that a programmer 82 | might mess up in finding a straight. These problems could include 83 | both figuring out if there is a straight as well as copying out the 84 | cards that make up the straight (or straight flush). 85 | 86 | There could even be an off-by-one bug in where to look for a straight 87 | in the hand ("count from 0 to the number of cards in the hand minus 88 | [something], call it i. Check if a straight starts at position i" 89 | In such an algorithm, [something] could be off by one) 90 | 91 | - Think about ways in which a programmer might mis-think about what 92 | they need to do. For example, one might think you can check for a 93 | straight flush by checking for a straight AND a flush, but that is 94 | not correct (with more than 5 cards, you could have some that are a 95 | straight but others of the same suit). 96 | 97 | - Be sure to just provide simple coverage of the basic cases (each 98 | type of hand outcome, etc). 99 | 100 | - The suits are numbers, and can be iterated across )where do you 101 | think we iterate over all the suits?). What if we counted wrong 102 | when iterating over the suits? 103 | 104 | 105 | - Two pairs have some subtle cases in terms of selecting the right 5 106 | cards for the final hand. Remember from the reading that the way 107 | tie breaking works requires the final 5 card hand to be ordered: 108 | (larger pair) (smaller pair) (tiebreaker) 109 | 110 | - Think about everywhere the programer could be off-by-one. 111 | This might include such things as counting through all the 112 | positions in the hand (either missing the start or the end), 113 | or being off-by-one in how many cards are required 114 | for a particular hand (e.g., only requiring 4 115 | cards meet a particular condition instead of 5). 116 | -------------------------------------------------------------------------------- /c2prj2_testing/example.txt: -------------------------------------------------------------------------------- 1 | 0c 0s 8s 7d 6d 6s 5c; Js 0s 8s 7d 6s 5c 4c 2 | -------------------------------------------------------------------------------- /c2prj2_testing/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Thu Aug 2 16:08:08 UTC 2018 2 | Your test cases identified the problem with test-eval-0000 3 | Your test cases identified the problem with test-eval-0001 4 | Your test cases identified the problem with test-eval-0002 5 | Your test cases identified the problem with test-eval-0003 6 | Your test cases identified the problem with test-eval-0004 7 | Your test cases identified the problem with test-eval-0005 8 | Your test cases identified the problem with test-eval-0006 9 | Your test cases identified the problem with test-eval-0007 10 | Your test cases identified the problem with test-eval-0008 11 | Your test cases identified the problem with test-eval-0009 12 | Your test cases identified the problem with test-eval-0010 13 | Your test cases identified the problem with test-eval-0011 14 | Your test cases identified the problem with test-eval-0012 15 | Your test cases identified the problem with test-eval-0013 16 | Your test cases identified the problem with test-eval-0014 17 | Your test cases identified the problem with test-eval-0015 18 | Your test cases identified the problem with test-eval-0016 19 | Your test cases identified the problem with test-eval-0017 20 | Your test cases identified the problem with test-eval-0018 21 | Your test cases identified the problem with test-eval-0019 22 | Your test cases identified the problem with test-eval-0020 23 | Your test cases identified the problem with test-eval-0021 24 | Your test cases identified the problem with test-eval-0022 25 | 26 | Overall Grade: PASSED 27 | -------------------------------------------------------------------------------- /c2prj2_testing/run_all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | run_test(){ 3 | prog="$1" 4 | testfile="$2" 5 | IFS=$'\n' 6 | IFS=" " correct=`/usr/local/l2p/poker/correct-test-eval $testfile 2>&1` 7 | IFS=" " broken=`$prog $testfile 2>&1` 8 | if [ "$broken" != "$correct" ] 9 | then 10 | return 0 11 | fi 12 | return 1 13 | } 14 | 15 | found=0 16 | notfound=0 17 | for i in /usr/local/l2p/poker/test-eval-* 18 | do 19 | run_test $i tests.txt 20 | x="$?" 21 | if [ "$x" != "0" ] 22 | then 23 | echo "Your test cases did not identify the problem with `basename $i`" 24 | let notfound=${notfound}+1 25 | else 26 | let found=${found}+1 27 | fi 28 | done 29 | echo "Test cases identified $found problems" 30 | echo "Test cases failed to identify $notfound problems" 31 | -------------------------------------------------------------------------------- /c2prj2_testing/test-eval: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c2prj2_testing/test-eval -------------------------------------------------------------------------------- /c2prj2_testing/tests.txt: -------------------------------------------------------------------------------- 1 | As Ad Kc Kh 0s 7c 3h; Ks Kc Kd 0h 0s 5c 4d 2 | Ks Kd Ac 9h 7s 3c 2h; Ks Kd 8c 9h 7s 3c 2h 3 | As Ad Kc Kh 0s 7c 3h; 0s 0c 0d 0h Qs 5c 4d 4 | Jc 0c 9s 8s 7s 6s 5s; Jc 0s 9s 8s 7s 6c 5s 5 | Js 0s 9s 8s 7d 6s 5c; Ks 0s 9s 8s 7d 6s 5c 6 | 0c 0s 8s 7d 6d 6s 5c; Js 0s 8s 7d 6s 5c 4c 7 | Js Js 9s 8s 7d 6h 6d; Js Js 9d 8d 8h 5h 5c 8 | Ks Kd Ac 9h 7s 3c 2h; Ks Kd Ac 9h 5s 3c 2h 9 | Ks Kd Kc Ah 5s 4c 3h; Ks Kd Kc Ah 7s 3c 2h 10 | Js Qc 5d 4h 3s 2h 0d; Ks Qd Jc 0h 9s 5c 4h 11 | As Qs 8s 7s 4s 3c 2d; Ks Kc Kd 0h 0s 5c 1d 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /c3prj1_deck/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-ggdb3 -Wall -Werror -pedantic -std=gnu99 2 | GIVEN_OBJS=deck-c4.o eval-c4.o future.o input.o main.o 3 | MY_OBJS=cards.o deck.o eval.o 4 | 5 | test-deck: deck.o test-deck.o deck-c4.o cards.o 6 | gcc -o test-deck -ggdb3 deck.o test-deck.o deck-c4.o cards.o 7 | clean: 8 | rm test poker cards.o my-test-main.o *~ 9 | -------------------------------------------------------------------------------- /c3prj1_deck/cards.c: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.c -------------------------------------------------------------------------------- /c3prj1_deck/cards.h: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.h -------------------------------------------------------------------------------- /c3prj1_deck/cards.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj1_deck/cards.o -------------------------------------------------------------------------------- /c3prj1_deck/deck-c4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj1_deck/deck-c4.o -------------------------------------------------------------------------------- /c3prj1_deck/deck.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "deck.h" 5 | 6 | int com (card_t c1,card_t c2){ 7 | if ((c1.value == c2.value) &&(c1.suit == c2.suit)) return 1; 8 | return 0; 9 | 10 | } 11 | 12 | void print_hand(deck_t * hand){ 13 | card_t ** card =hand -> cards ; 14 | card_t card1; 15 | for (size_t i=0 ;i<(hand -> n_cards );i++){ 16 | card1=**(card +i); 17 | print_card(card1); 18 | } 19 | } 20 | 21 | int deck_contains(deck_t * d, card_t c) { 22 | card_t ** card =d -> cards ; 23 | for (size_t i=0 ;i< d -> n_cards ;i++){ 24 | if (com(**(card+i),c)) return 1; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | void shuffle(deck_t * d){ 31 | card_t ** card =d -> cards ; 32 | card_t * temp; 33 | size_t n=d ->n_cards; 34 | int randarry; 35 | for (size_t i=0 ;i< n/2 ;i++){ 36 | randarry= random()%(n-i)+i; 37 | temp=card[i]; 38 | card[i]=card[randarry]; 39 | card[randarry]=temp; 40 | } 41 | } 42 | 43 | void assert_full_deck(deck_t * d) { 44 | card_t ** card =d -> cards ; 45 | card_t c; 46 | int count; 47 | 48 | for (size_t i=0 ;i< d -> n_cards ;i++){ 49 | c=**(card+i); 50 | count=0; 51 | for (size_t j=0 ;j< d -> n_cards ;j++){ 52 | if(com(**(card+j), c)) count ++; 53 | } 54 | assert(count ==1); 55 | } 56 | } 57 | 58 | void add_card_to(deck_t * deck, card_t c){ 59 | 60 | card_t* c1=malloc(sizeof(*c1)); 61 | 62 | c1->value = c.value; 63 | 64 | c1->suit = c.suit; 65 | 66 | deck ->cards= realloc(deck->cards,(deck ->n_cards + 1)*sizeof(*deck ->cards)); 67 | 68 | deck ->cards[deck ->n_cards]=c1; 69 | 70 | deck ->n_cards ++; 71 | 72 | } 73 | 74 | 75 | 76 | card_t * add_empty_card(deck_t * deck){ 77 | 78 | card_t* c1=malloc(sizeof(*c1)); 79 | 80 | c1->value = 0; 81 | 82 | c1->suit = 0; 83 | 84 | deck ->cards= realloc(deck->cards,(deck ->n_cards + 1)*sizeof(*deck ->cards)); 85 | 86 | deck ->cards[deck ->n_cards]=c1; 87 | 88 | deck ->n_cards ++; 89 | 90 | return c1; 91 | 92 | } 93 | 94 | 95 | 96 | 97 | 98 | deck_t * make_deck_exclude(deck_t * excluded_cards){ 99 | 100 | deck_t* ans=malloc(sizeof(*ans)); 101 | 102 | ans->cards=NULL; 103 | 104 | ans->n_cards=0; 105 | 106 | for(unsigned i=0;i<52;i++){ 107 | 108 | card_t c= card_from_num(i); 109 | 110 | if(!deck_contains(excluded_cards,c)) add_card_to(ans,c); 111 | 112 | } 113 | 114 | return ans; 115 | 116 | } 117 | 118 | deck_t * build_remaining_deck(deck_t ** hands, size_t n_hands) { 119 | 120 | deck_t* deck=malloc(sizeof(*deck)); 121 | 122 | deck->n_cards=0; 123 | 124 | deck->cards=NULL; 125 | 126 | for (size_t i=0 ; in_cards ;x++){ 129 | 130 | deck->cards=realloc(deck->cards,(deck->n_cards+1)*sizeof(*deck->cards)); 131 | 132 | deck->cards[deck->n_cards]=hands[i]->cards[x]; 133 | 134 | deck->n_cards ++; 135 | 136 | } 137 | 138 | } 139 | 140 | deck_t* deck2=make_deck_exclude(deck); 141 | 142 | free(deck->cards); 143 | 144 | free(deck); 145 | 146 | return deck2 ; 147 | 148 | } 149 | 150 | 151 | 152 | void free_deck(deck_t * deck) { 153 | 154 | for(int i=0 ;in_cards;i++){ 155 | 156 | free(deck->cards[i]); 157 | 158 | } 159 | 160 | free(deck->cards); 161 | 162 | free(deck); 163 | 164 | } 165 | 166 | 167 | -------------------------------------------------------------------------------- /c3prj1_deck/deck.h: -------------------------------------------------------------------------------- 1 | #ifndef DECK_H 2 | #define DECK_H 3 | #include 4 | #include "cards.h" 5 | struct deck_tag { 6 | card_t ** cards; 7 | size_t n_cards; 8 | }; 9 | typedef struct deck_tag deck_t; 10 | 11 | void print_hand(deck_t * hand); 12 | int deck_contains(deck_t * d, card_t c) ; 13 | void shuffle(deck_t * d); 14 | void assert_full_deck(deck_t * d) ; 15 | //The below functions will be done in course 4. 16 | deck_t * make_deck_exclude(deck_t * excluded_cards); 17 | void add_card_to(deck_t * deck, card_t c); 18 | card_t * add_empty_card(deck_t * deck); 19 | void free_deck(deck_t * deck) ; 20 | deck_t * build_remaining_deck(deck_t ** hands, size_t n_hands) ; 21 | #endif 22 | -------------------------------------------------------------------------------- /c3prj1_deck/deck.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj1_deck/deck.o -------------------------------------------------------------------------------- /c3prj1_deck/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Wed Aug 8 22:13:24 UTC 2018 2 | Compiling deck.c 3 | Compiling cards.c 4 | Linking cards.o deck.o deck-c4.o and the grader's .o file 5 | Checking the output of all the functions other than shuffle 6 | Your file matched the expected output 7 | - Those functions seem to work! 8 | Checking your shuffle results with a 6 card hand... 9 | Least common hand: 0.130700% 10 | Most common hand: 0.146400% 11 | Perfectly even is: 0.138888% 12 | Excellent! 13 | 14 | Overall Grade: A 15 | -------------------------------------------------------------------------------- /c3prj1_deck/test-deck.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj1_deck/test-deck.o -------------------------------------------------------------------------------- /c3prj2_eval/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-ggdb3 -Wall -Werror -pedantic -std=gnu99 2 | GIVEN_OBJS=deck-c4.o eval-c4.o future.o input.o main.o 3 | MY_OBJS=cards.o deck.o eval.o 4 | 5 | test-eval: deck.o eval.o test-eval.o cards.o input.o future.o 6 | gcc -o test-eval -ggdb3 deck.o deck-c4.o eval-c4.o eval.o test-eval.o cards.o input.o future.o 7 | poker: $(GIVEN_OBJS) $(MY_OBJS) 8 | gcc -o poker -ggdb3 $(MY_OBJS) $(GIVEN_OBJS) 9 | clean: 10 | rm test poker cards.o my-test-main.o *~ 11 | -------------------------------------------------------------------------------- /c3prj2_eval/cards.c: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.c -------------------------------------------------------------------------------- /c3prj2_eval/cards.h: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.h -------------------------------------------------------------------------------- /c3prj2_eval/cards.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/cards.o -------------------------------------------------------------------------------- /c3prj2_eval/deck-c4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/deck-c4.o -------------------------------------------------------------------------------- /c3prj2_eval/deck.c: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.c -------------------------------------------------------------------------------- /c3prj2_eval/deck.h: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.h -------------------------------------------------------------------------------- /c3prj2_eval/deck.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/deck.o -------------------------------------------------------------------------------- /c3prj2_eval/eval-c4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/eval-c4.o -------------------------------------------------------------------------------- /c3prj2_eval/eval.h: -------------------------------------------------------------------------------- 1 | #ifndef EVAL_H 2 | #define EVAL_H 3 | #include "deck.h" 4 | struct hand_eval_tag { 5 | hand_ranking_t ranking; 6 | card_t *cards[5]; 7 | }; 8 | typedef struct hand_eval_tag hand_eval_t; 9 | 10 | hand_eval_t evaluate_hand(deck_t * hand) ; 11 | int compare_hands(deck_t * hand1, deck_t * hand2) ; 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /c3prj2_eval/eval.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/eval.o -------------------------------------------------------------------------------- /c3prj2_eval/future.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/future.o -------------------------------------------------------------------------------- /c3prj2_eval/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Thu Aug 16 15:09:03 UTC 2018 2 | Compiling your code 3 | rm test poker cards.o my-test-main.o *~ 4 | Makefile:10: recipe for target 'clean' failed 5 | cc -ggdb3 -Wall -Werror -pedantic -std=gnu99 -c -o cards.o cards.c 6 | gcc -o test-eval -ggdb3 deck.o deck-c4.o eval-c4.o eval.o test-eval.o cards.o input.o future.o 7 | Testcase 1: Trying hands with nothing 8 | Checking the output 9 | Your file matched the expected output 10 | - Testcase passed 11 | Testcase 2: Trying hands with pairs 12 | Checking the output 13 | Your file matched the expected output 14 | - Testcase passed 15 | Testcase 3: Trying hands with 2 pairs 16 | Checking the output 17 | Your file matched the expected output 18 | - Testcase passed 19 | Testcase 4: Trying hands with 3 of a kind 20 | Checking the output 21 | Your file matched the expected output 22 | - Testcase passed 23 | Testcase 5: Trying hands with straights 24 | Checking the output 25 | Your file matched the expected output 26 | - Testcase passed 27 | Testcase 6: Trying hands with flushes 28 | Checking the output 29 | Your file matched the expected output 30 | - Testcase passed 31 | Testcase 7: Trying hands with full houses 32 | Checking the output 33 | Your file matched the expected output 34 | - Testcase passed 35 | Testcase 8: Trying hands with 4 of a kind 36 | Checking the output 37 | Your file matched the expected output 38 | - Testcase passed 39 | Testcase 9: Trying hands with straight flushes 40 | Checking the output 41 | Your file matched the expected output 42 | - Testcase passed 43 | Testcase 10: Trying each type of hand ranking 44 | Checking the output 45 | Your file matched the expected output 46 | - Testcase passed 47 | 48 | Overall Grade: A 49 | -------------------------------------------------------------------------------- /c3prj2_eval/input.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/input.o -------------------------------------------------------------------------------- /c3prj2_eval/test-eval.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AhmedGharib0014/Programming-Fundamentals-c-course-by-Duke-university/d7876def8387081bde25b8fac671cbed2e3ea3c8/c3prj2_eval/test-eval.o -------------------------------------------------------------------------------- /c3prj2_eval/tests.txt: -------------------------------------------------------------------------------- 1 | ../c2prj2_testing/tests.txt -------------------------------------------------------------------------------- /c4prj1_deck/README: -------------------------------------------------------------------------------- 1 | Poker: Almost There! 2 | -------------------- 3 | Now it is time to wrap up your poker project! 4 | 5 | You have four major things to do: 6 | (1) Write a few deck/evaluation functions that need 7 | malloc/realloc/free (which you were not ready for in 8 | Course 3). 9 | (2) Write the code to handle unknown cards (which 10 | we'll call "future cards" because they will be 11 | known in the future). 12 | (3) Read the input. 13 | (4) Write a main function, which puts this all together 14 | and does the Monte Carlo simulation loop 15 | (repeatedly drawing different random cards 16 | for the future cards). 17 | 18 | We're going to split this into 3 parts. In this assignment, 19 | you will do (1) above. In the next assignment, you will do (2) and (3), 20 | which will work together to process the input. In the final assignment, 21 | you will do (4), completing this project! 22 | 23 | 24 | Finish deck/evaluation 25 | ---------------------- 26 | Add these functions to deck.c: 27 | - void add_card_to(deck_t * deck, card_t c); 28 | Add the particular card to the given deck (which will 29 | involve reallocing the array of cards in that deck). 30 | - card_t * add_empty_card(deck_t * deck); 31 | Add a card whose value and suit are both 0, and return a pointer 32 | to it in the deck. 33 | This will add an invalid card to use as a placeholder 34 | for an unknown card. 35 | - deck_t * make_deck_exclude(deck_t * excluded_cards); 36 | Create a deck that is full EXCEPT for all the cards 37 | that appear in excluded_cards. For example, 38 | if excluded_cards has Kh and Qs, you would create 39 | a deck with 50 cards---all of them except Kh and Qs. 40 | You will need to use malloc to allocate this deck. 41 | (You will want this for the next function). 42 | Don't forget you wrote card_t card_from_num(unsigned c) 43 | in Course 2 and int deck_contains(deck_t * d, card_t c) 44 | in Course 3! They might be useful here. 45 | - deck_t * build_remaining_deck(deck_t ** hands, size_t n_hands) ; 46 | This function takes an array of hands (remember 47 | that we use deck_t to represent a hand). It then builds 48 | the deck of cards that remain after those cards have 49 | been removed from a full deck. For example, if we have 50 | two hands: 51 | Kh Qs ?0 ?1 ?2 ?3 ?4 52 | As Ac ?0 ?1 ?2 ?3 ?4 53 | then this function should build a deck with 48 54 | cards (all but As Ac Kh Qs). You can just build 55 | one deck with all the cards from all the hands 56 | (remember you just wrote add_card_to), 57 | and then pass it to make_deck_exclude. 58 | - void free_deck(deck_t * deck) ; 59 | Free the memory allocated to a deck of cards. 60 | For example, if you do 61 | deck_t * d = make_excluded_deck(something); 62 | free_deck(d); 63 | it should free all the memory allocated by make_excluded_deck. 64 | Once you have written it, add calls to free_deck anywhere you 65 | need to to avoid memory leaks. 66 | 67 | Next, go back to eval.c, and implement: 68 | - unsigned * get_match_counts(deck_t * hand) ; 69 | You will find its prototype after the code you wrote 70 | in Course 3, and before the functions we 71 | provided for you. Replace the prototype with 72 | your implementation. 73 | 74 | Given a hand (deck_t) of cards, this function 75 | allocates an array of unsigned ints with as 76 | many elements as there are cards in the hand. 77 | It then fills in this array with 78 | the "match counts" of the corresponding cards. 79 | That is, for each card in the original hand, 80 | the value in the match count array 81 | is how many times a card of the same 82 | value appears in the hand. For example, 83 | given 84 | Ks Kh Qs Qh 0s 9d 9c 9h 85 | This function would return 86 | 2 2 2 2 1 3 3 3 87 | because there are 2 kings, 2 queens, 88 | 1 ten, and 3 nines. 89 | 90 | We recommend you write your own testing code in a separate 91 | C file (e.g., test-c4deck.c) and run your own test cases 92 | before you submit. When you are satisfied with the correctness 93 | of your code, you should grade this assignment. -------------------------------------------------------------------------------- /c4prj1_deck/cards.c: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.c -------------------------------------------------------------------------------- /c4prj1_deck/cards.h: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.h -------------------------------------------------------------------------------- /c4prj1_deck/deck.c: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.c -------------------------------------------------------------------------------- /c4prj1_deck/deck.h: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.h -------------------------------------------------------------------------------- /c4prj1_deck/eval.c: -------------------------------------------------------------------------------- 1 | ../c3prj2_eval/eval.c -------------------------------------------------------------------------------- /c4prj1_deck/eval.h: -------------------------------------------------------------------------------- 1 | ../c3prj2_eval/eval.h -------------------------------------------------------------------------------- /c4prj1_deck/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Mon Aug 27 12:17:14 UTC 2018 2 | Compiling deck.c -> deck.o 3 | Compiling eval.c -> eval.o 4 | Compiling cards.c -> cards.o 5 | Linking eval.o, deck.o, cards.o, and our tester 6 | Testing free_deck(deck_t *) 7 | - Test passed 8 | Testing add_card_to(deck_t *, card_t) 9 | - Test passed 10 | Testing add_empty_card(deck_t *) 11 | - Test passed 12 | Testing make_deck_exclude(deck_t *) 13 | - Test passed 14 | Testing build_remaining_deck(deck_t **, size_t) 15 | Testing with 1 hand 16 | Testing with 2 hands 17 | Testing with 3 hands 18 | Testing with 4 hands 19 | Testing with 5 hands 20 | Testing with 6 hands 21 | - Test passed 22 | Testing get_match_count(deck_t *) 23 | - Test passed 24 | 25 | Overall Grade: A 26 | -------------------------------------------------------------------------------- /c4prj2_input/README: -------------------------------------------------------------------------------- 1 | Now you are going to write the part of this program 2 | that will read the input, but before you do the actual input 3 | processing, you are going to need to write some code 4 | to handle unknown cards (those hidden from you, or 5 | to be dealt in the future). 6 | 7 | Handling unknown/future cards 8 | ------------------------------ 9 | 10 | Here, you are going to write some functions to handle 11 | the unknown cards (?0, ?1, etc). 12 | 13 | First, take a look at future.h. You will see 14 | that there is one struct (which is basically an 15 | array of decks) and two functions. 16 | 17 | Next, watch the video in which we explain the conceptual idea 18 | of how we represent future cards. Good thing you learned to draw 19 | pictures of the behavior of programs isn't it? :) 20 | 21 | Now you are ready to write the two functions in future.c: 22 | 23 | - void add_future_card(future_cards_t * fc, size_t index, card_t * ptr); 24 | This adds ptr into the future cards for the given index (that is, 25 | which ?n it is). So if this is a future card for ?3, then index 26 | will be 3. ptr will point at an existing placeholder card 27 | (it will point into a hand at a card which was added 28 | with add_empty_card). 29 | Note that it is entirely possible to have an input like 30 | Kh Qh As 4c 2c ?3 ?4 31 | Ac Qc As 4c 2c ?3 ?4 32 | (which might happen if e.g., someone edited a file that 33 | originally have ?0, ?1, and ?2 but replaced them when 34 | they became known). Or you might see ?3 before ?2. 35 | Your add_future_card should handle such 36 | cases by reallocing its array to be large enough to handle 37 | the specified index, and just having empty decks for 38 | the indicies that have not had add_future_card called 39 | on them yet. 40 | 41 | - void future_cards_from_deck(deck_t * deck, future_cards_t * fc); 42 | This function takes a deck (which has been shuffled), 43 | and a future_cards_t (which has been filled in with 44 | all the pointers to placeholders) and draws cards from 45 | the deck and assigns their values and suits to the 46 | placeholders pointed to in fc. 47 | For example if the deck is 48 | As Kh 8c .... 49 | and fc was created from the input 50 | 3c 4c ?0 ?1 ?2 51 | 5h 9d ?0 ?1 ?2 52 | then this function will draw As for ?0, 53 | and fill in the two placeholders for ?0 (which 54 | you can find with the pointers in fc, as described 55 | in the video). Then it will draw Kh for ?1, 56 | and so on. Think about a case where this function would need to 57 | print an error message. 58 | 59 | You should write a main to test this functionality 60 | in a separate .c file (e.g., test-future.c). We will 61 | compile our future.c and link it with our own main 62 | to test it. We leave the details of how to test this 63 | up to you. 64 | 65 | Reading the input 66 | ----------------- 67 | 68 | Now, you are ready to actually read the input. 69 | In input.c, write the function: 70 | 71 | - deck_t ** read_input(FILE * f, size_t * n_hands, future_cards_t * fc); 72 | This function reads the input from f. Recall that the input 73 | file has one hand per line (and that we represent a hand 74 | with a deck_t). You should allocate a deck_t for each hand 75 | and place it into an array of pointers to deck_ts, which is your 76 | answer. 77 | This function needs to tell its caller how many hands it read. 78 | We could return a struct, but we are going to do this a 79 | different way: it will fill in *n_hands with the number 80 | of hands. This is a bit different, but you have seen it 81 | before: this is how getline "returns" the string it 82 | read and the size of the space allocated to that string. 83 | 84 | As you read the input, if you encounter future cards 85 | (?0, ?1, ...), you should use add_empty_card to 86 | create a placeholder in the hand, and then add_future_card 87 | to make sure you will update it correctly when you draw 88 | later. Also remember that you wrote add_card_to 89 | earlier in this course, as well as card_from_letters 90 | in Course 2. These will both be handy here! 91 | 92 | I abstracted out the code to take one single line 93 | and parse it into a hand: 94 | deck_t * hand_from_string(const char * str, future_cards_t * fc) 95 | and recommend you do the same. 96 | 97 | Note that most of the rest of the code assumes that a poker hand 98 | has AT LEAST 5 cards in it. Your read_input function should enforce 99 | this requirement. If there are fewer than 5 cards, print 100 | a useful error message and exit. 101 | 102 | Again, we recommend you write your own testing code (e.g., 103 | in test-input.c). Once you are satisfied with 104 | the functionality of your code, grade this assignment. 105 | -------------------------------------------------------------------------------- /c4prj2_input/cards.c: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.c -------------------------------------------------------------------------------- /c4prj2_input/cards.h: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.h -------------------------------------------------------------------------------- /c4prj2_input/deck.c: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.c -------------------------------------------------------------------------------- /c4prj2_input/deck.h: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.h -------------------------------------------------------------------------------- /c4prj2_input/eval.c: -------------------------------------------------------------------------------- 1 | ../c3prj2_eval/eval.c -------------------------------------------------------------------------------- /c4prj2_input/eval.h: -------------------------------------------------------------------------------- 1 | ../c3prj2_eval/eval.h -------------------------------------------------------------------------------- /c4prj2_input/future.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "deck.h" 6 | #include "cards.h" 7 | #include "future.h" 8 | void add_future_card(future_cards_t * fc, size_t index, card_t * ptr){ 9 | if(index < fc->n_decks){ 10 | fc->decks[index].cards=realloc(fc->decks[index].cards,(fc->decks[index].n_cards+1)*sizeof(*(fc->decks[index].cards))); 11 | fc->decks[index].cards[fc->decks[index].n_cards]=ptr; 12 | fc->decks[index].n_cards ++; 13 | } 14 | else{ 15 | while(fc->n_decks <= index){ 16 | fc->decks=realloc(fc->decks,(fc->n_decks+1)*sizeof(*fc->decks)); 17 | fc->decks[fc->n_decks].cards=NULL; 18 | fc->decks[fc->n_decks].n_cards=0; 19 | fc->n_decks ++; 20 | } 21 | fc->decks[index].cards=realloc(fc->decks[index].cards,(fc->decks[index].n_cards+1)*sizeof(*(fc->decks[index].cards))); 22 | fc->decks[index].cards[fc->decks[index].n_cards]=ptr; 23 | fc->decks[index].n_cards ++; 24 | } 25 | } 26 | void future_cards_from_deck(deck_t * deck, future_cards_t * fc){ 27 | //error cheching 28 | if (deck->n_cards< fc->n_decks){ 29 | fprintf(stderr,"future_cards_from_deck"); 30 | return; 31 | } 32 | //initalization 33 | for(int i=0 ;in_decks;i++){ 34 | if(fc->decks[i].n_cards == 0) continue; 35 | for(int x=0 ;xdecks[i].n_cards ;x++){ 36 | (*(fc->decks[i].cards[x])).value=(*(deck->cards[i])).value; 37 | (*(fc->decks[i].cards[x])).suit=(*(deck->cards[i])).suit; 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /c4prj2_input/future.h: -------------------------------------------------------------------------------- 1 | #ifndef FUTURE_H 2 | #define FUTURE_H 3 | #include "deck.h" 4 | 5 | 6 | struct future_cards_tag { 7 | deck_t * decks; 8 | size_t n_decks; 9 | }; 10 | typedef struct future_cards_tag future_cards_t; 11 | void add_future_card(future_cards_t * fc, size_t index, card_t * ptr) ; 12 | void future_cards_from_deck(deck_t * deck, future_cards_t * fc); 13 | 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /c4prj2_input/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Tue Aug 28 16:15:16 UTC 2018 2 | Compiling cards.c -> cards.o 3 | Compiling deck.c -> deck.o 4 | Compiling input.c -> input.o 5 | Compiling future.c -> future.o 6 | Linking input.o, future.o deck.o, cards.o, and our tester 7 | Testing with input file with 8 | o 1 hand 9 | o No unknown/future cards 10 | Your file matched the expected output 11 | Test case passed 12 | Testing with input file with 13 | o Many hands 14 | o No unknown/future cards 15 | Your file matched the expected output 16 | Test case passed 17 | Testing with input file with 18 | o Many hands 19 | o 1 unknown/future cards per hand 20 | Your file matched the expected output 21 | Test case passed 22 | Testing with input file with 23 | o Many hands 24 | o Many unknown/future cards per hand 25 | Your file matched the expected output 26 | Test case passed 27 | 28 | Overall Grade: A 29 | -------------------------------------------------------------------------------- /c4prj2_input/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "deck.h" 6 | #include "cards.h" 7 | #include "future.h" 8 | deck_t * hand_from_string(const char * str, future_cards_t * fc){ 9 | deck_t* deck=malloc(sizeof(*deck)); 10 | deck->cards = NULL; 11 | deck->n_cards=0; 12 | for(int i=0;in_cards < 5 ){ 33 | return NULL; 34 | fprintf(stderr,"asas");} 35 | return deck; 36 | } 37 | deck_t ** read_input(FILE * f, size_t * n_hands, future_cards_t * fc){ 38 | deck_t **arr=NULL; 39 | size_t n_hand=0; 40 | char *line=NULL; 41 | size_t sz=0; 42 | while(getline(&line,&sz,f)>=0){ 43 | arr=realloc(arr,(n_hand+1)*sizeof(*arr)); 44 | deck_t*deck=hand_from_string(line,fc); 45 | if (deck == NULL)continue; 46 | arr[n_hand]=deck; 47 | n_hand ++; 48 | } 49 | free(line); 50 | *n_hands=n_hand; 51 | return arr; 52 | } 53 | -------------------------------------------------------------------------------- /c4prj2_input/input.h: -------------------------------------------------------------------------------- 1 | #ifndef INPUT_H 2 | #define INPUT_H 3 | #include 4 | #include "deck.h" 5 | #include "future.h" 6 | 7 | deck_t * hand_from_string(const char * str, future_cards_t * fc); 8 | deck_t ** read_input(FILE * f, size_t * n_hands, future_cards_t * fc); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /c4prj3_finish/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Werror -ggdb3 -std=gnu99 -pedantic $(OTHERFLAGS) 3 | poker: cards.o deck.o eval.o future.o input.o main.o 4 | gcc -o $@ $(CFLAGS) $^ 5 | %.o: %.c 6 | gcc $(CFLAGS) -c $< 7 | depend: 8 | makedepend cards.c deck.c eval.c future.c input.c main.c 9 | clean: 10 | rm -rf *.o poker *~ 11 | 12 | -------------------------------------------------------------------------------- /c4prj3_finish/README: -------------------------------------------------------------------------------- 1 | Time to wrap it all up! You are now going to write 2 | main, which puts it all together. In particular, 3 | your program should take 1 or 2 command line arguments. 4 | The first is required, and is the name of the input 5 | file to read. The second is optional and is the 6 | number of Monte Carlo trials to perform. If it 7 | is not given, you should use 10,000 as a default value. 8 | 9 | Here is the gist of what your main does: 10 | - Check command line arguments/report errors 11 | - Open the input file and read the hands in it 12 | (you just wrote read_input!) 13 | - Create a deck with the remaining cards 14 | (you just wrote build_remaining_deck) 15 | - Create an array to count how many times each hand 16 | wins, with one more element for if there was a tie 17 | (so if there are 2 hands, you should have 3 elements). 18 | and initialize all its values to 0. 19 | - Do each Monte Carlo trial (repeat num_trials times) 20 | - Shuffle the deck of remaining cards 21 | (you wrote shuffle in Course 3) 22 | - Assign unknown cards from the shuffled deck 23 | (you just wrote future_cards_from_deck) 24 | - Use compare_hands (from Course 3) to 25 | figure out which hand won. Note that 26 | with potentially more than 2 hands, 27 | this is much like finding the max of 28 | an array, but using compare_hands 29 | instead of >. 30 | - Increment the win count for the winning 31 | hand (or for the "ties" element of the array 32 | if there was a tie). 33 | - After you do all your trials, you just need 34 | to print your results, free any memory 35 | you allocated, and close any open files. 36 | 37 | You should use the following format strings to report your results. 38 | For each hand, you should printf 39 | "Hand %zu won %u / %u times (%.2f%%)\n" 40 | where the %zu is the hand number (0,1,..) [zu is for size_t] 41 | the first %u is the number of wins for that hand 42 | the second %u is the total number of trials 43 | the %.2f is the percentage that this win/trials ratio gives 44 | Then you should printf one more line for the ties: 45 | "And there were %u ties\n" 46 | Where the %u is just a count of how many ties there were 47 | 48 | We're going to let you write your own Makefile for this assignment. 49 | This Makefile should create a program called "poker" when we run "make". 50 | 51 | Of course, you need to test this out before you submit 52 | it for grading. This seems like a complicated piece 53 | of code, but you already have a lot of confidence in what you 54 | wrote in Courses 2 and 3---so thats a good start. You 55 | have hopefully been testing most of the functions you 56 | wrote along the way. If so, you are confident in them too. 57 | 58 | But that leaves testing your whole program, which performs 59 | random simulations---that can be tricky. So how can you do it? 60 | To help you out, we have provided some test cases in provided-tests, 61 | along with the expected probabilities (in provided-tests/answers.txt) 62 | These probabilities are rounded to the nearest percent, and we 63 | don't expect your answer to be exact. Also note that the probabiltiies 64 | do not always add to 100% since some hands have 65 | the possibility of ties. 66 | 67 | If you have problems, you can make use of the testing 68 | infrastructure from eariler steps to help you sort 69 | out the problem. For example, you could have your 70 | main print out each set of hands in a format 71 | that you could feed into test-eval. 72 | You could then see if your evaluation and our evaluation 73 | match up for each hand that your simuluation drew. 74 | (Hint: use diff, don't try to compare them by hand!) 75 | 76 | 77 | We'll note that we setup the Makefile to build things 78 | for debugging by default. However, once your program 79 | works, if you want to run longer simulations faster 80 | you can do 81 | 82 | make clean 83 | make OTHERFLAGS=-O3 84 | 85 | which will build your program with optimizations, 86 | and it will run 2--3x faster. 87 | -------------------------------------------------------------------------------- /c4prj3_finish/cards.c: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.c -------------------------------------------------------------------------------- /c4prj3_finish/cards.h: -------------------------------------------------------------------------------- 1 | ../c2prj1_cards/cards.h -------------------------------------------------------------------------------- /c4prj3_finish/deck.c: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.c -------------------------------------------------------------------------------- /c4prj3_finish/deck.h: -------------------------------------------------------------------------------- 1 | ../c3prj1_deck/deck.h -------------------------------------------------------------------------------- /c4prj3_finish/eval.c: -------------------------------------------------------------------------------- 1 | ../c3prj2_eval/eval.c -------------------------------------------------------------------------------- /c4prj3_finish/eval.h: -------------------------------------------------------------------------------- 1 | ../c3prj2_eval/eval.h -------------------------------------------------------------------------------- /c4prj3_finish/future.c: -------------------------------------------------------------------------------- 1 | ../c4prj2_input/future.c -------------------------------------------------------------------------------- /c4prj3_finish/future.h: -------------------------------------------------------------------------------- 1 | ../c4prj2_input/future.h -------------------------------------------------------------------------------- /c4prj3_finish/grade.txt: -------------------------------------------------------------------------------- 1 | Grading at Fri Aug 31 13:04:29 UTC 2018 2 | Running 'make clean' to do a clean build of your project 3 | rm -rf *.o poker *~ 4 | Running 'make poker OTHERFLAGS=-O3' to build your project 5 | gcc -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 -c cards.c 6 | gcc -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 -c deck.c 7 | gcc -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 -c eval.c 8 | gcc -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 -c future.c 9 | gcc -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 -c input.c 10 | gcc -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 -c main.c 11 | gcc -o poker -Wall -Werror -ggdb3 -std=gnu99 -pedantic -O3 cards.o deck.o eval.o future.o input.o main.o 12 | - Starting with some Texas Hold'em hands 13 | Running a simulation with 20000 draws for 2 hands... 14 | Hand 0 was close enough to our answer 15 | Hand 1 was close enough to our answer 16 | Test case passed! 17 | Running a simulation with 25000 draws for 3 hands... 18 | Hand 0 was close enough to our answer 19 | Hand 1 was close enough to our answer 20 | Hand 2 was close enough to our answer 21 | Test case passed! 22 | Running a simulation with 25000 draws for 3 hands... 23 | Hand 0 was close enough to our answer 24 | Hand 1 was close enough to our answer 25 | Hand 2 was close enough to our answer 26 | Test case passed! 27 | Running a simulation with 25000 draws for 2 hands... 28 | Hand 0 was close enough to our answer 29 | Hand 1 was close enough to our answer 30 | Test case passed! 31 | Running a simulation with 80000 draws for 4 hands... 32 | Hand 0 was close enough to our answer 33 | Hand 1 was close enough to our answer 34 | Hand 2 was close enough to our answer 35 | Hand 3 was close enough to our answer 36 | Test case passed! 37 | Running a simulation with 30000 draws for 2 hands... 38 | Hand 0 was close enough to our answer 39 | Hand 1 was close enough to our answer 40 | Test case passed! 41 | - Next, few Seven Card Stud hands 42 | Running a simulation with 30000 draws for 2 hands... 43 | Hand 0 was close enough to our answer 44 | Hand 1 was close enough to our answer 45 | Test case passed! 46 | Running a simulation with 40000 draws for 3 hands... 47 | Hand 0 was close enough to our answer 48 | Hand 1 was close enough to our answer 49 | Hand 2 was close enough to our answer 50 | Test case passed! 51 | - Then one from a completely made up poker variant 52 | Running a simulation with 100000 draws for 6 hands... 53 | Hand 0 was close enough to our answer 54 | Hand 1 was close enough to our answer 55 | Hand 2 was close enough to our answer 56 | Hand 3 was close enough to our answer 57 | Hand 4 was close enough to our answer 58 | Hand 5 was close enough to our answer 59 | Test case passed! 60 | 61 | Overall Grade: A 62 | -------------------------------------------------------------------------------- /c4prj3_finish/input.c: -------------------------------------------------------------------------------- 1 | ../c4prj2_input/input.c -------------------------------------------------------------------------------- /c4prj3_finish/input.h: -------------------------------------------------------------------------------- 1 | ../c4prj2_input/input.h -------------------------------------------------------------------------------- /c4prj3_finish/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "cards.h" 6 | #include "deck.h" 7 | #include "eval.h" 8 | #include "future.h" 9 | #include "input.h" 10 | 11 | int win_hand(deck_t ** deck_array,int n_hands){ 12 | int w[n_hands+1]; 13 | for(int u=0;u0) w[i]++; 22 | else if (v<0) w[j]++; 23 | else w[n_hands]++; 24 | } 25 | } 26 | unsigned largest= 0; 27 | for(int x=0;x w[largest])largest=x; 29 | } 30 | int count=0; 31 | if(w[n_hands]>0){ 32 | for(int x=0;x1) return n_hands; 37 | return largest; 38 | } 39 | 40 | int main(int argc, char ** argv) { 41 | //check arguments 42 | if (argc < 2 ){ 43 | fprintf(stderr, "not enough number of arguments\n"); 44 | return EXIT_FAILURE ; 45 | } 46 | //Open the input file and read the hands in it 47 | FILE* f=fopen(argv[1],"r"); 48 | if(f == NULL) { 49 | fprintf(stderr, "not valid input file\n"); 50 | return EXIT_FAILURE ; 51 | } 52 | deck_t ** deck_array=NULL; 53 | size_t n_hands =0 ; 54 | future_cards_t * fc=malloc(sizeof(*fc)); 55 | fc->decks=NULL; 56 | fc->n_decks=0; 57 | deck_array=read_input(f,&n_hands,fc); 58 | //Create a deck with the remaining cards 59 | deck_t* sh=build_remaining_deck(deck_array, n_hands); 60 | int win_array[n_hands+1]; 61 | for(int u=0;un_decks-1 ;o>=0;o--){ 85 | if(fc->decks[o].n_cards != 0) free(fc->decks[o].cards); 86 | } 87 | free(fc->decks); 88 | free(fc); 89 | free_deck(sh); 90 | 91 | if(fclose(f) != 0){ 92 | fprintf(stderr, "close file"); 93 | return EXIT_FAILURE; 94 | } 95 | return EXIT_SUCCESS; 96 | } 97 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/answers.txt: -------------------------------------------------------------------------------- 1 | test01.txt: Hand 0 = 95%, Hand 1 = 5% 2 | test02.txt: Hand 0 = 14%, Hand 1 = 86% 3 | test03.txt: Hand 0 = 69%, Hand 1 = 31% 4 | test04.txt: Hand 0 = 63%, Hand 1 = 36% 5 | test05.txt: Hand 0 = 27%, Hand 1 = 73% 6 | test06.txt: Hand 0 = 20%, Hand 1 = 80% 7 | test07.txt: Hand 0 = 100%, Hand 1 = 0% 8 | test08.txt: Hand 0 = 1%, Hand 1 = 1% 9 | test09.txt: Hand 0 = 0%, Hand 1 = 4% 10 | test10.txt: Hand 0 = 0%, Hand 1 = 18% 11 | test11.txt: Hand 0 = 41%, Hand 1 = 58% 12 | test12.txt: Hand 0 = 84%, Hand 1 = 16% 13 | test13.txt: Hand 0 = 77%, Hand 1 = 23% 14 | test14.txt: Hand 0 = 0%, Hand 1 = 0%, Hand 2 = 31% 15 | test15.txt: Hand 0 = 2%, Hand 1 = 13%, Hand 2 = 84% 16 | test16.txt: Hand 0 = 4%, Hand 1 = 5%, Hand 2 = 12%, 17 | Hand 3 = 8%, Hand 4 = 58%, Hand 5 = 12% 18 | 19 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test01.txt: -------------------------------------------------------------------------------- 1 | Qs Qc 7h 2h Qd 0c ?0 2 | Ks Kc 7h 2h Qd 0c ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test02.txt: -------------------------------------------------------------------------------- 1 | 0c 9d Qs Ad Ah 4d ?0 2 | Kh 4h Qs Ad Ah 4d ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test03.txt: -------------------------------------------------------------------------------- 1 | 0h 0d ?0 ?1 ?2 ?3 ?4 2 | Ah 9h ?0 ?1 ?2 ?3 ?4 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test04.txt: -------------------------------------------------------------------------------- 1 | Kc Qc ?0 ?1 ?2 ?3 ?4 2 | 4s 2s ?0 ?1 ?2 ?3 ?4 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test05.txt: -------------------------------------------------------------------------------- 1 | Kc Qc 4d 8s 8d ?0 ?1 2 | 4s 2s 4d 8s 8d ?0 ?1 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test06.txt: -------------------------------------------------------------------------------- 1 | Kc Qc 4d 8s 8d 9c ?0 2 | 4s 2s 4d 8s 8d 9c ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test07.txt: -------------------------------------------------------------------------------- 1 | Kc Qc 4d 8s 8d 9c 9s 2 | 4s 2s 4d 8s 8d 9c 9s 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test08.txt: -------------------------------------------------------------------------------- 1 | 7c 6h ?4 ?3 ?2 ?1 ?0 2 | 7h 6c ?4 ?3 ?2 ?1 ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test09.txt: -------------------------------------------------------------------------------- 1 | 7c 6h Jh 4c 2h ?1 ?0 2 | 7h 6c Jh 4c 2h ?1 ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test10.txt: -------------------------------------------------------------------------------- 1 | 7c 6h Jh 4c 2h 8h ?0 2 | 7h 6c Jh 4c 2h 8h ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test11.txt: -------------------------------------------------------------------------------- 1 | 0c 6d ?4 ?3 ?2 ?1 ?0 2 | Js 4c ?4 ?3 ?2 ?1 ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test12.txt: -------------------------------------------------------------------------------- 1 | 0c 6d 0s 7d 5c ?1 ?0 2 | Js 4c 0s 7d 5c ?1 ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test13.txt: -------------------------------------------------------------------------------- 1 | 0c 6d 0s 7d 5c 8s ?0 2 | Js 4c 0s 7d 5c 8s ?0 3 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test14.txt: -------------------------------------------------------------------------------- 1 | Kh As ?0 ?1 ?2 2 | Kh As ?0 ?1 ?2 3 | 0s 9c ?1 ?2 ?3 4 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test15.txt: -------------------------------------------------------------------------------- 1 | 9c 7h Js 3h 3d ?0 ?1 2 | Qs 5h Js 3h 3d ?0 ?1 3 | Jc 7c Js 3h 3d ?0 ?1 4 | -------------------------------------------------------------------------------- /c4prj3_finish/provided-tests/test16.txt: -------------------------------------------------------------------------------- 1 | 3h ?0 Kc ?1 Qh ?2 2h 2 | Ac ?1 Jd ?2 3d ?3 5c 3 | Qc ?2 Qd ?3 3h ?4 2s 4 | Js ?3 0s ?4 9c ?5 6d 5 | 7h ?4 3h ?5 2h ?0 Ah 6 | Kd ?5 Qh ?0 Js ?1 As 7 | --------------------------------------------------------------------------------