| | |
Duplication
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2003
Posts: 1
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Aug 2003
Posts: 37
Reputation:
Solved Threads: 0
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.

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.
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?
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
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):
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:
- 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)
string getQuestion(vector<string>& v, int& numberOfStrings) { // handle errors if(numberOfStrings <= 0) return "error"; if(numberOfStrings > v.size()) return "error"; // select a random number in the range 0 - numberOfStrings-1 int next = rand() % numberOfStrings; // decrement the number of strings numberOfStrings--; // swap it with the last unused string swap(v[next], v[numberOfStrings]); // retrieve the string return v[numberOfStrings]; }
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)
#include <iostream> #include <vector> #include <string> #include <cstdlib> #include <algorithm> using namespace std; string getQuestion(vector<string>& v, int& numberOfStrings) { if(numberOfStrings <= 0) return "Error"; if(numberOfStrings > v.size()) return "Error"; int next = rand() % numberOfStrings; numberOfStrings--; swap(v[next], v[numberOfStrings]); return v[numberOfStrings]; } int main() { vector<string> q; q.push_back("One"); q.push_back("Two"); q.push_back("Three"); q.push_back("Four"); q.push_back("Five"); q.push_back("Six"); q.push_back("Seven"); q.push_back("Eight"); q.push_back("Nine"); q.push_back("Ten"); int num=q.size(); cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; cout << getQuestion(q,num) << endl; }
![]() |
Similar Threads
- Generate Random numbers without duplication other than storing in the database (Visual Basic 4 / 5 / 6)
- No Duplication in the question (C++)
- web site (Website Reviews)
- Send data on a serial port (C++)
- PLEASE HELP, Duplication in Arrays (Java)
- check for images duplication (ASP.NET)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






