Binary Search functin problem

I've written the following binary search program using template.
But when I run it, it gives an error: 55|error: no matching function for call to ‘Binary_Search(std::vector<int>&, int&, int&)’|

Where do I need to correct it?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<vector>

using namespace std;

template<class T>
int Binary_Search(T& Array, T& Size, T& Value)
{
    int Start=0;
    int End=Size;
    int Mid;

    while(Start<=End)
    {
        Mid=(Start+End)/2;

        if(Array[Mid]==Value)
            return Mid;
        else if(Array[Mid]<Value)
            Start=Mid+1;
        else
            End=Mid-1;

        return -1;
    }
}


int main()
{
    vector<int>Array;

    cout<<"Enter number of elements: ";
    int n;
    cin>>n;

    cout<<"Enter the elements: ";
    for(int i=0; i<n; i++)
    {
        int element;
        cin>>element;
        Array.push_back(element);
    }

    cout<<"Enter the value you want to search: ";
    int Value;
    cin>>Value;

    int Size=Array.size();

    if(Binary_Search(Array,Size,Value)==-1)
        cout<<"Element not found!"<<endl;
    else
        cout<<"Element found at position "<<Binary_Search(Array,Size,Value)<<endl;

    return 0;
}
Last edited on
int Binary_Search(T& Array, T& Size, T& Value)

Thats not quite how you take a vector as parameter. Change it to

int Binary_Search(vector<T>& Array, T& Size, T& Value)

Edit: Also, name your vector something else. Array aint cool.
Last edited on
Topic archived. No new replies allowed.