The contains you are setting to true on line 11 is not the same as the one you create on line 7 and return on line 13.
As an aside, you should initialise contains with some value; right now, it could be returned as true even if the number is not in the array, because it will be just some random value.
This line won't work properly: int sizeofarray = sizeof(ArrayBeingChecked);
In this case, the size of an int is 4, and the array contains 9 ints, so sizeof(ArrayBeingChecked) returns 36.
You can test what I said before to see that it's true. To get the number of elements in an array just do this: int sizeofarray = sizeof(ArrayBeingChecked)/sizeof(int);
We divide by sizeof(int) instead of just 4 because the size of an integer may be different depending on your system.
Also, you should limit the scope of your variables as much as possible. You should declare i in the for loop like this: for(int i = 0; i < sizeofarray; ++i)
And your function should return a bool, not an int.
P.S. when I cout the sizeof int and sizeof ArrayBeingChecked from the arraycontains function, it gives me 40 for both of them... That's the real problem.
Actually, since ArrayBeingChecked becomes a pointer when passed to the function sizeof(ArrayBeingChecked) is 4 bytes (the sizeof a pointer). This explains why NumberBeingChecked is not found if it is above 4. Since sizeofarray = 4 the rest of the elements are never looked at. The loop only goes for i=0,1,2,3.
You need to pass the size of the array as another parameter to the function.
int arraycontains (int ArrayBeingChecked[], int NumElements, int NumberBeingChecked){
int i;
// int sizeofarray = sizeof(ArrayBeingChecked);// this won't work as desired here
bool contains = false;// initialize the value
for(i = 0; i < NumElements; ++i){
if (NumberBeingChecked == ArrayBeingChecked[i]){bool contains = true;}}
return contains;
}
int main(){
bool contains = false;
int array[9] = {1,2,3,4,5,6,7,8,9};
contains = arraycontains (array, 9, 1);
cout << contains;
}
EDIT: It was my intent to pass the number of elements as the 2nd parameter. Changing variable name to NumElements, and omitting array size in 1st parameter.
I think I know what the problem is, and I know one way to fix it.
ArrayBeingChecked is a pointer to the firest value of the array, and pointer's sizes are usually 4, so sizeof(pointer) == 4. sizeof(int) == 4 as well, so sizeofarray becomes one. Your loop only checks the first value in the array, so if the number is greater than one, it will not find it.
The only way I can think of to fix the problem is by passing sizeof(array) to your function.contains = arraycontains (array, sizeof(array), 9);
and then
1 2 3
bool arraycontains (int ArrayBeingChecked[], int size, int NumberBeingChecked){
int sizeofarray = size/sizeof(int);
...
It seems that certain information is being lost when the array is passed to the function. I'm sorry but I don't know why this happens.
Edit: fun2code beat me (by quite a bit), but my solution is a little better I think.
The loop doesn't end because you keep looping as long as the element is found, and you are saying:
1. Get a random number
2. Put that in the array
3. Search the array for the number you just put in there
4. Loop as long as the number is found
So the loop will never end because you are always looking for a number that is in the array.