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....
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: - Public Function CombN(ByRef Vctr() As Integer, ByVal N As Integer) As Integer()
-
' returns all combinations of 'N' elements of the elements in vector 'Vctr'
-
-
Dim i As Integer, j As Integer, k As Integer, cc As Integer, M As Integer, nv As Integer
-
-
nv = UBound(Vctr) ' number of combinations
-
-
ReDim Co(1 To nv, 1 To N)
-
-
For i = 1 To N
-
cc = 1
-
For j = 1 To (nv ^ (i - 1))
-
For k = 1 To nv
-
For M = 1 To (nv ^ (N - i))
-
Co(cc, i) = Vctr(k)
-
cc = cc + 1
-
Next
-
Next
-
Next
-
Next
-
-
End Function
Cheers,
fitsumtk
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: leifmale |
last post by:
A Factorial And Combination Programming
--------------------------------------------------------------------------------
GUYS GOOD AFTERNOON,,
PLS help me in my project it goes:
Write a...
|
by: gw7rib |
last post by:
On 18 Aug, 13:59, "E.D.G." <edgrs...@ix.netcom.comwrote:
(snip)
(snip)
So which is it?
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
| |