Answer to Question #235307 in C for Tarurendra Kushwah

Question #235307
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:56-0400
#include <stdio.h>




int searchPage(int pages[], int p){
	int start_index=0;
	int end_index=49;
	while (start_index <= end_index){
		int middle = start_index + (end_index- start_index )/2;
		if (pages[middle] == p)
			return middle;
		if (pages[middle] < p)
			start_index = middle + 1;
		else
			end_index = middle - 1;
	}
	return -1;
}
int main()
{
	int pages[49];
	int index;
	int i;
	for(i=0;i<49;i++){
		pages[i]=i+1;
	}




	index=searchPage(pages,11);
	if(index!=-1){
		printf("\nThe page 11 is present in the book.\n\n");	
	}else{
		printf("\nThe page 11 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