#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5];
int i,j;
int n=5,m=5;
int mid,low,high,item;
printf("Enter a number to search");
scanf("%d",&item);
printf("\nArray elements");
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
a[i-1][j-1]=(i-1)*5+j;
printf("%d ",a[i-1][j-1]);
}
low=0;
high=n*m-1;
mid=(low+high)/2;
i=mid/5;
j=mid%5;
while((low<high)&&(a[i][j]!=item))
{
if(a[i][j]<item)
{
low=i*5+j+1;
mid=(low+high)/2;
i=mid/5;
j=mid%5;
}
else
{
high=i*5+j-1;
mid=(low+high)/2;
i=mid/5;
j=mid%5;
}
}
if(a[i][j]==item)
{
printf("\nitem found at %d",i*5+j+1);
}
else
{
printf("\nitem not fount");
}
getch();
}
output
#include<conio.h>
void main()
{
clrscr();
int a[5][5];
int i,j;
int n=5,m=5;
int mid,low,high,item;
printf("Enter a number to search");
scanf("%d",&item);
printf("\nArray elements");
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
a[i-1][j-1]=(i-1)*5+j;
printf("%d ",a[i-1][j-1]);
}
low=0;
high=n*m-1;
mid=(low+high)/2;
i=mid/5;
j=mid%5;
while((low<high)&&(a[i][j]!=item))
{
if(a[i][j]<item)
{
low=i*5+j+1;
mid=(low+high)/2;
i=mid/5;
j=mid%5;
}
else
{
high=i*5+j-1;
mid=(low+high)/2;
i=mid/5;
j=mid%5;
}
}
if(a[i][j]==item)
{
printf("\nitem found at %d",i*5+j+1);
}
else
{
printf("\nitem not fount");
}
getch();
}
output
No comments:
Post a Comment