473,804 Members | 4,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combinations in C?

Hello all,
I have an array of values which I would like to generate all
possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the
topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations... which I don't care
about.

--
Tom Cameron
tom<at>drdabble s<dot>us
http://drdabbles.us
Nov 14 '05 #1
19 7552


Thomas Cameron wrote:
Hello all,
I have an array of values which I would like to generate all
possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the
topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations... which I don't care
about.


Hint #1: In any combination, each of a,b,c is either
"in" or "out." Does that suggest anything?
Hint #2: One combination is missing from your list.
Does the nature of the missing combination make Hint #1
any clearer?

... and since there's an odor of homework about your
question, hints are all I'm willing to provide just now.
Good luck!

--
Er*********@sun .com
Nov 14 '05 #2
Bob
On Mon, 04 Apr 2005 10:17:26 -0400, Eric Sosman <er*********@su n.com>
wrote:

... and since there's an odor of homework about your
question, hints are all I'm willing to provide just now.


What about PERMUTATIONS ?

abc
acb
bac
bca
cab
aba

String of lenght n makes n! permutations.
What's the best algorithm ?
What's the asintotic computational complexity ?
What's about off-topics ? :-)

(This is a real "problem" of mine: I'd like to mix up
my Name-Surname string to look for significant
nicknames... :-)
Nov 14 '05 #3
Bob
On Mon, 04 Apr 2005 16:30:53 +0200, Bob <bo*@bob.bob> wrote:
abc
acb
bac
bca
cab
aba


Find the mistake !

Nov 14 '05 #4
In article <d2**********@n ews1brm.Central .Sun.COM>,
Eric Sosman <er*********@su n.com> wrote:

a
ab
abc
ac
b
bc
c
Hint #2: One combination is missing from your list.


How can you tell?

-- Richard
Nov 14 '05 #5
<posted & mailed>

Eric Sosman wrote:


Thomas Cameron wrote:
Hello all,
I have an array of values which I would like to generate all
possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the
topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations... which I don't care
about.


Hint #1: In any combination, each of a,b,c is either
"in" or "out." Does that suggest anything?
Hint #2: One combination is missing from your list.
Does the nature of the missing combination make Hint #1
any clearer?

... and since there's an odor of homework about your
question, hints are all I'm willing to provide just now.
Good luck!

--
Er*********@sun .com


Eric,
I can assure you that at 25 years old, I have no homework assigned to me.
Moreover, I believe my employer (www.timberland.com) would want to know
about any schooling I was attending that offered C courses, as they'd
gladly pay me for them. However, this is not the case.
The question I asked is in regards to a project I am working on personally
to test several command line options. Once I get this working I then have
two more for loops to add which will make the final algorithm Nx3x3
combinations. In other words, I'm expecting a rather absurd number of
combinations when all is said and done. (Perl output almost 80MB worth of
text from just the combinations without the for loops). Please note, PERL
is not a final solution to my problem, and I am not a PERL expert...thus
why I want to write this in C.

If anybody has an actual answer, that would be great. And thanks for your
help so far.
--
Tom Cameron
tom<at>drdabble s<dot>us
http://drdabbles.us
Nov 14 '05 #6

Also worth note is the actual answer:

c
b
b c
a
a c
a b
a b c

This is directly from the PERL that I found. Where did I miss one, unless
you're referring to the empty set...which I DO want, but can add manually
if need be.

--
Tom Cameron
tom<at>drdabble s<dot>us
http://drdabbles.us
Nov 14 '05 #7
Thomas Cameron wrote:

I have an array of values which I would like to generate all
possible combinations for. And example would be the values "a,b,c".
This would produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the
topic discussed, but never for strings or anything of the such. Most
discussion is regarding calculating the number of combinations...
which I don't care about.


I've published this before in this group. Combining it with the
following alias is useful:

[1] c:\c\jumble>ali as jumble
\c\jumble\jumbl e %1& | sort | uniq | pr -a -T --columns=8

---------------- cut here for jumble.c ---------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Public domain, by C.B. Falconer. *//* 2003-Aug-21 */
/* Attribution appreciated. */

/* Things get out of hand when larger than 8 */
#define MAXWORD 12

/* ------------------ */

/* exchange 0th and ith char in wd */
void trade(char *wd, unsigned int i)
{
char c;

c = *wd;
*wd = wd[i];
wd[i] = c;
} /* trade */

/* ------------------ */

/* Form all n char permutations of the characters in the
string wd of length lgh into outstring at index ix.
Output the results to stdout. */
void jumble(char *wd, unsigned int lgh,
unsigned int ix, /* output place to fill */
unsigned int n, /* max out places to fill */
char *outstring)
{
unsigned int i;

if (0 == n) {
outstring[ix] = '\0';
puts(outstring) ;
}
else
for (i = 0; i < lgh; i++) {
trade(wd, i); /* nop when (0 == i) */
outstring[ix] = *wd;
jumble(wd+1, lgh-1, ix+1, n-1, outstring);
trade(wd, i); /* restore the wd string */
}
} /* jumble */

