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
Printable View
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?Quote:
Now I am just able to think of a logic as to how to do it.
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.
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?
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.
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;
}
Don't give solutions to people.
And try indenting that code a little better, as well.
Also do not use magic constants
putchar(10);
It should be
putchar('\n');
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.
They are valid permutations if repetition is allowed.Quote:
111, 222, and 333 are neither combinations of 1, 2, and 3, nor permutations.
got it finally :)
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k ;
for (i=1; i<=3; i++)
{
for (j=1; j<=3; j++)
{
for (k=1; k<=3; k++)
{
printf ("%d%d%d\n",i,j,k);
}
}
}
getch();
}
Of course that code isn't standard.
yes "laserlight" had pointed these errors I am making the last time itself. Will see to it that I will not repeat it next time.
Thank you all once again.