Class vector
does not have an overloaded operator function to access its elements, so the content of the first loop is wrong:
for (i=0; i<8; i++)
{
tabla(i)= i+1; //inicializar vector
// ^^^
}
Instead you should use, since you have already created 8 elements in the vector when building it, it is the index operator []
for (i=0; i<8; i++)
{
tabla[i]= i+1; //inicializar vector
}
But of course, it does not make much sense to assign a predetermined number of positions and then do a fixed iteration. What happens if you then change the number of elments to 10? or you also update the limit in the loop or you will have errors. Here you should use the size()
method of the vector:
for (i=0; i<tabla.size(); i++)
{
tabla[i]= i+1; //inicializar vector
}
Even without this last change the program already compiles but ... what happens with this call?
int b=busqueda_recursiva(tabla,8,buscado, i);
If we review the program we will see that i
is 8 ... we see what effect that value has:
bool busqueda_recursiva(vector<int> array ,int length,int x, int i){
length = array.size();
if (length==0) {
cout << " el vector esta vacio " << endl;
return 0;
}
else if (x==array[i]){ // <<--- AQUI
return 1;
}
else
// ...
}
In the line highlighted with the comment you can see that in the first call to the function you already run the risk of accessing an invalid memory position. It is the risk of reusing variables.
In my humble opinion the main
should look more like this:
int main () { // (1)
//(2)
vector<int> tabla(8);
for (int i=0; i<8; i++) // (3)
{
tabla[i]= i+1; //inicializar vector
}
cout << "cual es el elemento que desea buscar" << endl;
cin >> buscado;
bool b=busqueda_recursiva(tabla,8,buscado, 0); // (4)
if (b){
cout << "el elemento " << buscado << "se encuentra en el vector"<< endl;
}
else {
cout << " el elemento " << buscado << " no se encuentra en el vector" << endl;
}
}
Where ...
In C ++, functions that do not receive arguments do not need void
are redundant and unnecessary.
In C ++ (and from C99 as well) it is not necessary to declare the variables at the beginning of the function. Ideally, delay your statement as long as possible.
Loops for
support variable declaration.
If the function returns bool
then it stores the value in a variable of type bool
. There is no point in converting the value to int
and it can be a source of errors.
The supposed recursive function also has several problems:
bool busqueda_recursiva(vector<int> array ,int length,int x, int i){ // (1)
length = array.size(); // (2)
if (length==0) {
cout << " el vector esta vacio " << endl; // (3)
return 0; // (4)
}
else if (x==array[i]){ //apunta al primer elemento (5) (6)
return 1; // (4)
}
else
{
busqueda_recursiva(array,--length,x,++i);
cout << "el elemento es " << x << endl; // (7)
return 1; // (4) (8)
}
}
Namely:
If you pass the vector by value you will make a copy of it in each recursive call. This is not very efficient either in time (although for that matter it is not a problem) or in memory (copying the vector a high number of times can fill the stack very quickly). It is preferable to pass a constant reference.
What good is it that the function receives an argument if you overwrite its value in the first line of the function?
Try not to use non-ASCII characters if you do not want to risk having strange messages
If the function returns a Boolean it is normal to use true
and false
instead of 1
and 0
. Remember that in C ++ (unlike C) the typing is strong and these things should be taken care of.
Do you not check the value of i
before accessing the array? What if the user enters a non-existent value? If you look at your function, it will only return false
when the vector is empty ... there are situations that you have not foreseen.
The comment of that line not only does not help but completely misleads. You are not accessing the first element but the one you touch in that iteration. To have bad comments is better not to have them.
x
is the value that the user enters. What does this message provide? nothing.
In case you have not set this message it will be repeated 1 time per recursive call. Is it necessary to get the same message 6 times in a row? I do not think so.
You should go over the code a bit.