write a program to search for an element in a given array.
If the array was found then display its position otherwise
display appropriate message in c language

Answers were Sorted based on User's Feedback



write a program to search for an element in a given array. If the array was found then display its ..

Answer / anuja kulkarni

#include<stdio.h>
#include<conio.h>

void main()
{
int a[5],i;
int ele,temp=0,pos=0;

clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);

// searching for the element

for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
pos=i;
}
}

if (temp==1)
printf("Element found %d , position==%d",ele,pos);
else
printf("Element not found\n");
} // end of main()

Is This Answer Correct ?    483 Yes 132 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / sharan

#include<stdio.h>
#include<conio.h>

void main()
{
int a[5],i;
int ele,temp=0;

clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);

// searching for the element

// searching for the element
for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
printf("Element found %d , position=%d\n",ele,i);
}
}
if (!temp)
printf("Element not found\n");
} // end of main()

Is This Answer Correct ?    149 Yes 97 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / bishwas bidari

#include<stdio.h>
#include<conio.h>

void main()
{
int a[5],i;
int ele,temp=0,pos=0;

clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be search\n");
scanf("%d",&ele);

/* searching for the element*/

for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
pos=i;
}
}

if (temp==1)
printf("Element found %d , position==%d",ele,pos);
else
printf("Element not found\n");
}

Is This Answer Correct ?    85 Yes 52 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / poorni

#include<stdio.h>
#include<conio.h>

int main()
{
int a[20],i,temp=0,n,pos=0,element;
//clrscr();
printf("Search an Element in Array\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the value for elements: ");
scanf("%d",&a[i]);
}
printf("Enter the searching element: ");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(a[i]==element)
{
temp=1;
pos=i;
}
}
if (temp==1)
printf("Element found %d , position=%
d",element,pos);
else
printf("Element not found\n");
getch();
}

Is This Answer Correct ?    56 Yes 31 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / kapil hansrajani

include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,n;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to search : ");
scanf("%d",&j);
for(i=0;i<n;i++)
{
if(a[i]==j)
{
printf("The position of the element %d is %d ",j,i);
break;
}
}
getch();
}

Is This Answer Correct ?    48 Yes 26 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / kirlosky

why should we mention temp=something ..... wat is temp here
plz explain

Is This Answer Correct ?    70 Yes 49 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / sunil makwana

#include<stdio.h>
#include<conio.h>
void searchval(int a[5],int n)
{
int i,temp=0,pos=0;

for (i=0; i<5; i++)
{
// printf("value of array:: %d",i);
if (a[i]==n)
{
temp=1;
pos=i;
}
}
if (temp==1)
{

printf("Element found %d , position==%d",n,pos);
}
else
{
printf("Element not found\n");
}




}
void main()
{
int a[5],i,n;
int ele,pos,temp;

clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);

searchval(a,ele);
getch();
}

Is This Answer Correct ?    29 Yes 17 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / abdinasir maxamed abdulle

Array that display search using c languege

Is This Answer Correct ?    16 Yes 8 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / mr. x

