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

int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

1 Answers  


Write a program to model an exploding firecracker in the xy plane using a particle system

0 Answers   HCL,


How can i find first 5 natural Numbers without using any loop in c language????????

2 Answers   Microsoft,


main() { char a[4]="HELLO"; printf("%s",a); }

3 Answers   CSC,


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

0 Answers  






#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

1 Answers  


main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }

2 Answers  


main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }

2 Answers  


Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable.

5 Answers   Amazon, Microsoft,


void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above

2 Answers   HCL,


can u give me the c codings for converting a string into the hexa decimal form......

1 Answers  


main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

1 Answers  


Categories