Couple of tips:
1) Give variable names more meaning. "a" isn't a good variable name.
2) Every time you do something that computer could do(and is tiresome), try to think if you can improve anything. For example your "black" and "red" array are not only a bit arbitrary(some comments could help), they're created by hand. You could implement a function like:
1 2 3 4 5 6 7 8 9
|
//Vector is essentially a smarter array. & means that you pass a reference
//Reference is something like a pointer, but simpler. You can think of this
//operation as "pass the destination, instead of grabbing a copy to it
void AddEvenRange(int begin, int end, std::vector<int>& destination)
{
if(begin % 2 == 1) ++begin;
for(; begin <= end; begin += 2)
destination.push_back(begin);
}
|
And so you could use this, along with other utility AddOddRange, to generate black and red(a bit like this):
1 2 3 4 5
|
std::vector<int> black;
AddEvenRange(2,10,black);
AddOddRange(11,17,black);
AddEvenRange(20,28,black);
AddOddRange(29,35,black);
|
Or better yet, put this in another utility, so the code is clearer:
1 2 3 4 5 6 7
|
void InitializeBlack(std::vector<int>& black)
{
AddEvenRange(2,10,black);
AddOddRange(11,17,black);
AddEvenRange(20,28,black);
AddOddRange(29,35,black);
}
|
This project might be too small for this, but this would make the code easier to read anyway.
Next, 0+ X = X, so you don't need to do that at line 9. Also, since it's C++, use std::mt19937 from <random> instead of rand() (homework: google "rand considered harmful).
As for error, it's simple: array is a pointer too. You're comparing int*(or int[]) with int. This won't do. Instead, you should check whether result is in the array. Using vector, you could do:
1 2 3
|
std::vector<int> black;
/*...*/
if(std::find(black.begin(), black.end(), result) != black.end())//...
|
Generally, when writing the code, try to think about the higher level. You're not "checking if color is in the array", you're "checking if color is black". The code is often easier to read if it's composed of small utilities that make it up. The intellectual effort to understand the program as a whole might be a little higher, but at the same time the program is easier to read, since it encapsulates abstractions and uses natural language to describe the problem, leaving implementation details out of the way.
Cheers!
PS. Oh, and please run the code in your head before running it on the PC. You are using a variable called color before creating it. That's an error.
PPS. I also noticed that the arrays(vectors) are storted. The find(if I remember things correctly) uses linear search algorithm, which works in O(n) time. Since you are sure that your arrays are sorted, you can use binary search algorithm(
http://www.cplusplus.com/reference/algorithm/binary_search/ ), which is much faster(it's O(log2(n))). It might not be much for this short sequence, but it's good to remember anyway.