how to print this output in c#?

  • Thread starter Thread starter DAXU
  • Start date Start date
D

DAXU

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
 
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:
 
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 :)
 
MaxMax said:
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.
 
MaxMax said:
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
 
Back
Top