Link to home
Start Free TrialLog in
Avatar of alexanderthegreat
alexanderthegreat

asked on

Howto mix letters

I wonna know the code's to mix the letters witch i give the size of, if the program has mixed them in all possible ways then the program should stop mixing them.
Hopefully its not to much to ask.

Only the code's for mixing letters would be very usefull as well.




Avatar of Exceter
Exceter
Flag of United States of America image

You want to print out every possible combination of the letters in a string which you provide?
Avatar of alexanderthegreat
alexanderthegreat

ASKER

Yes, for example:

#include <iostream.h>
int size;
char letters[27]="abcdefghijklmnopqrstuvwxyz";
cout << "Type in how many letters from the alphabet you want to use for mixing: ";
cin >> size;

Now when a user types 4, for example it has
To use 4 letters from the alphabet and needs to mix them
In all possible ways.
like: afcz, zklp, frrk, tttl, frwe. but when a user types
2 for exemple: az, da, ef, ze, gr, etc.
etc.
Is this homework?
No its not, its just an idea.
I'am pretty new with C++ so i am just trying stuf out.
Is it to hard for you to write that code, heheehehh,
dont be shy to say so.
>> Is it to hard for you to write that code, heheehehh, dont be shy to say so.

No class... Sheesh!

Try this,

#include <iostream>
#include <iomanip>

using namespace std;

void alphaCombo( int, int );

int NUMBER;
char *s;

int main()
{
     cout << "Enter the number of letters: ";
     cin >> NUMBER;

     s = (char*)malloc( sizeof(char) * NUMBER + 1 );

     for( int x = 0; x < NUMBER; x++ )
          s[x] = ' ';

     s[NUMBER] = '\0';

     alphaCombo( NUMBER, 0 );

     return 0;
}

void alphaCombo( int num, int current )
{
     if( num > 26 || num < 1 )
     {
          cout << "\nNumber out of range.\n";
          return;
     }

     if( current == num )
     {
          cout << s << endl;
          return;
     }

     for( int x = 0; x < 26; x++ )
     {
          s[current] = x + 97;
          alphaCombo( num, current + 1 );
     }
}

Exceter
Oops, that code has a memory leak. Insert

free(s);

just before main's return statement.

Exceter
To scramble a string which you supply,

#include <iostream>
#include <iomanip>

using namespace std;

void alphaCombo( int, int );

char *s;
char str[25];
     int NUMBER, len;

int main()
{
     cout << "Enter string to scramble: ";
     cin >> str;

     cout << "\nEnter number of chars to scramble: ";
     cin >> NUMBER;

     len = strlen(str);

     s = (char*)malloc( sizeof(char) * NUMBER + 1 );

     for( int x = 0; x < NUMBER; x++ )
          s[x] = ' ';

     s[NUMBER] = '\0';

     alphaCombo( NUMBER, 0 );

     free( s );

     return 0;
}

void alphaCombo( int num, int current )
{
     if( num == current )
     {
          cout << s << endl;
          return;
     }

     for( int x = 0; x < len; x++ )
     {
          s[current] = str[x];
          alphaCombo( num, current + 1 );
     }
}

Exceter
I realy think you did your best, but it seems to be harder then i thought it would be, when you take a look at the first program of yours it mix letters in many combinations but far from all combinations.
Same with the second. the second  program also repeats same combination.

Anyway thx a lot to even border with this idea of my.


 
ASKER CERTIFIED SOLUTION
Avatar of Exceter
Exceter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You should recive the following output.

tt
te
ts
et
ee
es
st
se
ss

Exceter
alexanderthegreat, some feedback would be nice.
Thanks for your great help Exceter, keep up the great work and let us learn from one another.

Greatings.
>> Thanks for your great help Exceter, keep up the great work...

Yeah my help must been REALLY great if you gave me a B!!!! Why in blazes did you give me a B!?!? If the substantial amounts of functional code I posted was not exactly what you wanted why didn't you say so!!!!!!!!!!!!!!!! I would have bee more than happy to modify it...

>> ...let us learn from one another.

