Answer to Question #235306 in C for Tarurendra Kushwah

Question #235306
Write a c program to search page 11 in a book where you don’t visit every page from 1 to 49. You open the current page(Middle page) in the book and check its page number. If the current page number is greater than 11, then 11 is on the left side of the current page otherwise 11 is on the right side of the current page.
1
Expert's answer
2021-09-09T12:08:59-0400
#include <stdio.h>


#define SIZE 49


int searchPage(int pages[], int left, int rigth, int page)
{
	if (rigth >= left) {
		int middle = left + (rigth - left) / 2;
		// If the page is present at the middle itself
		if (pages[middle] == page)
			return middle;


		// If the current page number is greater than page, then page is on the left side of the current page
		if (pages[middle] > page)
			return searchPage(pages, left, middle - 1, page);


		// otherwise page is on the right side of the current page.
		return searchPage(pages, middle + 1, rigth, page);
	}
	return -1;
}


int main()
{
	int pages[SIZE];
	int i,page;


	for(i=0;i<SIZE;i++){
		pages[i]=i+1;
	}


	printf("Enter page to search: ");
	scanf("%d",&page);
	i=searchPage(pages,0,SIZE,page);
	if(i!=-1){
		printf("\nPage is present in the book.\n\n");	
	}else{
		printf("\nPage is NOT present in the book.\n\n");	
	}


	scanf("%d",&i);


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS