void search(int a[],int n,int k)//a is array, n is element to be found, m is size of array
{
int e,ctr=0;
int m=0;//m is the location index
for(int i=0;i<k;i++)
{
if(a[i]==n)
{
e=a[i];
m=i;
ctr++;
}
}
if(ctr>=1)
printf("Value Found at this location %d", (m+1));
else if(ctr==0)
printf("%d Not found", n);
if(ctr>=2)
printf(n+" is found "+ctr+"times");
}//end of search method
}
/*
If m is not initialized with 0?
Ans: We will get error at (m+1) bcoz m contains garbage value
Enter the size of an array
4
Enter values into Array
7
6
5
4
Enter which value you want to search
6
Value Found at 2
E:\Raj\JavaPrograms\Balram\Assignment\Arrays>java MainSearch
Enter the size of an array
4
Enter values into Array
1
2
2
3
Enter which value you want to search
2
Value Found at 3
2Value found 2times
*/
{
int e,ctr=0;
int m=0;//m is the location index
for(int i=0;i<k;i++)
{
if(a[i]==n)
{
e=a[i];
m=i;
ctr++;
}
}
if(ctr>=1)
printf("Value Found at this location %d", (m+1));
else if(ctr==0)
printf("%d Not found", n);
if(ctr>=2)
printf(n+" is found "+ctr+"times");
}//end of search method
}
/*
If m is not initialized with 0?
Ans: We will get error at (m+1) bcoz m contains garbage value
Enter the size of an array
4
Enter values into Array
7
6
5
4
Enter which value you want to search
6
Value Found at 2
E:\Raj\JavaPrograms\Balram\Assignment\Arrays>java MainSearch
Enter the size of an array
4
Enter values into Array
1
2
2
3
Enter which value you want to search
2
Value Found at 3
2Value found 2times
*/
You may like the following posts:
No comments:
Post a Comment