answersLogoWhite

0


Best Answer

#include

#include

#include

void main(){

int arr[100],i,element,no;

clrscr();

printf("\nEnter the no of Elements: ");

scanf("%d", &no);

for(i=0;i

printf("\n Enter Element %d: ", i+1);

scanf("%d",&arr[i]);

}

printf("\nEnter the element to be searched: ");

scanf("%d", &element);

for(i=0;i

if(arr[i] == element){

printf("\nElement found at position %d",i+1);

getch();

exit(1);

}

}

printf("\nElement not found");

getch();

}

Output:

Enter the no of Elements: 5

Enter Element 1: 12

Enter Element 2: 23

Enter Element 3: 52

Enter Element 4: 23

Enter Element 5: 10

Enter the element to be searched: 23

Element found at position 2

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Non-recursive, linear search is achieved via a standard for or while loop. In effect, you start at the beginning of the collection (whether an array or a list), and repeatedly compare elements until a match is found. If the collection is sorted, then we can also exit the loop prematurely when the current element is larger than that being sought.

Example:

#include

bool Search( const int* Array, const int Elements, const int Value, int* Index )

{

for( *Index=0; *Index

{

if( Array[*Index] == Value )

return( true );

if( Array[*Index] > Value )

return( false );

}

return( false );

}

int main()

{

const int Elements = 10;

const int Array[Elements] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

int Index = 0;

printf( "Array: " );

for( ; Index

printf( "%d ", Array[Index] );

printf( "\n\n" );

for( int Value=1; Value<=20; ++Value )

{

if( Search( Array, Elements, Value, &Index ))

printf( "The value %d was found at element index %d\n", Value, Index );

else

printf( "The value %d does not exist; %d element%s checked\n", Value, Index + 1, Index?"s were":" was" );

}

return( 0 );

}

Output:

Array: 2 4 6 8 10 12 14 16 18 20

The value 1 does not exist; 1 element was checked

The value 2 was found at element index 0

The value 3 does not exist; 2 elements were checked

The value 4 was found at element index 1

The value 5 does not exist; 3 elements were checked

The value 6 was found at element index 2

The value 7 does not exist; 4 elements were checked

The value 8 was found at element index 3

The value 9 does not exist; 5 elements were checked

The value 10 was found at element index 4

The value 11 does not exist; 6 elements were checked

The value 12 was found at element index 5

The value 13 does not exist; 7 elements were checked

The value 14 was found at element index 6

The value 15 does not exist; 8 elements were checked

The value 16 was found at element index 7

The value 17 does not exist; 9 elements were checked

The value 18 was found at element index 8

The value 19 does not exist; 10 elements were checked

The value 20 was found at element index 9

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include<stdio.h> /*program to perform linear search*/

void main()

{

int a[5],se,i;

printf("Enter elements:");

for(i=0;i<5;i++)

{

scanf("%d",&a[i]);

}

printf("Enter search element:");

scanf("%d",&se);

for(i=0;i<5;i++)

{

if(a[i]==se)

{

printf("Element is at position number %d .\n",i+1);

}

else

printf("Invalid.\n");

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program for linear search non-recursive method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Are binary search and linear search already defined method in some class in java and what class is this?

You can check out the Arrays.binarySearch group of methods for searching sorted arrays. There is no predefined linear search for arrays, probably because it is trivially easy to implement. If you have some other data structure to search, the Collections.binarySearch methods should work for you. Most collections can also be converted to a List representation, which has a predefined indexOf method for linear searching.


Best first search program in c?

The best search programs to attempt writing in C are the following: Linear search (simplest), Binary search (faster) Hash search (fastest).


WAP in java to search for a given number using linear search?

// This method will search through nums for target. // It will return the index of target in nums, or -1 if target is not in nums. public static int search(final int target, final int[] nums) { // Linear search means start at one end and search element-by-element for(int i = 0; i &lt; nums.length; ++i) { if(nums[i] == target) { return i; } } return -1; }


Why do you use hashing and not array?

Hashing provides a method to search for data.Hashing provides a method to search for data.Hashing provides a method to search for data.Hashing provides a method to search for data.


What are the various applications of linear search in real time?

Linear search is necessary when we must search unordered sets. Linear search times across huge sets can be improved significantly by dividing the set amongst two or more threads that can execute on independent CPU cores.

Related questions

Are binary search and linear search already defined method in some class in java and what class is this?

You can check out the Arrays.binarySearch group of methods for searching sorted arrays. There is no predefined linear search for arrays, probably because it is trivially easy to implement. If you have some other data structure to search, the Collections.binarySearch methods should work for you. Most collections can also be converted to a List representation, which has a predefined indexOf method for linear searching.


Best first search program in c?

The best search programs to attempt writing in C are the following: Linear search (simplest), Binary search (faster) Hash search (fastest).


How do you run graphics program in C?

pro c language to implement linear search using pointers


Explain linear search with an example?

Sequential search of an object with in an array of objects is called as linear search.


What is linear searching?

The linear search algorithm is a special case of the brute force search.


What are advantage of linear searching?

There no advantages to linear search other than searching for the first (or last) nodes. Linear search takes linear time with an average O(n/2) for each search.


How do you find a solution for a linear equation?

Solution can be found by using three methods: 1. Cross Multiplication Method 2. Substitution Method 3. Elimination Method Other Method can also be there but I don't know You can further get info about these method by searching these on Google Search.


What is the running time of a linear search of an array?

Running time of a linear search is O(n)


WAP in java to search for a given number using linear search?

// This method will search through nums for target. // It will return the index of target in nums, or -1 if target is not in nums. public static int search(final int target, final int[] nums) { // Linear search means start at one end and search element-by-element for(int i = 0; i &lt; nums.length; ++i) { if(nums[i] == target) { return i; } } return -1; }


How can you contribute to a study abroad program?

In order to contribute to a study abroad program, you need to contact the sponsor of the program and find out what the is their preferred method of support. Just type the name of the program into your search box


Complexity of linear search?

the compexity of linear search in worst case is f(n) = n+1


Why do you use hashing and not array?

Hashing provides a method to search for data.Hashing provides a method to search for data.Hashing provides a method to search for data.Hashing provides a method to search for data.