wite a programme in c to linear search a data using flag and
without using flags?
Answers were Sorted based on User's Feedback
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 |
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 |
Answer / st0le
sorry, abt the typho in the previous post...
"ub" shud be "n"...
| Is This Answer Correct ? | 1 Yes | 2 No |
can anyone proide me reading material on svit00ef27@yahoo.com please thanx in advance
How the processor registers can be used in C ?
What is the difference function call by value & function call by reference?
Explain why can’t constant values be used to define an array’s initial size?
What are runtime error?
what is differnence b/w macro & functions
5 Write an Algorithm to find the maximum and minimum items in a set of ‘n’ element.
how can u print a message without using any library function in c
What are the types of arrays in c?
4. main() { int c=- -2; printf("c=%d",c); }
What is the difference between int and float?
Explain how can I make sure that my program is the only one accessing a file?