473,425 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

How to create combinations numbers without repeating in C++

Hi
How to create a program that ask to the user enter 10 numbers and the program can form all the combinations of 5 numbers without repeating. Thanks for any advice.
Jul 17 '10 #1

✓ answered by Junjie

Hi capablanca

You should look carefully writed by donbock:

The "10 choose 5" combinations of the numbers 0-9 are:
0 and all "9 choose 4" combinations of 1-9; and
1 and all "8 choose 4" combinations of 2-9; and
2 and all "7 choose 4" combinations of 3-9; and
3 and all "6 choose 4" combinations of 4-9; and
4 and all "5 choose 4" combinations of 5-9; and
5 and all "4 choose 4" combinations of 6-9.

Similarly,
The "9 choose 4" combinations of 1-9 are:
1 and all "8 choose 3" combinations of 2-9; and
2 and all "7 choose 3" combinations of 3-9; and
3 and all "6 choose 3" combinations of 4-9; and
4 and all "5 choose 3" combinations of 5-9; and
5 and all "4 choose 3" combinations of 6-9; and
6 and all "3 choose 3" combinations of 7-9.

The "8 choose 4" combinations of 2-9 are:
2 and all "7 choose 2" combinations of 3-9:and
3 and all "6 choose 2" combinations of 4-9:and
4 and all "5 choose 2" combinations of 5-9:and
5 and all "4 choose 2" combinations of 6-9:and
6 and all "3 choose 2" combinations of 7-9:and
7 and all "2 choose 2" combinations of 8-9.

if you can understand it,and find it is orderliness.

I just made your code change,and the second for loop of the first number is determined by the first loop,and the first number in the third loop is determined by the second loop,and so on,only six time for per loop.

15 13157
donbock
2,426 Expert 2GB
How many combinations of 5 numbers can you form from ten distinct numbers? Do you know the formulas for combinations and permutations?
Jul 18 '10 #2
newb16
687 512MB
Expand|Select|Wrap|Line Numbers
  1. dostuff(set<int> picked, set<int> remaining)
  2. {
  3.   if (size of 'picked' is 5) print its content
  4.     else
  5.   for ('item' in all items in 'remaining' that are greater than the largest item in 'picked')
  6.   {
  7.     dostuff(picked+'item', remaining-'item');
  8.   }
  9. }
  10. ...
  11. dostuff(empty set, <1..10> )
So ensuring that i-th element of combination is greater than previous guarantees that they will not repeat.

If numbers were hardcoded from 1 to 10, 'remaining' set may be dropped and number from the greatest in 'picked' to 10 used instead.

Yes, it will be rather slow because each set operation will allocate memory. Bit sets pre-allocated for each recursion step will probably be faster.
Jul 18 '10 #3
@donbock
Hi donbock
This is the formula for combination:

c = n!/(r! * (n - r)!)
c = 10!/(5! * (10 - 5)!)
c = 252 COMBINATIONS WITHOUT REPETITION

I think I do not need to use the formula for permutation. Thanks
Jul 20 '10 #4
@newb16
Hi newb16
I appreciate you are trying to help me but I do not get what you reply me can you give me an example in c++. Thanks
Jul 20 '10 #5
donbock
2,426 Expert 2GB
This problem can be split into three separate tasks:
  • Acquire ten input values from the user.
  • Verify the values are valid (all values are distinct).
  • Generate a list of the "10 choose 5" combinations.
I suggest that you store the ten values in an array, generate the "10 choose 5" list of the values 0-9, and finally use those values to index into the input array to get the user's values. I would start by printing out the 0-9 values, and only replace them with the user inputs after your combination function is working.

As you pointed out earlier, there are 252 combinations; that is 252 lines of output. How do you plan to verify the output is correct and complete? Order is unimportant in combinations, but perhaps there is a certain order that will make verification easier.If this is an assignment, then how will your instructor verify your work? It is worth your while to produce an output that is more easily recognized as correct by the instructor.
Jul 20 '10 #6
donbock
2,426 Expert 2GB
The "10 choose 5" combinations of the numbers 0-9 are:
0 and all "9 choose 4" combinations of 1-9; and
1 and all "8 choose 4" combinations of 2-9; and
2 and all "7 choose 4" combinations of 3-9; and
3 and all "6 choose 4" combinations of 4-9; and
4 and all "5 choose 4" combinations of 5-9; and
5 and all "4 choose 4" combinations of 6-9.

