What Linear search is what do you think is the importance of a search searches in C++ are of two types one is a linear search and the other is a binary search we learn about linear search. Linear search for a simple example lets us consider an integer array of 5 with 5 elements in it and let the elements be 3 5 9 8 and 16. Now, what is a linear search just like a simple search a Linear search is the one where we start with this beginning element and we check for the required element? Entered by the user and keep incrementing the array index.C++ projects for beginners with source code
Value let us see how it’s done suppose the user has mentioned the array elements as 3 5 9 8 and 16. Now he’s asking us where is element 8 stored in the array. We can find this out by using the linear search as the value entered by the user is 8.
The computer must check from the starting element of the array that is an of 0 that is 3 in 3 equal to 8. Now it goes to the next element is 5 equal to 8 no is 9 no, Finally, it reaches an of three is eight equal to eight yes bingoit must return us with the index with the position of the number entered by the user.
The user wanted the number eight & eight is present in the fourth location of the array that is an of three not to forget there starts with an of zero. That the computer has found out where the position of the number wanted by the user is it must return as the value. Now let us write a simple logic just to get the value and it’s just the core logic it’s not the entire program and gun right it’s going to be something like this if X equal to equal to eight 8 is the value entered by the user.
While okay if you are scanning the value entered by the user say you have scanned. The value entered by the user into a variable called Y you can use supposed to write if x equal to equal to Y fine. if X equal to equal to Y we print F percentile D what’s the position of is the fourth position. It’s going to be I plus one if you have declared the array a of I now to get a clear concept of this. We need a for loop and this is the Core Logic pathway that now returns us the value where the entrance is the position where our required element.Top 5 Programming Languages
Code:
#include <iostream> #include <conio.h>
using namespace std;
void main() { int arr[100]; int key, ar_value, i; int found =-1;
{ if (key == arr[i]) { found = i; break; } } if (found == -1) { cout<<“Value is Not found.”<<endl; } else { cout<<“\nValue found at “<<found<<” index.”; } getch(); }
#include
#include
using namespace std;
void main()
{
int arr[100];
int key, ar_value, i;
int found =-1;
court<<"Enter Array Value. ";
cin>>ar_value;
court<<"\nEnter Any "<>arr[i];
}
cout<<"\nEnter Find Value. ";
cin>>key;
for(i = 0; i < ar_value; i++)
{
if (key == arr[i])
{
found = i;
break;
}
}
if (found == -1)
{
cout<<"Value is Not found."<
Pingback: Binary Search Algorithm in C++ with Source Code
Pingback: Write an Assembly Language Program to Display a String