Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
c=
#include<stdio.h>
int a[100],c,n,search;
void L_search(int);
void B_search(int);
int main()
{
	int ch;
	printf("Enter the number of element:");
	scanf("%d",&n);
	printf("Enter %d elements:\t",n);
	for(c=0;c<n;c++)
	{
	  scanf("%d",&a[c]);
	}
	printf("Enter that element to search>>");
	scanf("%d",&search);
	printf("way to search an element...");
	printf("\n1.Linear search\n2.Binary search\n");
	printf("choose any...>>");
	scanf("%d",&ch);
	while(ch)
	{
		switch(ch)
		{
			case 1:
				printf("Searching in Linear way...\n");
				L_search(search);
				break;
			case 2:
				printf("Searching in binary way...\n");
				B_search(search);
				break;
			default :
				printf("Invalid option.\nPlease try again");
		}
	}
	return 0;
}
void L_search(int)
{
	 for(c=0;c<=n;c++)
   {
      if (a[c]==search)  
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
      else
      printf("Number Not Found");
   }
   
}
void B_search(int)
{
	int first = 0;
	int last = n-1;
	int middle = (first+last)/2;
	while (first <= last)
	{
		if(a[middle] < search)
		{
			first = middle + 1;

		}
		else if(a[middle] == search)
		{
			printf("%d found at location %d\n", search, middle+1);
			break;
		}
		else
		{
			 last = middle - 1;
		}
		middle = (first + last)/2;
	}
	if(first > last)
	{
		printf("Not found! %d is not present in the list.",search);
	}
}


What I have tried:

i have tried to run... program is running successfully but after the input value during execution of result ... the result loop is not breaking its statement...
Posted
Updated 23-Oct-17 21:28pm
v2

Your problem description is very vague,and doesn't explain a lot.
But...once you enter the loop in main, you have no way to exit, or change the options. Move the selection input code inside the loop, and add an "exit" option to the list.
 
Share this answer
 
Quote:
i have tried to run... program is running successfully but after the input value during execution of result ... the result loop is not breaking its statement...

Not clear at all.
When you don't understand what is doing your code, the solution is the debugger.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
You have a major flaw in your main function, try the following code instead.
C
int main()
{
  int ch;
  printf("Enter the number of element:");
  scanf("%d",&n);
  printf("Enter %d elements:\t",n);
  for(c=0;c<n;c++)
  {
    scanf("%d",&a[c]);
  }

  do
  {
    printf("Enter that element to search>>");
    scanf("%d",&search);
    printf("way to search an element...");
    printf("\n1.Linear search\n2.Binary search\n");
    printf("choose any...>>");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
      printf("Searching in Linear way...\n");
      L_search(search);
      break;
    case 2:
      printf("Searching in binary way...\n");
      B_search(search);
      break;
    case -1:
      printf("goodbye.\n");
      break;
    default :
      printf("Invalid option.\nPlease try again\n");
      break;
    }
  } while ( ch != -1);
  return 0;
}

Also you have a minor flaw in your L_search function, change to (you have to change its prototype as well):
C
void L_search()
{
   for(c=0;c<=n;c++)
   {
      if (a[c]==search)
      {
         printf("%d is present at location %d.\n", search, c+1);
         return;
      }
   }
   printf("Number Not Found\n");
}


I didn't analyze the B_search function, that's left to you as an exercise.

Please note: using global variables is a bad practice, I strongly encourage you to use local variables and pass them as function parameters.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900