array help

[code]
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

bool BinarySearch(int a[256], int t)
{
int size = sizeof(a)/sizeof(int);
cout << size;

int low = 0;
int high = size - 1;
while (low <= high)
{
int i = (low + high)/2;
if (t == a[i])
{
return true;
}
else if(t < a[i])
{
high = i- 1;
}
else
{
low = i + 1;
}
return false;
}
}

int main()
{
int array[256];
for(int i=0;i<256;i++) {
cout <<"Input for position number " << i+1
<<"\n";
cin >>array[i];
cout <<"Add another integer?(Y/N) ";
char repeatans;
cin >>repeatans;
if (repeatans == 'N'){
break;
}
}
cout << "Number to search for: " << endl;
int target;
cin >> target;
bool search_result;
search_result = BinarySearch(array[256], target);
if(search_result =! 0)
{
cout << "Integer was found";
}
else
{
cout << "Integer was not found";
}
}
/code]
error on line 52
1. Use code tags:

[code]
1
2
//Code here.
//Line numbers are automatic. 
[/code]

2. Show the error messages that you get. In general be explicit about what you want or need or what you are facing. For example, I don't bother in even looking at code that is not within code tags, and much less if I don't have a description of what I'm looking for. Others may work similarly.
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
58
59
60
61
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

 bool BinarySearch(int a[256], int t)//* binary search algorithm 
{
    int size = sizeof(a)/sizeof(int);
    cout << size;

    int low = 0;
    int high = size - 1;
    while (low <= high)
    {
        int i = (low + high)/2;
        if (t == a[i])
        {
            return true;
        }
        else if(t < a[i])
        {
            high = i- 1;
        }
        else
        {
            low = i + 1;
        }
        return false;
    }
}

int main()//* Add integers to an array
{
    int array[256];
    for(int i=0;i<256;i++) {
        cout <<"Input for position number " << i+1
             <<"\n";
        cin >>array[i];
        cout <<"Add another integer?(Y/N) ";
        char repeatans;
        cin >>repeatans;
            if (repeatans == 'N'){
                break;
            }
    }
    cout << "Number to search for: " << endl;//* Search for integer in array
    int target;
    cin >> target;
    bool search_result;
    search_result = BinarySearch(array[256], target);//* error: invalid conversion from ' int ' to ' int* '
        if(search_result =! 0)
    {
        cout << "Integer was found";
    }
    else
    {
        cout << "Integer was not found";
    }
}

gives me following errors:
|52|error: invalid conversion from 'int' to 'int*'|
|52|error: initializing argument 1 of 'bool BinarySearch(int*, int)'|
|53|warning: suggest parentheses around assignment used as truth value|
||=== Build finished: 2 errors, 1 warnings ===|


Read the error message. It says it can't convert from int to an int pointer on line 52. This probably means you are using the wrong type.
Well I obviously dont know what Im doing, could you go in depth perhaps?
search_result = BinarySearch(array[256], target)

You're trying to pass array[256] into the function BinarySearch. That's wrong. Here, look:

array[0] is the first element of the array. It's an int. Right? So, array[256] is another int, which is actually off the end of your array. Not only are you trying to pass an int to a function that expects an int*, you're passing an int that doesn't even exist.

Change this
bool BinarySearch(int a[256], int t)//* binary search algorithm
to
bool BinarySearch(int a[], int t)//* binary search algorithm


And pass to the function like this:
search_result = BinarySearch(array, target);//* error: invalid conversion from ' int ' to ' int* '
Not like this:
search_result = BinarySearch(array[256], target);//* error: invalid conversion from ' int ' to ' int* '


And I think your warning is that there is no return statement if the while loop isn't true:
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
 bool BinarySearch(int a[256], int t)//* binary search algorithm 
{
    int size = sizeof(a)/sizeof(int);
    cout << size;

    int low = 0;
    int high = size - 1;
    while (low <= high)
    {
        int i = (low + high)/2;
        if (t == a[i])
        {
            return true;
        }
        else if(t < a[i])
        {
            high = i- 1;
        }
        else
        {
            low = i + 1;
        }
        return false;
    }

    //Add a return here too!
    return false;
}


And what Moschops is saying is, when you create an array, you create it like array[ n - 1 ].

You have array[ 256 ] which has a range of 0 - 255. array[ 256 ] could be anything in your memory!
Last edited on
See? So many answers when you post a question correctly and with the right information.
Topic archived. No new replies allowed.