Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to linear search

+2 votes
asked Feb 21 by Reflection 1909 (140 points)

1 Answer

0 votes
answered Feb 21 by Yash Allabadi (140 points)

hey i hope this will help you

#include <bits/stdc++.h>

using namespace std;

int search(int arr[], int n, int x)


int i;

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

if (arr[i] == x)

return i;

return -1;


int main()


int arr[] = { 3, 4, 1, 7, 5 };

int n = sizeof(arr) / sizeof(arr[0]);

int x = 4;

int index = search(arr, n, x);

if (index == -1)

cout << "Element is not present in the array";


cout << "Element found at position " << index;

return 0;


Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.