Thread: How to write a program for...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    29

    How to write a program for...

    I need to write a program to print all combinations of 1, 2 and 3 using for loop.

    Now I am just able to think of a logic as to how to do it.

    Can somebody guide me please.

    Thanks in advance

    Cheers
    babyboy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now I am just able to think of a logic as to how to do it.
    What is the solution you have in mind?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    I can print all 3 numbers in reverse or forward using a for loop. But after that I am just not sure what to do.

    I know that i will have to use a nested for loop, but dont know exactly how.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you aren't exactly telling your whole solution to the problem.
    If you pointed out how, exactly, in steps how you would do it?
    It should be easy to understand how to make the code once you fully understand how you would do it in real life.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    To be honest even I have not thought of it like that... as in terms of steps. please give me 5 mins and let me try :-)

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    I am not able to come up with anything. I am sitting here looking at the screen and my mind is blank.

    Can you please give me a step by step break up and let me try coding it please.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm telling you that you should have a logic on how you want to do it first.
    So say you got a paper, how would you write down all those possible combinations?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lets start by looking at the expected output:
    (), (1), (2), (3), (1,2), (1,3), (2,3), (1,2,3)
    Of course the order you output these probably doesn't matter, so long as each one is only printed once.
    Now I'll happily state for you that to list all the combinations than include the numbers from 1 to n, you can first generate all the combinations from 1 to n-1, and then duplicate them, adding n to each element in the copy.
    e.g. Here are the combinations of 1 and 2: (), (1), (2), (1,2).
    And here are the combinations of 1: (), (1). See the pattern?
    Applying that rule recursively gives you one possible answer, though it can also easily enough be done without recursion.
    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"

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I was assuming the OP was looking for permutations, like this:

    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1

    If this assumption is correct, your simplest solution will involve 3 nested for loops each going from 1 to 3. Code that up, including a printf, and look at the ouput. Then use an if statement to filter out the ones you don't want. What do they have in common?

    I also assume that "babyboy" is a girl. What guy would call themselves babyboy?
    (Then again, what woman would put a bragging quote in their signature?)

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    using recursion:
    Code:
    void Show(int arr[], size_t n)
    {
    for(int i=0;i<n;i++)     
     if(arr[i]!=0)
       printf("%d,",arr[i]); 
    putchar(10);
    }
    
    void Combination(int k, int arr[], size_t n)
    {
     if(k>=n) return;
     
     Combination(k+1,arr, n);  
     arr[k]+=(k+1);
     Show(arr, n); 
     Combination(k+1,arr, n);  
     arr[k]-=(k+1);
    }
    
    int main()
    {   
    int arr[3]={0};    
    Combination(0, arr, 3);   
    getchar();  
    return 0;
    }
    Which will work for any number combinations.
    Last edited by 52Cent; 02-17-2008 at 07:04 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't give solutions to people.
    And try indenting that code a little better, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Also do not use magic constants
    putchar(10);

    It should be

    putchar('\n');
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    29
    Quote Originally Posted by oogabooga View Post
    I was assuming the OP was looking for permutations, like this:

    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1

    If this assumption is correct, your simplest solution will involve 3 nested for loops each going from 1 to 3. Code that up, including a printf, and look at the ouput. Then use an if statement to filter out the ones you don't want. What do they have in common?

    I also assume that "babyboy" is a girl. What guy would call themselves babyboy?
    (Then again, what woman would put a bragging quote in their signature?)

    also shouldnt i be printing
    111
    222
    333

    And does it really matter if i am boy or a gal

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by oogabooga View Post
    I was assuming the OP was looking for permutations
    Why would you assume that? There had been nothing posted so far that suggested what was asked for wasn't what was desired.
    If someone know's the difference, they know which to ask for, if they don't they usually just copy what the assignment says.

    111, 222, and 333 are neither combinations of 1, 2, and 3, nor permutations.
    If you want to know the difference, then the answer is easily found:
    http://www.google.com/search?hl=en&q...utations&meta=
    If you are sure that 111 etc should be part of your output then you're wanting for something totally unrelated to what you asked for.
    I think perhaps you should state what YOU want the output to be, so we are all on the same page.
    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"

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    111, 222, and 333 are neither combinations of 1, 2, and 3, nor permutations.
    They are valid permutations if repetition is allowed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  2. how could i write this program: cat < apa | wc | wc > bepa
    By strugglingman in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 04:40 PM
  3. Is there another way to write this program?
    By agentxx04 in forum C Programming
    Replies: 1
    Last Post: 11-23-2004, 09:28 PM
  4. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM