I am currently working on pset3/search and when I try to compile the codes it keeps telling me that ^ error. I know that it is because I must not be returning a boolean value as needed, but I do not understand where my error is. Here is my code:
bool search(int value, int values[], int n)
{
if(n<1)
{
return false;
}else{
for(int i=0; i<n;i++)
{
if(value == values[i])
{
return true;
break;
}else{
return false;
}
}
}
}
Selon moi, what I am doing is: 1. Deny 0 or negative numbers in the size 2. Comparing the value you might me lookgin for whith the ith value of the array. 2.1 If they are the same the function returns true and finishes the loop. 2.2 If not, the function returns false and continues the loop until the end of the array. After that everything ends, so in both cases you have a boolean value in return. Where is the mistake?
for
loop is never executed? you're assuming thatn
is gonna be always > 0, but the compiler doesn't assume that. it's the same problem with the same solution.