0% found this document useful (0 votes)
7 views3 pages

Odd Rule Presentation

Good Presentation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Odd Rule Presentation

Good Presentation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Searching

Finding an element with its position is called searching

Types of searching

1) Linear search
2) Binary search

Linear search

#include<stdio.h>

main()
{
int a[20],i,n,key,found=-1;

printf("Enter array size\n");


scanf("%d",&n);

printf("Enter elements into array\n");


for(i=0; i<n; i++)
scanf("%d",&a[i]);

printf("Enter the element to search\n");


scanf("%d",&key);

for(i=0; i<n; i++)


if(a[i]==key)
{
found=i;
break;
}

if(found==-1)
printf("Sorry element not found");
else
printf("Element %d is found at %d position",key,found);
}

Binary search

#include<stdio.h>
main()
{
int a[20],i,j,n,key,mid,low,high,temp;

printf("Enter array size\n");


scanf("%d",&n);

printf("Enter elements into array\n");


for(i=0; i<n; i++)
scanf("%d",&a[i]);

for(i=0; i<n-1; i++)


for(j=i+1; j<n; j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

printf("After sorting\n");
for(i=0; i<n; i++)
printf("%d\t",a[i]);

printf("Enter the element to search\n");


scanf("%d",&key);

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

if(a[mid]==key)
printf("Element %d found at %d position",key,mid+1);
else
printf("Sorry element not found");
}

Wap to find maximum element in an array

#include<stdio.h>
main()
{
int a[20],i,n,max,found;

printf("Enter array size\n");


scanf("%d",&n);

printf("Enter elements into array\n");


for(i=0; i<n; i++)
scanf("%d",&a[i]);

max=a[0];
for(i=1; i<n; i++)
if(a[i]>max)
{
max=a[i];
found=i;
}

printf("The maximum %d element found at %d position",max,found);


}

Addition of two arrays

#include<stdio.h>
main()
{
int a[10],b[10],c[10],m,n,i,temp;

printf("Enter the first array size\n");


scanf("%d",&m);

printf("Enter the elements into first array\n");


for(i=0; i<m; i++)
scanf("%d",&a[i]);

printf("Enter the second array size\n");


scanf("%d",&n);

printf("Enter the elements into second array\n");


for(i=0; i<n; i++)
scanf("%d",&b[i]);

if(m<n)
temp=m;
else
temp=n;

for(i=0; i<temp; i++)


c[i]=a[i]+b[i];

printf("After adding\n");
for(i=0; i<temp; i++)
printf("%d\t",c[i]);

You might also like