Hactoberfest2021 icon indicating copy to clipboard operation
Hactoberfest2021 copied to clipboard

First commit

Open Anshuman7080 opened this issue 4 months ago • 0 comments

Dynamic Array Allocation:

Instead of declaring the array with an uninitialized size (int arr[n];), I used dynamic memory allocation (int* arr = new int[n];) after reading the value of n. This avoids undefined behavior. Input Validation:

Added a check to ensure that n is a positive integer. If n is less than or equal to zero, the program will print a message and exit. Sorting the Array:

Included a call to sort(arr, arr + n); before the binary search. This ensures the array is sorted, which is a prerequisite for binary search to work correctly. Improved User Prompts:

Made the prompts clearer (e.g., "Enter the number of elements:" and "Enter element X:") to enhance user experience. Clearer Search Feedback:

Changed the message when the key is not found to be more descriptive: "The number you entered is not in the array." Memory Management:

Added delete[] arr; to free the dynamically allocated memory after it is no longer needed, preventing memory leaks. Improved Readability:

Made minor adjustments to variable names and structure for clarity and maintainability. These changes enhance the functionality, reliability, and user experience of your binary search implementation.

Anshuman7080 avatar Oct 03 '24 18:10 Anshuman7080