DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: help on writing a simple numbers combination program

1. Registered User
Join Date
Sep 2007
Posts
4

## help on writing a simple numbers combination program

hi , i'm new to this forum , may i say this is a great site , now i want some pointer pls on writing this program in c++ ,
i am trying to write a program where the user input 10 numbers and from the the program will generates let's say 20 best combinations set of 3 possibles
what i mean is this let say the user input 0 3 4 6 7 8 9 1
and it will generate a couple set of 3 for example : 046 037 etc

or A B C D E F G H I J = ABF ABG ABH ABI ABJ OR FGH FGI FGJ OR GHI GHJ

i will really appreciate if you or anyone can help me on that thx for everything.

2. Senior Member
Join Date
Dec 2003
Posts
3,366
this will work if you always have at least 3 numbers, but it might take a while to pick them randomly if you *only* have 3... maybe you can improve on it but its a start. You can also use random permutation in the standard library algorithms. You could generate all the combinations and pick some randomly as well, if you prefer.

int three[3] = {0};
//get three random inputs in range
three[0] = rand%(size_of_input);
three[1] = rand%(size_of_input);
three[2] = rand%(size_of_input);

//reroll until you get different random picks for the first 2
while(three[0] == three[1])
three[1] = rand%(size_of_input);

//reroll the third until it is different from the first 2
while(three[0] == three[2] || three[1] == three[2])
three[2] = rand%(size_of_input);

done, output is the letters in the original array indexed by the values in three.

3. Registered User
Join Date
Sep 2007
Posts
4

## re:

yes , so if the user input 10 it will return like let say 20 or 30 combination , in set of 3 right ? thx for your reply
the combination produce is based on the user input ? right thx .
Last edited by new guy; 01-25-2008 at 01:26 AM.

4. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
here a bit more minimalistic solution:
Code:
```#include <string>
#include <algorithm>
#include <iostream>

int main()
{
std::string inp("abcde1234"); // set a default value for inp
// assign a value to inp
// ....

for(size_t i = 0; i < 10; i++)
{
std::random_shuffle(inp.begin(),inp.end());
std::cout << inp.substr(0,3) << std::endl;
}

return 0;
}```

5. Senior Member
Join Date
Dec 2003
Posts
3,366
no mine was just to return 3 total. It was not really meant to be scaled, it was meant to show you the lowest level approach to the problem so you can understand how to express it in c++. For a flexible approach just do the above shuffle approach or the permutation approach. Mine will get ugly fast with the re-rolls.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise