From 4ca75f9f0921652984e60190e1dd872f6efeeab3 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 19 Oct 2020 21:14:21 -0400 Subject: [PATCH] Pick up on 3.4 Switch --- ch_03/exercise_03-01/main.c | 136 +++++++++++++++++++++++++++++ ch_03/if-else/main.c | 32 +++++++ ch_03/main_template.c | 6 ++ ch_03/statements_and_blocks/main.c | 22 +++++ 4 files changed, 196 insertions(+) create mode 100644 ch_03/exercise_03-01/main.c create mode 100644 ch_03/if-else/main.c create mode 100644 ch_03/main_template.c create mode 100644 ch_03/statements_and_blocks/main.c diff --git a/ch_03/exercise_03-01/main.c b/ch_03/exercise_03-01/main.c new file mode 100644 index 0000000..d9893b2 --- /dev/null +++ b/ch_03/exercise_03-01/main.c @@ -0,0 +1,136 @@ +/* + * + * Exercise 3-1. Our binary search makes two tests inside the loop, when one would + * suffice (at the price of more tests outside). Write a version with only one test + * inside the loop and measure the difference in run-time. + * + * Author: Kun Deng + */ + +#include +#include +#include + +#define ARRAY_SIZE 99900000 + +long v[ARRAY_SIZE]; + +long binsearch(long x, long v[], long n); +long binsearch_improved(long x, long v[], long n); + +void populate_array(const long size); +void print_array(const long v[], long size); + + +int main() +{ + extern long v[]; + + populate_array(ARRAY_SIZE); + // print_array(v, ARRAY_SIZE); + + long x = 901103; + + printf("Starting binary search. Trying to find %ld...\n", x); + + long index = binsearch(x, v, ARRAY_SIZE); + printf("Regular binary search complete\n"); + long index_improved = binsearch_improved(x, v, ARRAY_SIZE); + printf("Improved binary search complete\n"); + + if (index < 0) + { + printf("%ld was not found in the array\n", x); + + return -1; + } + + printf("Binary search: Value %ld is at index %ld of the array\n", x, index); + printf("Binary search improved: Value %ld is at index %ld of the array\n", x, index_improved); + + return 0; +} + + +long binsearch(long x, long v[], long n) +{ + long low, mid, high; + + low = 0; + high = n - 1; + + while (low <= high) + { + mid = (low + high) / 2; + + if (x < v[mid]) + { + high = mid - 1; + } + else if (x > v[mid]) + { + low = mid + 1; + } + else + { + return mid; + } + } + + return -1; +} + +long binsearch_improved(long x, long v[], long n) +{ + long low, mid, high; + + low = 0; + high = n - 1; + mid = (low + high) / 2; + + for (; (low <= high) && x != v[mid]; mid = (low + high) / 2) + { + if (x < v[mid]) + { + high = mid - 1; + } + else + { + low = mid + 1; + } + } + + if (v[mid] == x) + { + return mid; + } + + + return -1; +} + + +void populate_array(long size) +{ + extern long v[]; + long i = 0; + v[i] = 1; // First element will start at 1 + + for (i = 1; i != size; ++i) + { + long elem = v[i]; + v[i] = elem + (i * 2) + 1; + } +} + +void print_array(const long v[], long size) +{ + printf("array is: "); + + for (long i = 0; i != size; ++i) + { + printf("%ld ", v[i]); + } + + printf("\nWith %ld elements\n", size); +} diff --git a/ch_03/if-else/main.c b/ch_03/if-else/main.c new file mode 100644 index 0000000..8a1137c --- /dev/null +++ b/ch_03/if-else/main.c @@ -0,0 +1,32 @@ +/* + * + * Demonstration of the if-else statement + * + * Author: Kun Deng + */ + +#include + + +int main() +{ + const int x = 15; + const int compared_value = 1024; + + // The expression within the if statment evaluates the expression. + // The expression is true if the expression is a non-zero number. + // Otherwise it's false if 0. The reason for this if you're famililar + // with programming languages that have bool or boolean is the in C, + // there isn't a boolean type. Or well at least in not standards before + // C99. + if ((x < compared_value) != 0) + { + printf("Statement is true\n"); + } + else + { + printf("Statement is false\n"); + } + + return 0; +} \ No newline at end of file diff --git a/ch_03/main_template.c b/ch_03/main_template.c new file mode 100644 index 0000000..cbf1a28 --- /dev/null +++ b/ch_03/main_template.c @@ -0,0 +1,6 @@ +/* + * + * Exercise 3- + * + * Author: Kun Deng + */ diff --git a/ch_03/statements_and_blocks/main.c b/ch_03/statements_and_blocks/main.c new file mode 100644 index 0000000..e47506a --- /dev/null +++ b/ch_03/statements_and_blocks/main.c @@ -0,0 +1,22 @@ +/* + * + * Brief demonstration of statements and blocks + * + * Author: Kun Deng + */ + +#include + + +int main() +{ + // Below are some statements. A statement is considered one when + // the semicolon is the last character of the statement. The + // semicolon serves as a statement terminator. + + int x = 48; + x /= 4; + printf("Print statment %d\n", x); + + return 0; +}