You are on page 1of 2

Umesh Singh Verma (LCS2022052)

Design Analysis of Algorithm – Lab 02 Topic – Implementaion of Linear Search

Code:
#include <bits/stdc++.h>
using namespace std;

int LinearSearch(int arr[], int N, int x)


{
for (int i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}

int main()
{
int n;
cout << "Enter no of elements in the array ";
cin >> n;
int arr[n];
cout << "Enter array elements :";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int x;
cout << "Enter a no: ";
cin >> x;
int e = sizeof(arr) / sizeof(arr[0]);
int result = LinearSearch(arr, e, x);
if (result == -1)
{
cout << "Element is not present in array" << endl;
}
else
{
cout << "Element is present at index " << result << endl;
}
return 0;
}
Screen shot of the output:

You might also like