Linear search

QUESTION

Write a program for linear search using function.

“TESTCASE_1”: “4 \n2 3 1 4\n1\n###—###SEPERATOR—###—\nElement=1\nPosition=2”, “TESTCASE_2”: “4\n6 8 3 1\n8\n###—###SEPERATOR—###—\nElement=8\nPosition=1”, “TESTCASE_3”: “7\n3 1 2 4 3 5 6\n6\n###—###SEPERATOR—###—\nElement=6\nPosition=6”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{
	int n,i,x,pos=0;
  	cin>>n;
  	int a[n];
  	for(i=0;i<n;i++){
    	cin>>a[i];
    }
  	cin>>x;
  	for(i=0;i<n;i++){
    	if(a[i]==x){
        	pos=i;
        }
    }
  	cout<<"Element="<<x<<endl;
  	cout<<"Position="<<pos;
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock