Duplication

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2003
Posts: 1
Reputation: I0adidas0I is an unknown quantity at this point 
Solved Threads: 0
I0adidas0I I0adidas0I is offline Offline
Newbie Poster

Duplication

 
0
  #1
Mar 5th, 2003
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 37
Reputation: Xelfinn is an unknown quantity at this point 
Solved Threads: 0
Xelfinn Xelfinn is offline Offline
Light Poster
 
0
  #2
Mar 6th, 2003
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,043
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
 
0
  #3
Mar 6th, 2003
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?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member
 
0
  #4
Mar 7th, 2003
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):

  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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 37
Reputation: Xelfinn is an unknown quantity at this point 
Solved Threads: 0
Xelfinn Xelfinn is offline Offline
Light Poster
 
0
  #5
Mar 7th, 2003
I like that...much faster than my idea and with no guaranteed work to prove the string hasn't been used.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member
 
0
  #6
Mar 8th, 2003
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC