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

Write a program to compute the following 1!+2!+...n!

4 Answers  


#include main() { enum _tag{ left=10, right, front=100, back}; printf("left is %d, right is %d, front is %d, back is %d",left,right,front,back); }

0 Answers   Wilco,


What is pointer to pointer in c language?

0 Answers  


What is the difference between printf and scanf in c?

0 Answers  


2)#include<iostream.h> main() { printf("Hello World"); } the program prints Hello World without changing main() the o/p should be intialisation Hello World Desruct the changes should be a)iostream operator<<(iostream os, char*s) os<<'intialisation'<<(Hello World)<<Destruct b) c) d)none of the above

4 Answers   Siemens,


the maximum width of a c variable name can be a) 6 characters b) 8 characters c) 10 characters d) 20 characters

2 Answers  


what do structure language means?

3 Answers   Microsoft,


write a statement to display all the elements array M(in reverse order? int M[8]={20,21,22,23,24,25,26,27};

5 Answers  


What are structures and unions? State differencves between them.

0 Answers   iNautix,


how to create c progarm without void main()?

1 Answers   NIIT,


How is a pointer variable declared?

0 Answers  


What is the purpose of the statement: strcat (S2, S1)?

0 Answers  


Categories