943,491 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4792
  • C++ RSS
Mar 5th, 2003
0

Duplication

Expand Post »
OK I need some help... so far ive been able to get everything on my own but...im making a program that selects questions from an array numbered 1-40. i put in a random fuctions so the questions are chosen randomly. but i want to have it so that theres no duplication in the questions. like if the number is already chosen... skip and pick the next. any help would be much appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I0adidas0I is offline Offline
1 posts
since Mar 2003
Mar 6th, 2003
0
Re: Duplication
It seems easy enough to set up another array to store a reference of string pointers to the questions chosen. And each time a random question is selected search the other array for a matching pointer. Make sure you set each of the other array cells to NULL so you don't compare some unknown something.

Of course I don't know C++ but that's how I'd do it in C. I'd probably set the array only to 10 pointers and run it like a queue once it's filled up. A repeat after 10 questions wouldn't be bad.
Reputation Points: 14
Solved Threads: 1
Light Poster
Xelfinn is offline Offline
37 posts
since Aug 2003
Mar 6th, 2003
0
Re: Duplication
Here's an extremely simplistic/mediocre way of doing it in C++, based on Aelfinn's posted.

Lets say you have array of strings A[40] with 40 cells, that looks sorta like the following?

A[0] = Question 1?
A[1] = Question 2?
A[2] = Question 3?

and, well, you get the idea. Now, basically create a second array, int B[40]. Also create a variable int so_far which will be an accumulator each time a question is asked.

So, lets say a random number 5 is chosen. First, look up A[5] and ask the question. Then, set B[0] equal to 5, and increment so_far by 1. Now, lets say a random number 8 is chosen. Look up A[8], ask the question, and set B[1] equal to 8, and increment so_far by 2. Each time a question is asked, do a linear search on B[0] through B[so_far - 1] to see if any of the values match up to 8. Make sense?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Mar 7th, 2003
0
Re: Duplication
One easy way to achieve this would be as follows:

- Start with your array of 40 questions (strings).
- Initialise a counter to the number of strings (40)
- Select a random string in the range 1 to 40 (e.g. string 10)
- Swap the selected string (10) with the last unused string (40)
- Output string 40
- decrement the number of strings (to 39)

The string you used is now outside the range (as string 40)

- Select a random string in the range 1 to 39 (e.g. string 26)
- Swap the selected string (26) with the last unused string (39)
- Output string 39
- decrement the number of string to 38

The used strings are at positions 39 and 40, outside of the range

- repeat as required, handle the condition whereby zero strings are left


It sounds more complicated than it really is, here's a function that achieves it (using a vector of strings):

C++ Syntax (Toggle Plain Text)
  1. string getQuestion(vector<string>& v, int& numberOfStrings)
  2. {
  3. // handle errors
  4. if(numberOfStrings <= 0) return "error";
  5. if(numberOfStrings > v.size()) return "error";
  6.  
  7. // select a random number in the range 0 - numberOfStrings-1
  8. int next = rand() % numberOfStrings;
  9.  
  10. // decrement the number of strings
  11. numberOfStrings--;
  12.  
  13. // swap it with the last unused string
  14. swap(v[next], v[numberOfStrings]);
  15.  
  16. // retrieve the string
  17. return v[numberOfStrings];
  18. }

You populate your vector with strings, then set an integer variable equal to the number of strings, and off you go - make calls to the getQuestion() function.