Perhaps you should do some learning right now. Experts find it rather irritating when they answer a question in more than adequate detail and recieve a B for their trouble!!!!!

The grading system works as follows,

A = Excellent - Solves your problem perfectly
B = Good - Sort of solves problem but could be better
C = Average - sort of solves problem but leaves much to be desired

By giving me a B you have told me that my code could have been better. If this is true I fail to see why I should be to blame. Your question has been VERY vauge from the begining. I posted three different versions of the program trying to solve your problem. After the third version you simply did not respond, until now... If that version was not what you wanted you should have shown some consideration for both of us by telling me what was wrong with it. That way you would have gotten a better answer and I wouldn't be irritated...

Remember, the answers you recieve are only as good as the questions you ask...

>> when you take a look at the first program of yours it mix letters in many combinations but far from all combinations.

I still want you to explain this...

Exceter
First of all i dont clearly understand why you get sort of mad for an B, The reason i have given you an B and did not reply anymore is because its not all the information i wanted, this has noting to do with you this is because I'm not from an English speaken country, so i cant cleary explain to you anymore then i already did, now when i see how importand this grading is for an expert, on the second thought i would have given you an A, hope you forgive me for that.
>> First of all i dont clearly understand why you get sort of mad for an B

I'm mad because you gave me a B without giving me the chance to improve it.

>> I'm not from an English speaken country

Couldn't you have said something like, "No, that is not what I want?"

Exceter
Yea you are right i should have at least said something like that, anyway i have need of your wisdom ones again,
i want to make a program that does the following thing:

now what i want is to let the tafels1, tafels2, tafels3, tafels4 come out to a user in different ways, so i dont want to tell the program
to show: cout << tafels1 << endl;
but i want the program to make chooices between:
        char tafels1[10]="1 * 2 = ";
     char tafels2[10]="2 * 2 = ";
     char tafels3[10]="3 * 2 = ";
     char tafels4[10]="4 * 2 = ";
the way of showing the text array's, and i prefer, if you cooice to help me not to use the runtime library because then i dont see how something like that can be done and i will not learn from the sample.

Hope its clear what i mean and that you will
show me this.

Greatings.







>> anyway i have need of your wisdom ones again,

You need to ask this in a new question. It is simply standard operating proceedure around here. It is for everyone's benefit. That way experts get points and you get the input from multiple experts so that you can get the best possible answer for your question.

Note: For future reference, simply tell experts that you are not completly satisfied with their responce. Work with them until they solve your problem. Then reward them with a fair grade. The important thing is to give the experts a chance to earn the A. :-)

Exceter
>> >> when you take a look at the first program of yours it mix letters in many combinations but far from all combinations.

What did you mean by this?

Exceter
You see that it does not mix all the letters in all the combinations possible, check it out i can give you many combination that dont come in the program output, samples:

ab
ac
ad
..
az

ba
..
bz

ca
..
cz

da
..
dz

etc.

when a user types 3 then it haves to look something like this:
aaa
bbb
...
zzz

aab
aac
aad
...
aaz


You see many combo's are missing.







Um, I just tried my first program and it is giving me those comninations.

Exceter
You calling me a liar?
I dont like people calling me a liar.

(Annoyed)


>> You calling me a liar?
>> I dont like people calling me a liar.
>>
>> (Annoyed)

NO!!! I'm not calling you a liar!!!

(Confused)

However, the fact remains that my program does print out those combinations. Try this,

test4 > test.txt // test4 is the name of this program on my machine

Press 2 and then Enter and then edit test.txt

