Question

#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending...

#include<stdio.h>

int main() {

      /* For binary search, the array should be arranged in ascending or descending order */

      int data[] = {1,2,3,4,5,6,7,8,9,10}; ****You might change the numbers for your program also

      /* 'min' use for starting location of the array, 'max' use for end location of the array and 'mid' use for middle location of the array */

      int min = 0, max = 10, search, mid = (max + min) / 2;

      printf("Enter a number to search:\n");

      scanf("%d", &search);

      while(min <= max) {

      ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******

      }

      if(min > max)

            printf("%d is not found!\n",search);

      return 0;

}

Looking for alternative solutions for this program code.

Homework Answers

Answer #1

#include<stdio.h>

int main()
{

//Binary Search (Considering Ascending Order of Elements in the Array)

int data[] = {1,2,3,4,5,6,7,8,9,10};

   //min, mid and max variables to indicate starting, middle and ending index (or position) of the array respectively

int min = 0, max = 10, search, mid = (max + min) / 2;

printf("Enter a number to search:\n");
scanf("%d", &search);

while(min <= max) {
  
   //If the element is greater than mid value, then mid becomes the min    
   if(data[mid]<search)
       min=mid+1;
  
   //If element is found in the middle
   else if(data[mid]==search)
   {
       printf("%d is found at index: %d",search,mid);
       break;
}
      
       //If the element is smaller than mid, then mid becomes the max
       else
       max=mid-1;
      
   //Updating the mid position
   mid= (max+min)/2;   

}
  
  
   //If the element is not found in the array  
if(min > max)
printf("%d is not found!\n",search);

return 0;

}

Sample Outputs:

Recursive Approach:

#include<stdio.h>

//bSearch() function to perform binary search with an array, min-index, max-index, search element as parameters
int bSearch(int arr[],int min,int max,int search)
{
   //variable to store mid positon
   int mid;
  
   if(min<=max)
   {
       mid = (min+max)/2;
      
       //If the element is found at mid position, then return the positon.
       if(arr[mid]==search)
           return mid;
      
       //If the search element is less than the mid value, then max=mid-1 and call bSearch()  
       else if(search<arr[mid])
           return bSearch(arr,min,mid-1,search);
      
       //If the search element is greater than the mid value, then min=mid+1 and call bSearch()  
       else
           return bSearch(arr,mid+1,max,search);  
      
   }
  
   //If the element is not found
   return -1;
  
}


void main()
{
   int arr[]={10,20,33,45,49,65,77,81,94,101};
  
   int result,search;
  
   printf("Enter a number to search:\n");
scanf("%d", &search);
  
  
   result=bSearch(arr,0,10,search);
  
   if (result==-1)
       printf("%d is not found",search);
   else
       printf("%d is found at index: %d",search,result);
  
  
}

Sample Runs:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8;...
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8; int x[8] = {2, 3, 5, 7, 11, 13, 17, 19}; scanf("%d",&k); aux = x[k]; for (int i = k; i < size - 1; i++) x[i] = x[ i + 1]; x[ size - 1] = aux;   for (int i = 0; i < size; i++) printf("%d ", x[i]); } change this program to Write a program to remove an element from an...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum; scanf("%d", &numitms); sum=0; for (j=1;j<=numitms;j++) { scanf("%d", &data); sum+=data; } printf("sum: %d\n",sum); return0; } Please test the answer using Pep/9. Thank you so much!
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
#include <stdio.h> int main( void ) {     double height, weight, bmi;     int bmievalcode;    ...
#include <stdio.h> int main( void ) {     double height, weight, bmi;     int bmievalcode;     while( 1 )     {         /* --> add code to input weight and height between here */         scanf("%lf %lf\n" , &weight &height)         bmi = weight * 703 / (height * height);         /* <-- and here */         /* --> add code to calculate bmi and bmievalcode between here */         if (bmi <= 00.0){             bmievalcode = -1;         else...
A programmer that you work with, Peter, is a jerk. He is responsible for an array...
A programmer that you work with, Peter, is a jerk. He is responsible for an array that is a key part of an important program and he maintains a sum of the array value.He won't give you access to this array; however, your boss has told you that you need to get input from the user and then place it into the array.Each evening Peter will scan the code and remove any illegal references to his array. Using pointers, access...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT