469,328 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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 40270
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.

Similar topics

2 posts views Thread by Martin Foster | last post: by
reply views Thread by Sure | last post: by
1 post views Thread by Rafal Lagowski | last post: by
2 posts views Thread by Developwebsites | last post: by
2 posts views Thread by Brian Keanie | last post: by
4 posts views Thread by Ben Holness | last post: by
3 posts views Thread by cdfd1234 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.