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 context in c?

0 Answers  


how to count no of words,characters,lines in a paragraph.

0 Answers  


What is the difference between variable declaration and variable definition in c?

0 Answers  


What is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?

0 Answers  


what is pointer

1 Answers   TCS,






Explain what is the purpose of "extern" keyword in a function declaration?

0 Answers  


What is a char in c?

0 Answers  


What is structure in c language?

0 Answers  


Disadvantages of C language.

0 Answers   Impetus,


what wud be the output? main() { char *str[]={ "MANISH" "KUMAR" "CHOUDHARY" }; printf("\nstring1=%s",str[0]); printf("\nstring2=%s",str[1]); printf("\nstring3=%s",str[2]); a)string1=Manish string2=Kumar string3=Choudhary b)string1=Manish string2=Manish string3=Manish c)string1=Manish Kumar Choudhary string2=(null) string3=(null) d)Compiler error

9 Answers   Infosys,


Explain goto?

0 Answers  


#define MAX 3 main() { printf("MAX = %d ",MAX ); #undef MAX #ifdef MAX printf("Vector Institute”); #endif }

1 Answers   Vector India,


Categories