Function does not return correct value.

Pages: 12
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
#include <iostream>
using namespace std;

int arraycontains (int ArrayBeingChecked[9], int NumberBeingChecked){
	int i;
	int sizeofarray = sizeof(ArrayBeingChecked);
	bool contains;


		for(i = 0; i < sizeofarray; ++i){
			if (NumberBeingChecked == ArrayBeingChecked[i]){bool contains = true;}}

		return contains;
}


int main(){

bool contains;
int array[9] = {1,2,3,4,5,6,7,8,9};

contains = arraycontains (array, 1);

cout << contains;

}

This should be returning 1, or true, but it returns 0. Don't yell at me if this code looks ridiculous, I'm a huge newbie. Also, any tips? :P
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.
Line 11 you declare a new variable named contains which is shadowing the one you declared on line 7.
Thanks, but now if the number being checked is anything above 4, it will return false... Strange...
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.
Last edited on
I thought sizeof then an array would equal the amount of elements in it? If not, then how would I get that number?
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.
Last edited on
Okay, I changed a few things based on your suggestions, but now it returns anything above 1 as 0. :(


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
#include <iostream>
using namespace std;

bool arraycontains (int ArrayBeingChecked[], int NumberBeingChecked){
	int sizeofarray = (sizeof(ArrayBeingChecked))/sizeof(int);
	bool contains = false;


		for(int i = 0; i < sizeofarray; ++i){
			if (NumberBeingChecked == ArrayBeingChecked[i]){contains = true;}}

		return contains;
}


int main(){

bool contains;
int array[] = {1,2,3,4,5,6,7,8,9};

contains = arraycontains (array, 9);

cout << contains;

}
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.
Last edited on
closed account (D80DSL3A)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
Last edited on
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.
Last edited on
Why would you pass the size of the array in bytes when you can just pass the number of elements in the array?
Okay, it must be getting stuck in the loop, because it never couts the value of RowOne[1] I know I must be doing something stupid, but what?


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
#include <iostream>
using namespace std;

bool arraycontains (int ArrayBeingChecked[], int NumberBeingChecked, int SizeOfArray){
	bool contains = false;


		for(int i = 0; i < SizeOfArray; ++i){
			if (NumberBeingChecked == ArrayBeingChecked[i])
			{contains = true;}
			else {contains = false;}}


		return contains;
}


int main(){

int RowOne[9] = {0,0,0,0,0,0,0,0,0};
bool contains = true;

    RowOne[0] = rand()%9-1;


    do {RowOne[1] = rand()%9-1; contains = arraycontains(RowOne, RowOne[1], 9);}
    while (contains = true);

    cout << RowOne[1];



    }
Last edited on
RowOne will never contain 9 because rand()%9-1 may only produce -1 through 8.
Why would you pass the size of the array in bytes when you can just pass the number of elements in the array?

What if the number of elements is not known at compilation?
Uh, you sure about that? How would I do 1-9 then? rand() 9+1? Thanks, but that still doesn't solve the major problem, that the loop never ends.
Last edited on
while (contains = true);
It should be (contains == true);
You also should seed the random number generator with srand().
Here's some reading on random numbers:
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

rand() 9+1

This is correct.
Fail, how could I forget ==? Also, I know that normal rand isn't really random, but that's not important right now.
Wait, no, I was right. I was setting contains to a value, not comparing it. Still doesn't work. :(
Wait, no, you were right, I was looking in the wrong function, even more fail.
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.
Pages: 12