Function does not return correct value.

Pages: 12
Zhuge is correct.

It would help us if we knew what your code is supposed to do in the long run.
*Sigh* more problems... This generates two of the same numbers (two 9's)


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;

bool arraycontains (int ArrayBeingChecked[], int NumberBeingChecked, int SizeOfArray){ //Will return true if the array contains the number, false if it doesnt't.



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


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
    );
    //seperate-------------------------------
    do {
    RowOne[2] = rand()%9+1;
    contains = arraycontains(RowOne, RowOne[2], 9);
    }
    while (
    contains == true
    );
    //seperate-------------------------------
        do {
    RowOne[3] = rand()%9+1;
    contains = arraycontains(RowOne, RowOne[3], 9);
    }
    while (
    contains == true
    );
    //seperate------------------------------
        do {
    RowOne[4] = rand()%9+1;
    contains = arraycontains(RowOne, RowOne[4], 9);
    }
    while (
    contains == true
    );


cout << RowOne[0];
cout << RowOne[1];
cout << RowOne[2];
cout << RowOne[3];
cout << RowOne[4];
            }
This is just the beginning of a very long code, but this portion is supposed to produce 9 random numbers, all being different. In the longer run it will have to produce a Sudoku board. I suppose I should be using a 2D array, but I haven't really learned those yet... Remember, I'm a newbie.
@Zhuge No, it's saying to generate a random number for an element, check if the element is already in the array afterward, and if it is, keep looping. So, if it gets to a number not in the array, it will continue. At least that's what I'm going for.
I see; well the problem is you place the value in the array before searching, so you are guaranteed to find it in the array. Place the random number into a temporary variable, perform the search, then place the value into the array.
Do you know how to use vectors or random_shuffle()? These would help a lot.
@Zhuge Ah, I see. But now I'm confused, why would it then continue then? (The never-ending loop doesn't happen with that code) The array-searching function is really borked isn't it?

@Browni Sadly, I do not, I have a feeling that random shuffle would simplify this a lot...
Can you link me to a good explanation of vectors and random_shuffle?
*Raises eyebrow* It's getting out of the first loop? Just looking over it, that seems to be impossible.

Random shuffle:
1
2
3
4
5
  vector<int> myvector;
  // set some values:
  for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
  random_shuffle ( myvector.begin(), myvector.end() );
  // myvector is now randomly ordered 
I know, strange right? Try running it for yourself...

I think that random shuffle does what I want in 5 lines, rather than my roughly 100 lines...
Although, it's the experience that counts, so I'll keep doing it this way. But I just changed it to:


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

bool arraycontains (int ArrayBeingChecked[], int NumberBeingChecked, int SizeOfArray){ //Will return true if the array contains the number, false if it doesnt't.



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


int main(){

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

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


    do {
    TemporaryValue = rand()%9+1;
    contains = arraycontains(RowOne, TemporaryValue, 9);
    }
    while (
    contains == true
    );
    RowOne[1] = TemporaryValue;
    //seperate-------------------------------
    do {
    TemporaryValue = rand()%9+1;
    contains = arraycontains(RowOne, TemporaryValue, 9);
    }
    while (
    contains == true
    );
    RowOne[2] = TemporaryValue;
    //seperate-------------------------------
        do {
    TemporaryValue = rand()%9+1;
    contains = arraycontains(RowOne, TemporaryValue, 9);
    }
    while (
    contains == true
    );
    RowOne[3] = TemporaryValue;
    //seperate------------------------------
        do {
    TemporaryValue = rand()%9+1;
    contains = arraycontains(RowOne, TemporaryValue, 9);
    }
    while (
    contains == true
    );
    RowOne[4] = TemporaryValue;


cout << RowOne[0];
cout << RowOne[1];
cout << RowOne[2];
cout << RowOne[3];
cout << RowOne[4];
            }
I see it. Your array search function is wrong; it checks the first element of the array, and if it matches what you are looking for, it returns true. If it doesn't, it returns false - without checking the rest of the array. Remove the else and put the return false; line outside of the loop.
AHA found the problem, with the checker function I wrote, it would always just return the value that happened when it compared the number and the LAST element, because there was nothing to stop it from going on after it found a valid number, to fix it I just got rid of "else {return false;}" :)
Lol, guess you posted that as I was posting mine :P It seems to post valid numbers now. Yay.
Last edited on
Yeah. But it isn't the last element. It's the first, the one located at RowOne[0].
I don't think so, it keeps looping till it hits the last element, which then returns true or false.
That's not right. Analyzing it step by step:

(this is the non-working method, by the way)
int i is set to 0
check to see if i is less than Size
it is, so we enter the loop
compare array[i] (in this case, array[0]) is equal to what we are searching for
- If it is, we return true; the function ends
- If it isn't, we go to the else clause, which does return false; the function ends

We never get past the first element of the array.

Naturally, you fixed this when you removed the else statement; now if it isn't equal, we move on the the next element, and if we get through all the elements without finding a match, we leave the loop and return false.
Ah, I kind of forgot that once you return, you leave the loop.
Topic archived. No new replies allowed.
Pages: 12