User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Mar 2003
Posts: 1
Reputation: I0adidas0I is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
I0adidas0I I0adidas0I is offline Offline
Newbie Poster

Duplication

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2003
Posts: 37
Reputation: Xelfinn is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
Xelfinn Xelfinn is offline Offline
Light Poster
  #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  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 107
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb
  #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?
Reply With Quote  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
Colleague
Bob Bob is offline Offline
Team Member
  #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):

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;    
}
Reply With Quote  
Join Date: Aug 2003
Posts: 37
Reputation: Xelfinn is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
Xelfinn Xelfinn is offline Offline
Light Poster
  #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  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
Colleague
Bob Bob is offline Offline
Team Member
  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:55 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC