The document discusses linear search in arrays. It provides code for a linear search function that takes an array, its size, and an element to search for as parameters. The function searches the entire array sequentially and returns the index of the element if found, or -1 if not found. The main function gets array input from the user, calls the linear search function to search for a given element, and prints the result.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views
Computer Practical File
The document discusses linear search in arrays. It provides code for a linear search function that takes an array, its size, and an element to search for as parameters. The function searches the entire array sequentially and returns the index of the element if found, or -1 if not found. The main function gets array input from the user, calls the linear search function to search for a given element, and prints the result.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Computer practical file
Arrays 1)linear search array : code:
#include "iostream"
using namespace std;
int main() { int N; int arr[50]; cout<<"enter maximum array size"<<endl; cin>>N; cout<<"enter the elements of the array"<<endl; for (int i=0; i<N; i++) { cin>>arr[i]; } int ele; cout<<"enter the element that has to be searched "<<endl; cin>> ele;
int lsearch(int arr[],int N,int ele);
int index; index=lsearch(arr, N, ele); if (index==-1) { cout<<"the item could not be found"<<endl; } else cout<<"the element "<<ele<<" is at an index of "<<index; return 0; } int lsearch(int arr[],int N,int ele) { int number; for (int i=0;i<N; i++) { if (arr[i]==ele) { number=i; return number; } else continue; } return -1; } output: