6. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –06] Introduce 1D Array manipulation and implement binary search


Algorithm

  • Step-1: Start
  • Step-2: [Read the value of N]-Read N
  • Step-3: [Enter the elements in the ascending order]- For i=0 to N-1 is to be read from keyboard
  • Step-4: [Enter a element for searching that could be a key element]
  • Step-5: Intitially set low=0 and high = N
  • Step-6: [Find out the middle value]-Mid = (low + high)/2;
  • Step-7: Check wheather the key value is lesser than the middle value than goto step 8
    • Check wheather the key value is greater than the middle value than goto step 9
  • Step-8: high = mid -1
  • Step-9: low = mid + 1
  • Step-10: if key value and mid- value are same then goto step 11 if not than goto step 12
  • Step-11: Successful search
  • Step-12: Not searched the key element
  • Step-13: Stop
See also  12. C PROGRAMMING LAB | Check Now

Flowchart

C PROGRAMMING -binary search

Program -6 source code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<stdio.h>
#include<string.h>
void main()
{
char name[50][50],key[50];
int n,i,low,high,mid,found=0;
printf("How many names to Read:\n");
scanf("%d",&n);
printf("Enter the Names in ascending order:\n");
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
printf("\nType the name to be searched:");
scanf("%s",key);
low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low+high)/2;
if(strcmp(name[mid],key)==0)
found=1;
else if(strcmp(name[mid],key)<0)
low=mid+1;
else
high=mid-1;
}
if(found==1)
printf("\nSearch successful and Name found in the list at position:%d",mid+1);
else
printf("Name not found and search Unsuccessful....!");
}
#include<stdio.h> #include<string.h> void main() { char name[50][50],key[50]; int n,i,low,high,mid,found=0; printf("How many names to Read:\n"); scanf("%d",&n); printf("Enter the Names in ascending order:\n"); for(i=0;i<n;i++) { scanf("%s",name[i]); } printf("\nType the name to be searched:"); scanf("%s",key); low=0; high=n-1; while(low<=high && !found) { mid=(low+high)/2; if(strcmp(name[mid],key)==0) found=1; else if(strcmp(name[mid],key)<0) low=mid+1; else high=mid-1; } if(found==1) printf("\nSearch successful and Name found in the list at position:%d",mid+1); else printf("Name not found and search Unsuccessful....!"); }
#include<stdio.h>
#include<string.h>

void main()
{
 char name[50][50],key[50];
 int n,i,low,high,mid,found=0;
 printf("How many names to Read:\n");
 scanf("%d",&n);
 printf("Enter the Names in ascending order:\n");
 for(i=0;i<n;i++)
 {
  scanf("%s",name[i]);
 }
 printf("\nType the name to be searched:");
 scanf("%s",key);

 low=0;
 high=n-1;
 while(low<=high && !found)
 {
  mid=(low+high)/2;
  if(strcmp(name[mid],key)==0)
   found=1;
     else if(strcmp(name[mid],key)<0)
   low=mid+1;
     else
   high=mid-1;
 }

 if(found==1)
  printf("\nSearch successful and Name found in the list at  position:%d",mid+1);
 else
  printf("Name not found and search Unsuccessful....!");

}

C PROGRAMMING -Output

  • How many names to Read:
    • 3
    • Enter the names in ascending order
      • abhishek
      • keshev
      • veresh
        • Type the name to be searched
          • keshav
  • Search successful and name found in the list at position : 2

C PROGRAMMING -Viva Questions

1] What is an array / definition of array

See also  5. COMPUTER GRAPHICS LAB | READ NOW

2] What are the types of array?

3] What is a multidimensional array?

4] How to declare and initialize one dimensional array?

5] What are the advantages of an array?

6] What is the difference between array and string?

7] Write the syntax of declaring an array.

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp Icon Join For Job Alerts