Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

how to print this output in c#?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DAXU@HOTMAIL.COM - 25 May 2007 12:01 GMT
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
MaxMax - 25 May 2007 12:14 GMT
> I know it is very easy, but I just can't fngure out the algorithm.
> For example: I have these:
[quoted text clipped - 3 lines]
>
> 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
Moty Michaely - 25 May 2007 12:20 GMT
> > I know it is very easy, but I just can't fngure out the algorithm.
> > For example: I have these:
[quoted text clipped - 23 lines]
> > 2 b e
> > 2 b f

Maybe that's why he needs a computer to do it :)
Michael A. Covington - 25 May 2007 15:17 GMT
>> I know it is very easy, but I just can't fngure out the algorithm.
>> For example: I have these:
[quoted text clipped - 16 lines]
>    }
> }

Right.  I see you deliberately left a small error in, as an exercise for the
student.
Bill Butler - 25 May 2007 16:15 GMT
>> I know it is very easy, but I just can't fngure out the algorithm.
>> For example: I have these:
[quoted text clipped - 16 lines]
>    }
> }

Of course, since the arrays contain strings this will fail to run, but
the basic premise is correct.
   Bill

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.