•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,945 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,887 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3420 | Replies: 5
![]() |
•
•
Join Date: Mar 2003
Posts: 1
Reputation:
Rep Power: 0
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:
Rep Power: 6
Solved Threads: 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.
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation:
Rep Power: 32
Solved Threads: 107
#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
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
#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):
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:
#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;
} #6
Mar 8th, 2003
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Send data on a serial port (C++)
- PLEASE HELP, Duplication in Arrays (Java)
- duplication of picture when trying to copy paste (Windows NT / 2000 / XP / 2003)
- check for images duplication (ASP.NET)
- web site (Website Reviews)
Other Threads in the C++ Forum



Linear Mode