Thread: combination of number program in c

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    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. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    great that you share it with us

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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))
    Last edited by iMalc; 01-04-2013 at 01:41 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    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!!!
    Last edited by Crossfire; 01-04-2013 at 01:44 PM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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[??]);
        }
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 56
    Last Post: 09-03-2012, 12:00 PM
  2. C program for generating combination
    By ymadhusoodan in forum C Programming
    Replies: 13
    Last Post: 05-18-2010, 09:54 AM
  3. Replies: 8
    Last Post: 09-27-2008, 07:32 PM
  4. Combination program problem
    By Garland in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2007, 03:35 PM
  5. Replies: 1
    Last Post: 08-11-2006, 05:44 AM