473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create combinations numbers without repeating in C++

60 New Member
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
15 13174
donbock
2,426 Recognized Expert Top Contributor
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 Contributor
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
capablanca
60 New Member
@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
capablanca
60 New Member
@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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 Contributor
@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
capablanca
60 New Member
@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 Recognized Expert Top Contributor
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

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

Similar topics

8
4315
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 Function/Module that creates relationships for my tables. The problem is that I need to provide for some tables that may have > 32 relationships...
1
1459
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
2159
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 blocks? I have quite a lot of stuff in the ItemTemplate block, but because so much of it is bound fields, creating a seperate user control mapping all...
16
12760
by: codergem | last post by:
How to add two numbers without using the plus operator?
2
1551
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 possible for me to use the custom membeship create user method? If yes how can i call it ? What should i be giving the button_click event of my...
1
1778
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 is what i have so far: Text1.Text = Int(Rnd * 60) + 1 Text2.Text = Int(Rnd * 60) + 1 Text3.Text = Int(Rnd * 60) + 1 Text4.Text = Int(Rnd * 60) +...
4
4258
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 happen to have no decimal fractions, the results through pysqlite are coming through as integers. The funny thing is that when looking at the database...
7
16469
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 10 is call out and display in the label. How can i make sure that number 10 wont be call out and display again?
0
7420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7446
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.