-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_search.cpp
33 lines (25 loc) · 1.03 KB
/
linear_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//************************************************************************************
// File: linear_search.cpp
// Author: Amauri Tuoni
//
// Program to do a linear search of data.
//
// Created on 26 de Setembro de 2020, 17:40
//************************************************************************************
#include<iostream>
#include<cstdlib>
using namespace std;
int linear_search(int *data, int size, int item)
{
int element; // index of the data array
int answer_search = 0; // initial set as 0 (not found)
for(element=0; element < size; element++) //run loop until the end of data array
{
if(data[element]==item) //check if the search item is equal to data element
{
answer_search = element+1; // save the data that is equal
cout <<" The search item is at position: "<< answer_search << endl; //print on the screen the position of the data element found
}
}
return answer_search; // return the result ( 0 not found)
}