Passing array as parameter problem

Hi..... i have a problem while trying to pass an array to a fuction by parameters heres my code.

#include <iostream>


using namespace std;

int binsearch(int data[], int numElements, int searchkey);

int binsearch(int data[], int numElements, int searchkey)
{
int resultado=0, mitad = 11,g=0,searchkey=0;

while(data[resultado]==searchkey)
{
g=mitad;
if(searchkey==data[mitad])
{
resultado=mitad;
}
else if(searchkey<data[mitad])
{
g=mitad;
mitad = mitad/2;
}
else if(searchkey>data[mitad])
{
g = numElements;
mitad = mitad + g;
}
}
return resultado;

}

int main()
{
int array1[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95},i=0,numElements=23,opcion2=0;
int opcion;
do
{
cout << "{";
for(i=0;i<23;i++)
{
if(i==22)
{
cout << array1[i];
break;
}
cout << array1[i] << ",";

}
cout << "}";
cout << endl << endl << "Enter search key (or 'x' to exit): ";
cin >> opcion;
/*if(opcion == 'x')
{
break;
}*/
/*opcion = static_cast<int>(opcion);
opcion2 = (int)opcion;*/
cout << endl<< opcion << " is in position " << binsearch (array1,numElements, opcion) << endl << endl;
}
while(opcion!='x');
cout << endl << endl << "Exiting..." << endl << endl;

}

the problem its simple i don`t know whats wrong with my array i spell it correctly and it does not apear in my binary function... can someone help me plz thx in advance
Hello,

There is an error in the function:
1
2
3
4
5
6
7
8
int binsearch(int data[], int numElements, int searchkey /* int searchkey */)
{
	int resultado=0, mitad = 11,g=0,searchkey=0; // ERROR: searchkey already exists
	
	// BUT
	// int resultado=0, mitad = 11, g = 0;  // OK
	// searchkey = 0;			// OK
}
Last edited on
1
2
3
4
5
6
7
8
9
10
int binsearch(int data[], int numElements, int searchkey) // interger variable called searchkey
{
    int resultado=0, mitad = 11,g=0,searchkey=0; //Error - already have a variable  in this function called searchkey - see above

    while(data[resultado]==searchkey)
    {
        g=mitad;
        if(searchkey==data[mitad])
        {
......

#include <iostream>


using namespace std;

int binsearch(int data[], int numElements, int searchkey);

int binsearch(int data[], int numElements, int searchkey)
{
int resultado=0, mitad = 11,g=0;

while(data[resultado]==searchkey)
{
g=mitad;
if(searchkey==data[mitad])
{
resultado=mitad;
}
else if(searchkey<data[mitad])
{
g=mitad;
mitad = mitad/2;
}
else if(searchkey>data[mitad])
{
g = numElements;
mitad = mitad + g;
}
}
return resultado;

}

int main()
{
int array1[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95},i=0,numElements=23,opcion2=0;
int opcion;
do
{
cout << "{";
for(i=0;i<23;i++)
{
if(i==22)
{
cout << array1[i];
break;
}
cout << array1[i] << ",";

}
cout << "}";
cout << endl << endl << "Enter search key (or 'x' to exit): ";
cin >> opcion;
/*if(opcion == 'x')
{
break;
}*/
/*opcion = static_cast<int>(opcion);
opcion2 = (int)opcion;*/
cout << endl<< opcion << " is in position " << binsearch (array1,numElements, opcion) << endl << endl;
}
while(opcion!='x');
cout << endl << endl << "Exiting..." << endl << endl;

}

that was a mistake that i did while copy and fixing at the same time but the array issue still persist the array when i test it in the other fuction apears empty with only a single variable thx for the help that you can bring to me
oh god!! i found it XD thx body i love you too... anyway the mistake was at the while. The array was correctly passed by the fuction but the while interfere because i had to put != rather than ==. And with some minor fixes in the function now everything was going well. thx for the help you lend me.

while(data[resultado]==searchkey) // its != not == XD
Topic archived. No new replies allowed.