Similarly,
The "9 choose 4" combinations of 1-9 are:
1 and all "8 choose 3" combinations of 2-9; and
2 and all "7 choose 3" combinations of 3-9; and
3 and all "6 choose 3" combinations of 4-9; and
4 and all "5 choose 3" combinations of 5-9; and
5 and all "4 choose 3" combinations of 6-9; and
6 and all "3 choose 3" combinations of 7-9.

And so on. It might be helpful to think of each combination as being composed of a fixed prefix and a yet-to-be determined suffix sub-combination.
Jul 20 '10 #7
newb16
687 512MB
@capablanca
This will be not a reply but a complete debugged and working solution teaching nothing. I suggested to use a recusrive function, where on each iteration generated partial combination is passed, and then either printed if it is of required size, or new level of recursion is launched, adding new numbers to the combination in the following way:
suppose we have number from 1 to 10, and partially generated combination of size 3 - {3,5,6}; Starting from the greater number not included in the set, numbers 7 to 10 are added during 4 iteration of loop.
Jul 20 '10 #8
@donbock
Hi donbock
This is not an assignment. I have 6 months learning C++ by myself
This is what I have created so far:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int x=0 ;
  9.     int array[10];
  10.  
  11.     cout<<"\n   Enter 10 numbers differents: ";
  12.  
  13.     for(int i=0; i<10; i++)
  14.       {
  15.            cin>>array[i];
  16.            if(array[i]==array[i+1])
  17.             {
  18.               cout<<"\n\n   You just enter a number several times  "
  19.                    "try again\n";
  20.             }
  21.       }
  22.       cout<<endl;
  23.       cout<<setw(80)<<"   Generating All Combination Elements Without" 
  24.                         " Repetition\n";
  25.       cout<<setw(85)<<"============================================"
  26.       "==================\n\n";
  27.  
  28.       for (int i = 0; i <6; ++i)
  29.        {
  30.         for (int j = i+1; j <7; ++j)
  31.          {
  32.           for (int k = i+2; k <8; ++k)
  33.            {
  34.             for (int l = i+3; l <9; ++l)
  35.              {
  36.               for (int m = i+4; m <10; ++m)
  37.                {
  38.                  ++x;
  39.                  cout<<setw(36)<<x<<")      "<<array[i]<<"     "
  40.                  <<array[j]<<"     "<<array[k]<<"     "
  41.                  <<array[l]<<"     "<<array[m]<<endl;
  42.                }
  43.              }
  44.            }
  45.          }
  46.        }
  47.     cout<<endl<<endl;
  48.     return 0;
  49. }
As you can see the code is wrong:
First, when I enter two numbers that are equals, the message do not appear
Second, I can not get your idea to the code. Can you help me. Thanks
Jul 22 '10 #9
donbock
2,426 Expert 2GB
To determine if array[i] is a duplicate you need to compare it to all previous array elements, from 0 to i-1. Accessing element i+1 is a mistake in two ways, you're looking at an uninitialized element of the array; and when i=9 you're looking past the end of the array.

Refer to reply #7. The most natural way to solve this problem is through recursion.

I'm not a C++ programmer, but I've heard rumors of nifty dynamic array class templates (vector comes to mind) that would be a very handy way to pass the prefix combination to the recursive function.

I need to step back now that you're moving from general algorithm to specific C++ code. There are many C++ experts who can help you with coding details.
Jul 22 '10 #10
Hi
Is there somebody can help me with my program of how to create combinations numbers without repeating in C++ using iteration (loops), not a recursive function. Thanks
Jul 30 '10 #11
Junjie
2
hi capablanca

this code can do .

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int x=0 ;
  9.     int array[10];
  10.  
  11.     cout<<"\n   Enter 10 numbers differents: ";
  12.  
  13.     for(int i=0; i<10; i++)
  14.       {
  15.            cin>>array[i];
  16.            if(array[i]==array[i+1])
  17.             {
  18.               cout<<"\n\n   You just enter a number several times  "
  19.                    "try again\n";
  20.             }
  21.       }
  22.       cout<<endl;
  23.       cout<<setw(80)<<"   Generating All Combination Elements Without" 
  24.                         " Repetition\n";
  25.       cout<<setw(85)<<"============================================"
  26.       "==================\n\n";
  27.  
  28.       for (i = 0; i <6; ++i)
  29.        {
  30.         for (int j = i+1; j <7; ++j)
  31.          {
  32.           for (int k = j+1; k <8; ++k)
  33.            {
  34.             for (int l = k+1; l <9; ++l)
  35.              {
  36.               for (int m = l+1; m <10; ++m)
  37.                {
  38.                  ++x;
  39.                  cout<<setw(36)<<x<<")      "<<array[i]<<"     "
  40.                  <<array[j]<<"     "<<array[k]<<"     "
  41.                  <<array[l]<<"     "<<array[m]<<endl;
  42.                }
  43.              }
  44.            }
  45.          }
  46.        }
  47.     cout<<endl<<endl;
  48.     return 0;
  49. }
