From 579738f7a35f19f547a3190ead686cf9e0d04472 Mon Sep 17 00:00:00 2001 From: abiduzz420 Date: Fri, 24 Nov 2017 11:18:36 +0530 Subject: [PATCH 1/2] dynamic memory allocation and modified variable names --- search/Binary_Search.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/search/Binary_Search.c b/search/Binary_Search.c index 666d03a44..35713e4ab 100644 --- a/search/Binary_Search.c +++ b/search/Binary_Search.c @@ -1,35 +1,38 @@ #include +#include + int main() { - int n, a[30], item, i, j, mid, top, bottom; + int n_terms, *arr, item, i, j, mid, top, bottom; printf("Enter how many elements you want:\n"); // no of elements - scanf("%d", &n); - printf("Enter the %d elements in ascending order\n", n); - for (i = 0; i < n; i++) { - scanf("%d", &a[i]); + scanf("%d", &n_terms); + arr = (int*)malloc(sizeof(int)*n_terms); + printf("Enter the %d elements in ascending order\n", n_terms); + for (i = 0; i < n_terms; i++) { + scanf("%d", &arr[i]); } printf("\nEnter the item to search\n"); // Target element to be searched scanf("%d", &item); bottom = 1; - top = n; + top = n_terms; do { mid = (bottom + top) / 2; - if (item < a[mid]) + if (item < arr[mid]) top = mid - 1; // Here we are dividing the array into two equal parts - else if (item > a[mid]) /* if target element > mid part of array , we do the search in the upper part of the array + else if (item > arr[mid]) /* if target element > mid part of array , we do the search in the upper part of the array else search in the lower part */ bottom = mid + 1; - } while (item != a[mid] && bottom <= top); + } while (item != arr[mid] && bottom <= top); - if (item == a[mid]) { + if (item == arr[mid]) { printf("Binary search successfull!!\n"); - printf("\n %d found in position: %d\n", item, mid + 1); + printf("%d found in position: %d\n", item, mid + 1); } else { - printf("\n Search failed\n %d not found\n", item); + printf("Search failed\n%d not found\n", item); } return 0; } From 982a0752e3f24dc0255418e85fdd1402020d17cf Mon Sep 17 00:00:00 2001 From: abiduzz420 Date: Fri, 24 Nov 2017 11:22:34 +0530 Subject: [PATCH 2/2] dynamic memory allocation in linear_search.c --- search/linear_search.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/search/linear_search.c b/search/linear_search.c index b5b6d37ef..bd88056ff 100644 --- a/search/linear_search.c +++ b/search/linear_search.c @@ -1,21 +1,22 @@ #include - +#include int main() { - int array[100], search, c, n; + int *array, search, c, n_terms; printf("Enter the number of elements in array\n"); - scanf("%d",&n); // Total no of elements + scanf("%d",&n_terms); // Total no of elements - printf("Enter %d integer(s)\n", n); + array = (int*)malloc(sizeof(int)*n_terms); + printf("Enter %d integer(s)\n", n_terms); - for (c = 0; c < n; c++) + for (c = 0; c < n_terms; c++) scanf("%d", &array[c]); // Reading the elements printf("Enter the number to search\n"); // Target element to be searched scanf("%d", &search); - for (c = 0; c < n; c++) + for (c = 0; c < n_terms; c++) { if (array[c] == search) /* if required element found */ { @@ -23,7 +24,7 @@ int main() break; } } - if (c == n) + if (c == n_terms) printf("%d is not present in array.\n", search); // Element not found return 0;