Question

In: Computer Science

Write a binary search algorithm in C language by performing following steps: Prompt the user to...

  1. Write a binary search algorithm in C language by performing following steps:
  2. Prompt the user to enter the number of array elements (say, N).  
  3. Read the N different values (define these values to be of type integer).  
  4. Read the element (integer value) which you want to search.
  5. Invoke a function to display the values in the array. The function should take the array reference and number of elements as arguments and should have the return type as void. The function should loop through the array and print the array values one per line.
  6. Invoke a function to search in the array using binary search (this function should take the array reference and number of elements as the arguments and the search element). The binary search function should have the return type as integer and it should return the array index when the search value is found, otherwise it should return -1.
  7. The main program should print a message if the search value is found and the corresponding position in the array where it was found. If the search value was not found, then the main program should also print a message saying that the search value does not exist in the given array.

Solutions

Expert Solution

Binary search program:

//declare header file
#include <stdio.h>
//display_array function displays the array eleements
void display_array(int* a,int N){
int i=0;
printf("array eleements are:");
//traversing through array to print eleements
for(i=0;i<N;i++){
printf("%d\t",a[i]);
}
}
//binary_search function returns the search index in the array
int binary_search(int* a,int N,int k){
//declaration of variables
int first,last,middle;
  
first = 0;
last = N - 1;
middle = (first+last)/2;
//traversing through array
while (first <= last) {
//checking if the search element is less than mid value
if (a[middle] < k)
first = middle + 1;
//if the search element is found return the index of element
else if (a[middle] == k) {
return middle;
break;
}
else
last = middle - 1;

middle = (first + last)/2;
}
if (first > last)
return -1;
  
}
//start of main
int main()
{
//declaration of variables
int c,N,i,k;
//asking the user to enter array size
printf("Enter the number of elements");
scanf("%d",&N);
int a[N];
//asking the user to enter array elements
printf("enter array elements\n");
for(i=0;i<N;i++){
scanf("%d",&a[i]);
}
//prompting the user for search element
printf("\nEnter the element which you want to search:");
scanf("%d",&k);
//function calling
display_array(&a,N);
//storing the return value of binary_search function in c
c=binary_search(&a,N,k);
//printing the search element index
printf("\nkey element found at location:%d",c);
return 0;
}

OUTPUT:

NOTE:

1.There are warnings but the program will run successfully.


Related Solutions

Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
2) Write an algorithm to compute the area of circles. Your algorithm should prompt the user...
2) Write an algorithm to compute the area of circles. Your algorithm should prompt the user to take the number of circles and their radius values. Then it should compute the areas of each circle. Finally, your algorithm will print both radius and area of all circles into the output. [N.B. you need to use iterative statement for solving the problem. Consider Pi = 3.14159] Input: Area of how many Circles you want to compute? 3 Key in radius values:...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Write a program in C language that implements the logic of Binary Trees with the following...
Write a program in C language that implements the logic of Binary Trees with the following requirements: 1- Elements of the tree should be read from the user. 2- Keep the binary tree heigh-balanced that is the absloute value of ((hight of left sub-tree) - ( height of right sub-tree)) should not be greater than 1. If so, then the binary tree is no longer balanced. Hint: The best approach is to maintain balance during insertion. *Remember, we are talking...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT