wite a programme in c to linear search a data using flag and
without using flags?

Answers were Sorted based on User's Feedback



wite a programme in c to linear search a data using flag and without using flags?..

Answer / ruchi

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,a[15],num;
printf("\nHow many elements are there ");
scanf("%d",&n);
printf("\nEnter the elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element do you want to search ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
printf("\nElement is present ");
break;
}
else if((a[i]!=num)&&(i==n-1
))
{
printf("\nElement is not present");
}
}
getch();
}

Is This Answer Correct ?    19 Yes 8 No

wite a programme in c to linear search a data using flag and without using flags?..

Answer / st0le

int linearSearch(int a[],int ub,int key)
{
for(int i=0;i<n;i++)
if(a[i] == key) return i;
return -1; //not found!
}

int linearSearch(int a[],int ub,int key)
{
int flag = 0;
for(int i=0;i<n;i++)
if(a[i] == key)
{ flag = 1; break; }

return (flag)? i:-1; //not found!
}

Is This Answer Correct ?    7 Yes 0 No

wite a programme in c to linear search a data using flag and without using flags?..

Answer / st0le

sorry, abt the typho in the previous post...

"ub" shud be "n"...

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Interview Questions

What is c language & why it is used?

0 Answers  


c program to input values in a table(using 2D array) and print odd numbers from them

1 Answers  


how to write palindrome program?

3 Answers  


What is difference between array and structure in c?

0 Answers  


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

7 Answers   HCL,






Tell me when would you use a pointer to a function?

0 Answers  


How can I sort a linked list?

0 Answers  


What is volatile

2 Answers  


what is the difference between while and do while?

2 Answers  


write a c programme for add of two numbers with out use of arthematic operators

2 Answers  


What does s c mean in text?

0 Answers  


How do you sort filenames in a directory?

0 Answers  


Categories