DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: help on writing a simple numbers combination program

  1. #1
    Join Date
    Sep 2007
    Posts
    4

    Exclamation 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. #2
    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. #3
    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. #4
    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;
    }
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  5. #5
    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.

Similar Threads

  1. Help Needed w/ simple Calculator Program!
    By blumoon02k in forum Java
    Replies: 4
    Last Post: 03-24-2005, 05:39 PM
  2. JMS: simple program (urgent please)
    By mhkateb in forum Java
    Replies: 1
    Last Post: 03-07-2005, 03:34 PM
  3. Need help with mult. program
    By Linda in forum .NET
    Replies: 1
    Last Post: 11-05-2002, 11:41 AM
  4. a simple program
    By Sindu in forum Java
    Replies: 1
    Last Post: 02-08-2002, 11:35 PM
  5. Replies: 1
    Last Post: 04-12-2001, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center