Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First commit #366

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

First commit #366

wants to merge 2 commits into from

Conversation

Anshuman7080
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant