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
This is a discussion on How to write a program for... within the C Programming forums, part of the General Programming Boards category; I need to write a program to print all combinations of 1, 2 and 3 using for loop. Now I ...
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
What is the solution you have in mind?Now I am just able to think of a logic as to how to do it.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい！私は天才だからね！ ^_^
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 :-)
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.
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?
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい！私は天才だからね！ ^_^
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"
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?)
using recursion:
Which will work for any number combinations.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; }
Last edited by 52Cent; 02-17-2008 at 06:04 PM.
Don't give solutions to people.
And try indenting that code a little better, as well.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい！私は天才だからね！ ^_^
Also do not use magic constants
putchar(10);
It should be
putchar('\n');
The first 90% of a project takes 90% of the time,
the last 10% takes the other 90% of the time.
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"
They are valid permutations if repetition is allowed.111, 222, and 333 are neither combinations of 1, 2, and 3, nor permutations.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way