/* ------------------ */

int main(int argc, char *argv[])
{
unsigned int n, lgh, min;
double max;
char outstring[MAXWORD];

if (argc < 2) {
fprintf(stderr,
"Usage: jumble <baseword> [lgh]\n"
" where the (optional) lgh specifies the\n"
" maximum length of the output words\n");
return 0;
}
lgh = strlen(argv[1]);
if (lgh >= MAXWORD) argv[1][lgh = MAXWORD-1] = '\0';

min = lgh;
if ((argc > 2) && (1 == sscanf(argv[2], "%u", &n)))
if (n && (n <= lgh)) min = n;

for (n = lgh, max = 1.0; n > (lgh - min); n--)
max = max * n;

fprintf(stderr, "string=\"% s\", max=%.0f, len=%u\n",
argv[1], max, min);

jumble(argv[1], lgh, 0, min, outstring);
return 0;
} /* main, jumble.c */

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #8
Thomas Cameron wrote:
Also worth note is the actual answer:

c
b
b c
a
a c
a b
a b c

This is directly from the PERL that I found. Where did I miss one, unless
you're referring to the empty set...which I DO want, but can add manually
if need be.


Hint #3
You have three "positions" to switch on or off (2 choices) which yields
2**3 possibilities.. . Something ringing?

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #9
<posted & mailed>

Michael Mair wrote:
Thomas Cameron wrote:
Also worth note is the actual answer:

c
b
b c
a
a c
a b
a b c

This is directly from the PERL that I found. Where did I miss one, unless
you're referring to the empty set...which I DO want, but can add manually
if need be.


Hint #3
You have three "positions" to switch on or off (2 choices) which yields
2**3 possibilities.. . Something ringing?

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


In reality, I have 2^19=524288 possible combinations. Of course, each
element is not actually one letter, but an entire string unto itself. I
assume the simplest way to do this would be to put each element into an
array position and iterate through that way? Either way, this is going to
be a pain in the backside, I can tell. And no, nothing's ringing a bell
yet...as I'm a novice with C (at best).

--
Tom Cameron
tom<at>drdabble s<dot>us
http://drdabbles.us
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4711
by: Jim Hubbard | last post by:
Does anyone have any code for doing combinations in VB.net or visual basic ? I'd like to be able to get an array of combinations (not permutations) of words from a base group of words while specifying the number of unique words to be used in the result set. For instance if I have the set {cat dog fish snake} I'd like to be able to get back a result array of all possible combinations (not permutations) of X number of words from my base...
36
9487
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
1
4272
by: M a n i s h | last post by:
i have been trying to build a program to find various combinations of a string. the problem is that if there are multiple similar characters in the string then the program displays multiple similar combinations. how could i overcome this ...comparing each o/p doesn't seem feasible.
15
2423
by: Thomas Cameron | last post by:
Hello all, I have an array of values which I would like to generate all possible combinations for. And example would be the values "a,b,c". This would produce the following: a ab abc ac b
3
5411
by: Ryan | last post by:
I've been trying to find an algorithm that will output all of the possible combinations of items in an array. I know how to find the number of combinations for each set using nCr=n!/(r!(n-r)!) set being possible combinations of r objects from a group of n objects. e.g. n=5 r=3 using A,B,C,D,E as items ABC ABD ABE ACD ACE
2
1493
by: GrantMagic | last post by:
I have found that some strange combinations of characters in a URL can cause an error in my ASP.NET application. This is regarding URL Paramters For example: if i have the URL: http://www.mysite.com/home.aspx?param=123 my page loads fine
0
1721
by: Louis Aslett | last post by:
I hope this is the correct newsgroup for this query (if not please give me a pointer to where is best): I understand the theory of normalisation etc and am trying to follow best practices in the design of the database for a new project, but I am unsure as to the best practice when one wants to store data relating to combinations of arbitrary numbers of sets of data. For example, take the following two groups of sets, each containing...
5
5969
by: Bails | last post by:
Hi all I have a theory for a lotto system and need help on how to code it. I want to create 1 massive database with EVERY combination of numbers possible in a given lotto system, then remove all the numbers in a sequence. For example, in Australia our lotto system has 45 balls and you need to pick 6 numbers to win the jackpot.
2
2274
by: zgfareed | last post by:
Can anyone suggest an algorithm or function to generate combinations/ permutations of a group of substrings stored in a vector. The substrings consists of 3 letters and the resulting string combinations should be of a size that is a multiple of 3.
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4304
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
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.