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.

Recommended Answers

All 8 Replies

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.

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?

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):

string getQuestion&#40;vector<string>& v, int& numberOfStrings&#41;
&#123;
    // handle errors
    if&#40;numberOfStrings <= 0&#41; return "error";
    if&#40;numberOfStrings > v.size&#40;&#41;&#41; return "error";

    // select a random number in the range 0 - numberOfStrings-1
    int next = rand&#40;&#41; % numberOfStrings;

    // decrement the number of strings
    numberOfStrings--;
    
    // swap it with the last unused string
    swap&#40;v&#91;next&#93;, v&#91;numberOfStrings&#93;&#41;;
    
    // retrieve the string
    return v&#91;numberOfStrings&#93;;
&#125;

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&#40;vector<string>& v, int& numberOfStrings&#41;
&#123;
    if&#40;numberOfStrings <= 0&#41; return "Error";
    if&#40;numberOfStrings > v.size&#40;&#41;&#41; return "Error";

    int next = rand&#40;&#41; % numberOfStrings;
    numberOfStrings--;
    swap&#40;v&#91;next&#93;, v&#91;numberOfStrings&#93;&#41;;
    return v&#91;numberOfStrings&#93;;
&#125;

int main&#40;&#41;
&#123;
    vector<string> q;

    q.push_back&#40;"One"&#41;;
    q.push_back&#40;"Two"&#41;;
    q.push_back&#40;"Three"&#41;;
    q.push_back&#40;"Four"&#41;;
    q.push_back&#40;"Five"&#41;;
    q.push_back&#40;"Six"&#41;;
    q.push_back&#40;"Seven"&#41;;
    q.push_back&#40;"Eight"&#41;;
    q.push_back&#40;"Nine"&#41;;
    q.push_back&#40;"Ten"&#41;;
    
    int num=q.size&#40;&#41;;
    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
    cout << getQuestion&#40;q,num&#41; << endl;    
&#125;

I like that...much faster than my idea and with no guaranteed work to prove the string hasn't been used.

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.

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....:)

commented: Special bonus for bumping a thread nearly 7 YEARS OLD -4
commented: I wonder if I0adidas0I can finish his homework now. -2

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

int number_of_questions = questions.size();
std::vector<int> index;
for(int i = 0; i < number_of_questions; ++i)
{
index.push_back(i);
}

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

//when a new asker arrives
index = shuffle(index);
std::vector<int> shuffled_index = index;

int ind = 0;
//now each time when you get a question
//check ind ok
if(ind >=0 && ind < shuffled_index.size())
{
int cur_ind = shuffled_index[ind];
//use current ind to get the question
if(cur_ind >= 0 && cur_ind < questions.size())
{
std::string question = questions[cur_ind];
}
//get the next random id
++ind
}
else
{
//asked all the questions
}

you only need to randomise when you shuffle

#define & tetro congratz ladies/gents!

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

Thread closed.

commented: Nice! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.