473,385 Members | 1,429 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,385 software developers and data experts.

c/c++ programming combination

1
hi everyone,
I was trying to output all the r-combinations of n such that if the user inputs n{1,3,6,2,9} and the r is 3 the output would be like this

1 3 6
1 3 2
1 3 9
3 6 2
3 6 9
6 2 9

i tried to do it with recursion but all i get was an infinite output....
then i tried also with functions but the output is this:
1 1
1 3
1 6
1 9
1 2
1
3 3
3 1
3 6
3 9
3 2
3
. . . .
2 2
2 1
2 3
2 6
2 9
2

anything would be a great help... i'm still a beginner at c/c++ programming that's why i'm having difficulties with these kinds of programs....
Feb 28 '07 #1
2 6883
hi everyone,
I was trying to output all the r-combinations of n such that if the user inputs n{1,3,6,2,9} and the r is 3 the output would be like this

1 3 6
1 3 2
1 3 9
3 6 2
3 6 9
6 2 9

i tried to do it with recursion but all i get was an infinite output....
then i tried also with functions but the output is this:
1 1
1 3
1 6
1 9
1 2
1
3 3
3 1
3 6
3 9
3 2
3
. . . .
2 2
2 1
2 3
2 6
2 9
2

anything would be a great help... i'm still a beginner at c/c++ programming that's why i'm having difficulties with these kinds of programs....

Here's a VB code it is of any help:

Expand|Select|Wrap|Line Numbers
  1. Public Function CombN(ByRef Vctr() As Integer, ByVal N As Integer) As Integer()
  2.  ' returns all combinations of 'N' elements of the elements in vector 'Vctr'
  3.  
  4.    Dim i As Integer, j As Integer, k As Integer, cc As Integer, M As Integer, nv As Integer
  5.  
  6.    nv = UBound(Vctr)         ' number of combinations
  7.  
  8.    ReDim Co(1 To nv, 1 To N)
  9.  
  10.    For i = 1 To N
  11.       cc = 1
  12.       For j = 1 To (nv ^ (i - 1))
  13.          For k = 1 To nv
  14.             For M = 1 To (nv ^ (N - i))
  15.                 Co(cc, i) = Vctr(k)
  16.                 cc = cc + 1
  17.             Next
  18.          Next
  19.       Next
  20.    Next
  21.  
  22. End Function
Cheers,
fitsumtk
Mar 11 '07 #2
Ganon11
3,652 Expert 2GB
What you will have to do is have the set in an array - this is the first step.

Now, if you were computing an r combination of set r, how would you do it without a computer? One way is the start with the first element and choose the next r - 1 numbers - this is your combination. Then you move the last number one to the right, and repeat, until you reach the end. Then you move the second to last number to the right and repeat the first process.

Are you starting to see a pattern here? Here's the first few 3 combinations of a sample set:

Set n = {1, 2, 3, 4, 5}
r = 3

Comb 1: {1, 2, 3}
Comb 2: {1, 2, 4}
Comb 3: {1, 2, 5}
Comb 4: {1, 3, 4}
Comb 5: {1, 3, 5}
Comb 6: {1, 4, 5}
Comb 7: {2, 3, 4}

etc.
Mar 11 '07 #3

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

Similar topics

1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
42
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming...
4
by: Sreekanth | last post by:
Hi all, I have implemented a timing out version of fgets function call. I am pasting the entire code below. I have following doubts: 1. The code which I have written does it follow standard C...
25
by: Dave | last post by:
I want to spend two minutes on my soapbox. I love C#. I am so productive in it it's ridiculous. But it's so easy to write code that uses poor design principles. Example: just making...
10
by: Volker Hetzer | last post by:
Hi! After messing around for some time with Tcl/Tk and VBScript, we've finally reached the point where we need to put some speed into our applications. So, our typical application looks like...
38
by: ifti_crazy | last post by:
I am VB6 programmer and wants to start new programming language but i am unable to deciced. i have read about Python, Ruby and Visual C++. but i want to go through with GUI based programming...
3
by: leifmale | last post by:
A Factorial And Combination Programming -------------------------------------------------------------------------------- GUYS GOOD AFTERNOON,, PLS help me in my project it goes: Write a...
7
by: gw7rib | last post by:
On 18 Aug, 13:59, "E.D.G." <edgrs...@ix.netcom.comwrote: (snip) (snip) So which is it?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.