diff --git a/ch_03/break_and_continue/compile.sh b/ch_03/break_and_continue/compile.sh new file mode 100755 index 0000000..0a4b409 --- /dev/null +++ b/ch_03/break_and_continue/compile.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ ! -d "bin" ]; then + mkdir bin + echo "Created bin directory" +fi + +gcc -c main.c -std=gnu99 -o bin/main.o -O3 + +gcc bin/main.o -std=gnu99 -o bin/a.out -O3 diff --git a/ch_03/break_and_continue/main.c b/ch_03/break_and_continue/main.c new file mode 100644 index 0000000..50ebac6 --- /dev/null +++ b/ch_03/break_and_continue/main.c @@ -0,0 +1,54 @@ +#include + +int main() +{ + const int ARRAY_SIZE = 9; + const int bills [] = { 60, 35, 61, 160, 30, 15, 200, 15, 25 }; + + printf("Going through bills\n"); + // + // Demonstrating the use of break + int bills_over = 0; + const int BILL_TOLERANCE_LIMIT = 59; + + for (int i = 0; i != ARRAY_SIZE; ++i) + { + if (bills[i] > BILL_TOLERANCE_LIMIT) + { + bills_over++; + } + + printf("%d\n", bills[i]); + + if (bills_over > 2) + { + printf("These bills are killing me\n"); + + break; + } + else if (bills_over == 2) + { + printf("If I see one more bill above %d...\n", BILL_TOLERANCE_LIMIT); + } + } + + + const int collection_of_numbers[] = { 12, 88, 43, 13, 97, 45, 333, 245, 1093 }; + + printf("\nGoing through a collection of numbers\n"); + + for (int i = 0; i != ARRAY_SIZE; ++i) + { + if (collection_of_numbers[i] % 2 == 0) + { + // skipt even numbers + continue; + } + + printf("%d ", collection_of_numbers[i]); + } + + printf("\n"); + + return 0; +} diff --git a/ch_03/goto/compile.sh b/ch_03/goto/compile.sh new file mode 100755 index 0000000..0a4b409 --- /dev/null +++ b/ch_03/goto/compile.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ ! -d "bin" ]; then + mkdir bin + echo "Created bin directory" +fi + +gcc -c main.c -std=gnu99 -o bin/main.o -O3 + +gcc bin/main.o -std=gnu99 -o bin/a.out -O3 diff --git a/ch_03/goto/main.c b/ch_03/goto/main.c new file mode 100644 index 0000000..b259376 --- /dev/null +++ b/ch_03/goto/main.c @@ -0,0 +1,50 @@ +#include + + +/* From what i gathered from the author of the C programming boook is that + * gotos are highly discouraged, although they have their uses. Goto is + * essentially a stronger version of the break statement. Code can become + * more difficult to read as more gotos are used. + */ + +int main() +{ + const int ARRAY_SIZE = 9; + const int numbers1[] = { 60, 35, 61, 160, 30, 15, 200, 15, 25 }; + const int numbers2[] = { 3, 2, 50, 4, 88, 3, 29, 91, 39 }; + + printf("Going through numbers, trying to find a negative number\n"); + // Demonstrating the use of goto + int negative_product = 0; + const int BILL_TOLERANCE_LIMIT = 59; + + for (int i = 0; i != ARRAY_SIZE; ++i) + { + for (int j = 0; j != ARRAY_SIZE; ++j) + { + int product = numbers1[i] * numbers2[j]; + + if (product < 0) + { + negative_product = product; + + goto found_negative; + } + + if ((j + 1) == ARRAY_SIZE && (i + 1) == ARRAY_SIZE) + { + goto no_negative_found; + } + } + } + +no_negative_found: + printf("Did not find any negative products\n"); + + return 0; +found_negative: + printf("Found a negative product: %d\n", negative_product); + + + return 0; +}