Here's a demo program:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. string getQuestion(vector<string>& v, int& numberOfStrings)
  10. {
  11. if(numberOfStrings <= 0) return "Error";
  12. if(numberOfStrings > v.size()) return "Error";
  13.  
  14. int next = rand() % numberOfStrings;
  15. numberOfStrings--;
  16. swap(v[next], v[numberOfStrings]);
  17. return v[numberOfStrings];
  18. }
  19.  
  20. int main()
  21. {
  22. vector<string> q;
  23.  
  24. q.push_back("One");
  25. q.push_back("Two");
  26. q.push_back("Three");
  27. q.push_back("Four");
  28. q.push_back("Five");
  29. q.push_back("Six");
  30. q.push_back("Seven");
  31. q.push_back("Eight");
  32. q.push_back("Nine");
  33. q.push_back("Ten");
  34.  
  35. int num=q.size();
  36.  
  37. cout << getQuestion(q,num) << endl;
  38. cout << getQuestion(q,num) << endl;
  39. cout << getQuestion(q,num) << endl;
  40. cout << getQuestion(q,num) << endl;
  41. cout << getQuestion(q,num) << endl;
  42. cout << getQuestion(q,num) << endl;
  43. cout << getQuestion(q,num) << endl;
  44. cout << getQuestion(q,num) << endl;
  45. cout << getQuestion(q,num) << endl;
  46. cout << getQuestion(q,num) << endl;
  47. cout << getQuestion(q,num) << endl;
  48. cout << getQuestion(q,num) << endl;
  49. }
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Mar 7th, 2003
0
Re: Duplication
I like that...much faster than my idea and with no guaranteed work to prove the string hasn't been used.
Reputation Points: 14
Solved Threads: 1
Light Poster
Xelfinn is offline Offline
37 posts
since Aug 2003
Mar 8th, 2003
0
Re: Duplication
Note that you'd want to add a call to srand() at the start of main() in your program. It suited me not to do this during testing so that it always used the same sequence.
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Feb 7th, 2010
-3
Re: Duplication
ok...
if i were on your place then i will try to do it some thing like that

i will make another array of same size of or of the size greater than that of your array but as it was wastage of memory so make array of size as u need and after that i will place random number in that array and if that number comes by using some search technique i will check whether this number comes before if it was... then i will again generate random number and so on
hope u will understand....
Reputation Points: 4
Solved Threads: 1
Newbie Poster
#define is offline Offline
6 posts
since Jan 2010
Feb 8th, 2010
0
Re: Duplication
2 approaches to consider:

1st assuming that you have a list of questions that are unique
in something like a std::vector<std::string> questions

there are two approaches I would recommend #2

#1
Shuffle the questions so that they are in a random order
You can use a method suggest by the other posters

have an iterator (something to point to the contents)
point to the [0] string of the shuffled questions
std::string<std::vector>::iterator it(questions.begin());

on get_question you can get current_string
*it (to get the value the iterator is pointing to) in this case a string
++it; //move onto next

need to check (it != questions.end())
as this would mean you have run out of questions

#2
leave the questions fixed in order

have an index of ints

C++ Syntax (Toggle Plain Text)
  1. int number_of_questions = questions.size();
  2. std::vector<int> index;
  3. for(int i = 0; i < number_of_questions; ++i)
  4. {
  5. index.push_back(i);
  6. }

Now you can shuffle the index to give a shuffled index
then when you want a question get the number

C++ Syntax (Toggle Plain Text)
  1. //when a new asker arrives
  2. index = shuffle(index);
  3. std::vector<int> shuffled_index = index;
  4.  
  5. int ind = 0;
C++ Syntax (Toggle Plain Text)
  1. //now each time when you get a question
  2. //check ind ok
  3. if(ind >=0 && ind < shuffled_index.size())
  4. {
  5. int cur_ind = shuffled_index[ind];
  6. //use current ind to get the question
  7. if(cur_ind >= 0 && cur_ind < questions.size())
  8. {
  9. std::string question = questions[cur_ind];
  10. }
  11. //get the next random id
  12. ++ind
  13. }
  14. else
  15. {
  16. //asked all the questions
  17. }

you only need to randomise when you shuffle
Last edited by tetron; Feb 8th, 2010 at 9:32 am. Reason: left out a line of code
Reputation Points: 32
Solved Threads: 37
Junior Poster
tetron is offline Offline
199 posts
since Feb 2010
Feb 8th, 2010
1
Re: Duplication
#define & tetro congratz ladies/gents!

You just made new forum record of re-opening oldest thread which now stands for 7 years.

Thread closed.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Chaining Overloaded Operators
Next Thread in C++ Forum Timeline: Using C library with anonymous struct in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC