# Thread: combination of number program in c

1. ## combination of number program in c

Hello to all of you.Here is the simple code for finding combination of three number.
Code:
void find()
{
int a,b,c,x,y,z,s;
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
s=a;
else if(b>c)
s=b;
else
s=c;
for(x=1;x<=s;x++)
{
for(y=1;y<=s;y++)
{
for(z=1;z<=s;z++)
{
if(z==a||z==b||z==c)
{
if(y==a||y==b||y==c)
{
if(x==a||x==b||x==c)
{
if(!(x==y||x==z||y==z))
printf("%d%d%d\n",x,y,z);
}
}
}
}
}
}

2. great that you share it with us

3. Your braces are unbalanced. So next time you say "hey, here's a function," make sure it at least compiles. Or was that your question?

Code:
#include <stdio.h>

void find() {
int a,b,c,x,y,z,s;
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
s=a;
else if(b>c)
s=b;
else
s=c;
for(x=1; x<=s; x++) {
for(y=1; y<=s; y++) {
for(z=1; z<=s; z++) {
if(z==a||z==b||z==c) {
if(y==a||y==b||y==c) {
if(x==a||x==b||x==c) {
if(!(x==y||x==z||y==z))
printf("%d%d%d\n",x,y,z);
}
}
}
}
}
}

/*
mingw32-gcc.exe -Wall  -g  -pedantic -Wextra -Wall -ansi -g    -c C:\Users\Josh2\Documents\bar\main.c -o obj\Debug\main.o
C:\Users\Josh2\Documents\bar\main.c: In function 'find':
C:\Users\Josh2\Documents\bar\main.c:25: error: expected declaration or statement at end of input
*/
Add a brace at the very end, as the white space would tell you. Indenting is a necessary tool not really an option.

4. Well, considering you've posted here, you must want to know what's wrong with it. Other than having no indentation and what has been mentioned...

Many problems have specific cases that can be solved with specific algorithms, plus general cases which need a general algorithm. Often times a specific case algorithm is more efficient than the general case algorithm.
What we have here is definitely not a general case algorithm because it clearly only works for three values.
However, at the same time, as a special case algorithm it's less efficient than the general solution.

For example of how this can be more efficient:
The innermost for-loop for z can be replaced with a simple calculation: z = a+b+c-x-y; You then need not perform any of the tests that include z, which means the entire first nested if-statement is redundant, as is part of the innermost one.

Or better yet, you could put the values in an array, have the first loop pick an item from the array and then swap it with the last value. Then the next loop could pick amoung only the first two values and swap its choice with the second position in the array.
Then the last item is taken from the first position in the array.
After each permutation is printed, the items are swaped back to where they were. This very easily turns into the general case algorithm. But to fully do that, you'd have to change the printf & scanf, and perhaps involve recursion or an explicit stack since you can no longer have a fixed number of loops.

You also are filtering out all results whenever some of the numbers entered are the same. I.e. say I entered "2 4 2", which should actually give three results.
It would also be nice if your results were presented in lexicographical order, which the general case solution I described above will do, if the input is sorted.

You could also learn about and apply De Morgan's Laws here, which would simplify the expression:
Code:
if (!(x==y||x==z||y==z))

5. wow iMalc!!! so indepth on a simple program!!!

i like where your going with it though, and would like to see how the whole program advances as now this interests me!!

like having it compute for more then just 3 numbers

also i have only used De Morgan's Laws as they apply to electronics and logic circuits, and yet am at a loss to how it would be used here!!!

cant wait to see though!!

//edit//

just noticed, when any of the numbers are the same, it doesnt do anything!!!

6. To rearrange things with De Morgan's Laws, perform these three steps, in any order:
Negate each individual condition.
Toggle between AND and OR.
Negate the whole thing.

Starting with: !(x==y||x==z||y==z)
First I'd negate the whole thing: You would normally write this as !!(x==y||x==z||y==z) initially but this of course simplifies to just x==y||x==z||y==z so its easier to just take the outer NOT away.
Then I'd change the ORs to ANDs: x==y && x==z && y==z
Then I'd negate the individual expressions: You could write this as !(x==y) && !(x==z) && !(y==z) initially but I would just negate each individual expression by changing the comparison operator instead i.e. x!=y && x!=z && y!=z

Here's part of the general solution, with certain bits intentionally blanked out so that it cannot be copy'n'pasted without first understanding how it works. It also does not remove duplicates:
Code:
void void printPermutations(int arr[], int n, int pos = 0) {
if (?? == n) {
for (int i=0; ??<n; ++i)
printf("%d ", arr[??]);
printf("\n");
return;
}
for (int i=pos; i<??; ++i) {
swap(arr[??], arr[pos]);
printPermutations(arr, ??, pos+1);
swap(arr[i], arr[??]);
}
}