#include<stdio.h>
#include<conio.h>
void main ()
{
int a[5],b,c,i;
clrscr();
printf("
Enter no. of elements: ");
scanf("%d",&c);
i=0;
while(i<c)
{
scanf("%d",&a[i]);
i++;
}
printf("
Enter the elements to be searched: ");
scanf("%d",&b);
i=0;
while(i<c &b!=a[i])
{
i++;
}
if (i<c)
{
printf("number found");
i++;
}
else
{
printf(" number not found");
}
getch();
}

Is This Answer Correct ?    8 Yes 5 No

write a program to search for an element in a given array. If the array was found then display its ..

Answer / ghost

#include<stdio.h>
#include<conio.h>

void main()
{
int a[100],i,element,temp,pos;

clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&element);

// searching for the element

for (i=0; i<5; i++)
{
if (a[i]==element)
{
temp=1;
pos=++i;
}
}

if (temp==1)
printf("%d Element found at position==%d",element,pos);
else
printf("Element not found\n");
getch();

} // end of main()

Is This Answer Correct ?    13 Yes 12 No

Post New Answer

More C Interview Questions

Write a programm such that if user enter 11.25 it roundup to 11 but if user enter 11.51 upto 11.99 it will round up to 12 i.e.;convert the floting point value into integer format as explain above..

2 Answers  


Function shall sum members of given one-dimensional array. However, it should sum only members whose number of ones in the binary representation is higher than defined threshold (e.g. if the threshold is 4, number 255 will be counted and 15 will not) - The array length is arbitrary - output the results to the stdout

0 Answers   XYZ,


I need help with the following lab. Can anyone explain it to me on how to approach this problem. Coding would be great too !!! No busy waiting allowed. Remember that Java monitors are 'signal and continue'. The input data is called the database (DB). In this problem, it consists of a single variable initialized in the main thread, to contain the current time of day in HH:MM:SS:mmm format (mmm is milliseconds) truncated to 3 digits. Threads follow these rules: 1. Readers may all attempt to read at the same time, but NOT if a writer is writing (i.e.; the writer is in the monitor. 2. Writing is exclusive (only 1 writer at a time, no readers while writing). 3. Options: (to be specified by instructor) a. Readers have absolute priority over writers. b. Writers have absolute priority over readers c. When a reader arrives and a writer is waiting, the reader is suspended behind the writer instead of being admitted immediately. Thus, a writer waits for readers that were running when it arrived, but does not to wait for new readers. You may program this in C++ or Java. There are 4 controlling input values: 1. r – number of readers 2. w – number of writers 3. R – delay for a reader to restart 4. W – delay for a writer to restart Create the following program consisting of 1 main process (your main program) with n threads: 1. The main thread: a. Creates the 'n' threads needed. Of these, r of them are readers and w of them are writers. Starts all readers and writers. b. Waits for all 'n' threads to complete. You may use any method to detect when they are all complete. c. Prints out the resulting outputs from the threads. 2. Threads: a. When a thread starts to run, it immediately tries to enter the monitor. b. A reader thread reads the DB, outputs the exact message below, then exits: >>> DB value read =: HH:MM:SS:mmm by reader number: rr where the underlined text is replaced with actual data. c. A writer thread updates the DB with a new value (from the system time), outputs the exact message below, then exits: *** DB value set to: HH:MM:SS:mmm by writer number: ww where the underlined text is replaced with actual data. d. Each thread accesses the data a total of 10 times, exiting, then re-entering the monitor after each access. Hints: 1. File access *might* not be thread-safe, so you should be prepared to handle this. Read the documentation for the language you are using. 2. Suggested values for delays are: a. If looping, R >=1,000,000 and W>=100,000 b. If using “sleep”, then R=100 ms, W=50 ms. Additional: • Thread output is to ONE file used by ALL threads (so you need to synchronize access to it). • Readers must provide sufficient delay that results are useful (delay by R before re-trying). R is another input value. • Writers must also delay. Delay by W, updating the DB each time. W is an input. • Test your program with AT LEAST TWO different sets of values for r and w (#s of readers and writers) plus this set: r = 4, w=2. Basic operation of a thread: attempt to enter the monitor. If unsuccessful, you get put on a queue. When you get in, read or write the data (depending on type of thread), exit the monitor, wait the required delay amount, then try again. Repeat 10 times.

0 Answers  


What is a far pointer?What is the utility?

4 Answers  


What is the scope of an external variable in c?

0 Answers  






GIven a sequence of characters. How will you convert the lower case characters to upper case characters. ( Try using bit vector - sol given in the C lib -> typec.h)

0 Answers  


main() {int a=200*200/100; printf("%d",a); }

14 Answers   TCS,


what type of language is C?

13 Answers   Microsoft,


How to write the code of the program to swap two numbers with in one statement?

2 Answers  


what is a non volatile key word in c language?

1 Answers  


how to set Nth bit of variable by using MACRO

3 Answers   HCL,


What is define c?

0 Answers  


Categories