Jul 31 '10 #12
@Junjie
Hi Junjie
The program works Thanks
Can you explain a little bit how the for loops works, once again Thanks
Jul 31 '10 #13
Junjie
2
Hi capablanca

You should look carefully writed by donbock:

The "10 choose 5" combinations of the numbers 0-9 are:
0 and all "9 choose 4" combinations of 1-9; and
1 and all "8 choose 4" combinations of 2-9; and
2 and all "7 choose 4" combinations of 3-9; and
3 and all "6 choose 4" combinations of 4-9; and
4 and all "5 choose 4" combinations of 5-9; and
5 and all "4 choose 4" combinations of 6-9.

Similarly,
The "9 choose 4" combinations of 1-9 are:
1 and all "8 choose 3" combinations of 2-9; and
2 and all "7 choose 3" combinations of 3-9; and
3 and all "6 choose 3" combinations of 4-9; and
4 and all "5 choose 3" combinations of 5-9; and
5 and all "4 choose 3" combinations of 6-9; and
6 and all "3 choose 3" combinations of 7-9.

The "8 choose 4" combinations of 2-9 are:
2 and all "7 choose 2" combinations of 3-9:and
3 and all "6 choose 2" combinations of 4-9:and
4 and all "5 choose 2" combinations of 5-9:and
5 and all "4 choose 2" combinations of 6-9:and
6 and all "3 choose 2" combinations of 7-9:and
7 and all "2 choose 2" combinations of 8-9.

if you can understand it,and find it is orderliness.

I just made your code change,and the second for loop of the first number is determined by the first loop,and the first number in the third loop is determined by the second loop,and so on,only six time for per loop.
Jul 31 '10 #14
Can any one tell me how to do that
Expand|Select|Wrap|Line Numbers
  1. #       for (i = 0; i <6; ++i)
  2. #        {
  3. #         for (int j = i+1; j <7; ++j)
  4. #          {
  5. #           for (int k = j+1; k <8; ++k)
  6. #            {
  7. #             for (int l = k+1; l <9; ++l)
  8. #              {
  9. #               for (int m = l+1; m <10; ++m)
  10. #                {
  11. #                  ++x;
  12. #                  cout<<setw(36)<<x<<")      "<<array[i]<<"     "
  13. #                  <<array[j]<<"     "<<array[k]<<"     "
  14. #                  <<array[l]<<"     "<<array[m]<<endl;
  15. #                }
  16. #              }
  17. #            }
  18. #          }
  19. #        }
but with unknown number of loops..
Aug 19 '10 #15
newb16
687 512MB
Using recursion for example - each loop uses the variable of the outer loop as starting value and set of already known part of the combination generated in all outer loops.
Aug 19 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Brian S. Smith | last post by:
Hi gang, Please help. I've been through the Access help, searched the Web, and I can't seem to get a straight answer. As the Subject line suggests, I want to run a fairly simple VB/Access Sub...
1
by: boy0612 | last post by:
How to create asp webserver without registry for finding aspnet_isapi.dll and how to be patched up for System.Web.dll ? thanks.
3
by: Danny Tuppeny | last post by:
Hi all, Is there a way in something like a DataList, to have a different colour background (via CssClass) without repeating all my code in both the ItemTemplate and AlternatingItemTemplate...
16
by: codergem | last post by:
How to add two numbers without using the plus operator?
2
by: =?Utf-8?B?cHJhZHk=?= | last post by:
Hi, Is it possible for me to create new users without using the wizard? I am using a custom membership as i want to capture more data other than the ones which are available in the wizard. Is it...
1
by: gobblegob | last post by:
Hi guys i am trying to randomize 6 numbers into 6 textboxes up to the value of 60 without repeating a number but i dont know how an error handler would work for duplicate numbers any idea's? this...
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
7
by: Suprim | last post by:
I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out? For example, if number...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.