472,127 Members | 1,898 Online

# how to print this output in c#?

Hi,

I know it is very easy, but I just can't fngure out the algorithm.

For example: I have these:
string[] a={"1","2"};
string[] b={"a","b" "c"};
string[] c={"d","e","f"};

How to write an algorithm to print all the combinations like:
1 a d
1 a e
1 a f
2 a d
2 a e
2 a f
1 b d
1 b e
1 b f
2 b d
2 b e
2 b f
1 c d
1c e
1 c f
2 b d
2 b e
2 b f
.....
The best I got is to remember the one has most strings and printthem
first. Then loop through. Can someone provide me a formal algorithm to
handle it?

Thanks

May 25 '07 #1
4 40380
I know it is very easy, but I just can't fngure out the algorithm.
For example: I have these:
string[] a={"1","2"};
string[] b={"a","b" "c"};
string[] c={"d","e","f"};

How to write an algorithm to print all the combinations like:

This seems a school exercise. I hope this isn't a school exercise.

foreach (int i in b)
{
foreach (int j in a)
{
foreach (int k in c)
{
Console.WriteLine("{0} {1} {2}", i, j, k);
}
}
}

And you even copied it wrong I think. You wrote twice:
2 b d
2 b e
2 b f

May 25 '07 #2
On May 25, 2:14 pm, "MaxMax" <n...@none.comwrote:
I know it is very easy, but I just can't fngure out the algorithm.
For example: I have these:
string[] a={"1","2"};
string[] b={"a","b" "c"};
string[] c={"d","e","f"};
How to write an algorithm to print all the combinations like:

This seems a school exercise. I hope this isn't a school exercise.

foreach (int i in b)
{
foreach (int j in a)
{
foreach (int k in c)
{
Console.WriteLine("{0} {1} {2}", i, j, k);
}
}

}

And you even copied it wrong I think. You wrote twice:

2 b d
2 b e
2 b f
Maybe that's why he needs a computer to do it :)

May 25 '07 #3

"MaxMax" <no**@none.comwrote in message
news:Fy******************@twister2.libero.it...
>I know it is very easy, but I just can't fngure out the algorithm.
For example: I have these:
string[] a={"1","2"};
string[] b={"a","b" "c"};
string[] c={"d","e","f"};

How to write an algorithm to print all the combinations like:

This seems a school exercise. I hope this isn't a school exercise.

foreach (int i in b)
{
foreach (int j in a)
{
foreach (int k in c)
{
Console.WriteLine("{0} {1} {2}", i, j, k);
}
}
}
Right. I see you deliberately left a small error in, as an exercise for the
student.
May 25 '07 #4

"MaxMax" <no**@none.comwrote in message
news:Fy******************@twister2.libero.it...
>I know it is very easy, but I just can't fngure out the algorithm.
For example: I have these:
string[] a={"1","2"};
string[] b={"a","b" "c"};
string[] c={"d","e","f"};

How to write an algorithm to print all the combinations like:

This seems a school exercise. I hope this isn't a school exercise.

foreach (int i in b)
{
foreach (int j in a)
{
foreach (int k in c)
{
Console.WriteLine("{0} {1} {2}", i, j, k);
}
}
}
Of course, since the arrays contain strings this will fail to run, but
the basic premise is correct.
Bill
May 25 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion.