开发者

no duplicate function for a lottery program

right now im trying to make a function that checks to see if the user’s selection 开发者_运维问答is already in the array , and if it does itll tell you to choose a diff number. how can i do this?


Do you mean something like this?

bool CheckNumberIsValid()
{
   for(int i = 0 ; i < array_length; ++i)
   {
      if(array[i] == user_selection)
         return false;
   }

   return true;
}

That should give you a clue, at least.


What's wrong with std::find? If you get the end iterator back, the value isn't in the array; otherwise, it is. Or if this is homework, and you're not allowed to use the standard library, a simple while loop should do the trick: this is a standard linear search, algorithms for which can be found anywhere. (On the other hand, some of the articles which pop up when searching with Google are pretty bad. You really should use the standard implementation:

Iterator
find( Iterator begin, Iterator end, ValueType target )
{
    while ( begin != end && *begin != target )
        ++ begin;
    return begin;
}

Simple, effective, and proven to work.)


[added post factum]Oh, homework tag. Ah well, it won't really benefit you that much then, still - I'll leave my answer since it can be of some use to others browsing through SO.

If you'd need to have lots of unique random numbers in a range - say 45000 random numbers from 0..45100 - then you should see how this is going to get problematic using the approach of:

while (size_of_range > v.size()) {
    int n = // get random
    if ( /* n  is not already in  v */ ) {
        v.push_back(n);
    } 
}

If the size of the pool and the range you want to get are close, and the pool size is not a very small integer - it'll get harder and harder to get a random number that wasn't already put in the vector/array. In that case, you'll be much better of using std::vector (in <vector>) and std::random_shuffle (in <algorithm>):

unsigned short start = 10; // the minimum value of a pool
unsigned short step = 1; // for 10,11,12,13,14... values in the vector
// initialize the pool of 45100 numbers
std::vector<unsigned long> pool(45100);
for (unsigned long i = 0, j = start; i < pool.size(); ++i, j += step) {
    pool[i] = j;
}
// get 45000 numbers from the pool without repetitions
std::random_shuffle(pool.begin(), pool.end());
return std::vector<unsigned long>(pool.begin(), pool.begin() + 45000);

You can obviously use any type, but you'll need to initialize the vector accordingly, so it'd contain all possible values you want.

Note that the memory overhead probably won't really matter if you really need almost all of the numbers in the pool, and you'll get good performance. Using rand() and checking will take a lot of time, and if your RAND_MAX is equal 32767 then it'd be an infinite loop. The memory overhead is however noticeable if you only need few of those values. The first approach would usually be faster then.


If it really needs to be the array you need to iterate or use find function from algorithm header. Well, I would suggest you go for putting the numbers in a set as the look up is fast in sets and handy using set::find function

ref: stl set


These are some of the steps (in pseudo-code since this is a homework question) on how you may get around to doing this:

  1. Get user to enter a new number.
  2. If the number entered is the first, push it to the vector anyway.
  3. Sort the contents of the vector in case size is > 1.
  4. Ask user to enter the number.
  5. Perform a binary search on the contents to see if the number was entered.
  6. If number is unique, push it into vector. If not unique, ask again.
  7. Go to step 3.

HTH,
Sriram.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