Problem Statement : To Perform Linear Search on Array of Integers. If true, return 1, otherwise -1.
Language Used : C++
Implementation Method : Iteration
Input : arr[]={5,8,4,6,2,4,5,6,9};
Target Element : 14
Output : -1
Code:
#include<iostream>using namespace std;int linearsearchInteger(int arr[],int key, int size){if(size==0){return 0;}for(int i=0;i<size;i++){if(arr[i]==key){return i;}}return -1;}int main(){int arr[]={5,8,4,6,2,4,5,6,9};int size = sizeof(arr)/sizeof(arr[0]);int key = 14;cout<< linearsearchInteger (arr, key, size);return 0;}
Check out more posts like this. If any doubt, comment below.
No comments:
Post a Comment