You should see this,

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q
nr
ns
nt
nu
nv
nw
nx
ny
nz
oa
ob
oc
od
oe
of
og
oh
oi
oj
ok
ol
om
on
oo
op
oq
or
os
ot
ou
ov
ow
ox
oy
oz
pa
pb
pc
pd
pe
pf
pg
ph
pi
pj
pk
pl
pm
pn
po
pp
pq
pr
ps
pt
pu
pv
pw
px
py
pz
qa
qb
qc
qd
qe
qf
qg
qh
qi
qj
qk
ql
qm
qn
qo
qp
qq
qr
qs
qt
qu
qv
qw
qx
qy
qz
ra
rb
rc
rd
re
rf
rg
rh
ri
rj
rk
rl
rm
rn
ro
rp
rq
rr
rs
rt
ru
rv
rw
rx
ry
rz
sa
sb
sc
sd
se
sf
sg
sh
si
sj
sk
sl
sm
sn
so
sp
sq
sr
ss
st
su
sv
sw
sx
sy
sz
ta
tb
tc
td
te
tf
tg
th
ti
tj
tk
tl
tm
tn
to
tp
tq
tr
ts
tt
tu
tv
tw
tx
ty
tz
ua
ub
uc
ud
ue
uf
ug
uh
ui
uj
uk
ul
um
un
uo
up
uq
ur
us
ut
uu
uv
uw
ux
uy
uz
va
vb
vc
vd
ve
vf
vg
vh
vi
vj
vk
vl
vm
vn
vo
vp
vq
vr
vs
vt
vu
vv
vw
vx
vy
vz
wa
wb
wc
wd
we
wf
wg
wh
wi
wj
wk
wl
wm
wn
wo
wp
wq
wr
ws
wt
wu
wv
ww
wx
wy
wz
xa
xb
xc
xd
xe
xf
xg
xh
xi
xj
xk
xl
xm
xn
xo
xp
xq
xr
xs
xt
xu
xv
xw
xx
xy
xz
ya
yb
yc
yd
ye
yf
yg
yh
yi
yj
yk
yl
ym
yn
yo
yp
yq
yr
ys
yt
yu
yv
yw
yx
yy
yz
za
zb
zc
zd
ze
zf
zg
zh
zi
zj
zk
zl
zm
zn
zo
zp
zq
zr
zs
zt
zu
zv
zw
zx
zy
zz

As you can see, these combinations are printed out.

Exceter
Can you do this for me please:
now what i want is to let the tafels1, tafels2, tafels3, tafels4 come out to a user in different ways, so i dont want to tell the program
to show: cout << tafels1 << endl;
but i want the program to make chooices between:
    char tafels1[10]="1 * 2 = ";
    char tafels2[10]="2 * 2 = ";
    char tafels3[10]="3 * 2 = ";
    char tafels4[10]="4 * 2 = ";
the way of showing the text array's, and i prefer, if you cooice to help me not to use the runtime library because then i dont see how something like that can be done and i will not learn from the sample.

Please show me how i can shake them.

Thank you.



>> Can you do this for me please:

As I said before, you need to ask this in a new question.

P.S. Did you try what I suggested with my first program? Did you get the same results that I posted?

Exceter
>> P.S. Did you try what I suggested with my first program? Did you get the same results that I posted?

Did you get the same results I posted above?

Exceter
To be honost i have no clue what you mean, perhaps this have to do with my language or my leg of braincells.
I also lost my intrest in it and i want
to move on to the last program what i was talking about.

Greatings exceter.
>> To be honost i have no clue what you mean

I want you to verify that you are getting the same results from that first program that I am. I have checked it repeatedly and it is not possible for it to not be printing out all of the letter combinations that are 2 letters in length. However, there are so many of them that they scroll right off the screen and only the last 298 out of the 675 combinations remain visible in the DOS console, assuming that you have a windows scroll bar to work with, otherwise there are only 23 visible when the program terminates. By typing test4 > numbers.txt you are piping the output of the program to a text file. In this manner you can check to see all of the combinations the program outputed, 675 to be precise. The only tricky part is that ALL of the program's output is sent there. Including the prompt that tells you to enter the number of letter to mix. So, once the program is executed you simply type 2 and press enter. Then edit numbers.txt and look at the output. I beleive that you will find there are no combinations missing.

>>I also lost my intrest in it and i want
>>to move on to the last program what i was talking about.

Two things, you never get anywhere programming things when you tire of the project after a short period of time.

Second, I would be more than happy to help you with this if you would create a new question. If you have already done so and I have simply overlooked it, please post a link to it.

Exceter