how can i search an element in an array

Answers were Sorted based on User's Feedback



how can i search an element in an array..

Answer / pavan_mustyala

In C-Language, In general, we compare each element of the
array in a loop that iterates till the element is found.
This is called Linear search.

while(1)
{
// code here
if(element found)
break;
}

Here, execution time depends on the array size and position
of the element in the array.
For sorted arrays, we can use techniques such as binary
search using which execution is faster.

Is This Answer Correct ?    2 Yes 0 No

how can i search an element in an array..

Answer / palanisaran



my $element = 'Whatever you are searching for' ;
if (grep {$_ eq $element} @TheArray) {
print "Element '$element' found!\n" ;
}

Is This Answer Correct ?    4 Yes 9 No

Post New Answer

More C Code Interview Questions

can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

0 Answers   TCS,


#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }

1 Answers  


main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above

5 Answers   HCL,


main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

1 Answers  


What is the hidden bug with the following statement? assert(val++ != 0);

1 Answers  






How to access command-line arguments?

4 Answers  


void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }

3 Answers  


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


C program to print magic square of order n where n > 3 and n is odd

2 Answers   Accenture,


main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above

1 Answers   HCL,


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

0 Answers  


main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }

2 Answers  


Categories