Wrox Programmer Forums
|
BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C#
This is the forum to discuss the Wrox book MCSD Certification Toolkit (Exam 70-483): Programming in C# by Tiberiu Covaci, Rod Stephens, Vincent Varallo, Gerry O'Brien; ISBN: 978-1-118-61209-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 17th, 2014, 05:59 PM
Registered User
 
Join Date: May 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 2 Page 43

Why is the outer loop used here? The following code is generating 6 random numbers 49 times, yet at the end only 6 numbers are written out.

Code:
// pick 6 random numbers
for (int limit = 0; limit < 49; limit++)
{
for (int select = 0; select < 6; select++)
{
picked[select] = range[rnd.Next(49)];
}

}
Console.WriteLine("Your lotto numbers are:");
for (int j = 0; j < 6; j++)
{
Console.Write(" " + picked[j] + " ");
}
 
Old May 17th, 2014, 08:07 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I didn't write that chapter, so I can't be absolutely sure. But the text says the example demonstrates nested loops. You're right, however, that the outer loop doesn't really do anything so it's sort of a trivial example. (One might think picking the numbers 49 times would make it "more random," but it really doesn't.)

Here's a slightly more interesting example. It picks 10 sets of 6 numbers, assuming you're going to buy 10 lotto tickets.

Code:
// Make 10 picks.
for (int pick = 0; pick < 10; pick++)
{
    Console.WriteLine("Pick " + pick + ": ");
    for (int select = 0; select < 6; select++)
    {
        Console.Write(rnd.Next(49) + " ");
    }
    Console.WriteLine();
}
Even this one doesn't guarantee that the picks don't duplicate each other. For example, one set of six numbers might include the same number twice.

Still, it's good enough to demonstrate nested loops. (If you want to know how to avoid that, reply and I'll show you. )
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old May 18th, 2014, 11:32 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Okay, I got interested in this and wrote up this solution. (Note the use of LINQ's Enumerable.Range method to generate the initial values 1 to 49.)

Code:
// Initialize the array of values between 1 and 49.
int[] values = Enumerable.Range(1, 49).ToArray();

// Make a random number generator.
Random rnd = new Random();

// Make 10 picks.
for (int pick = 0; pick < 10; pick++)
{
    // Randomize the first 6 values.
    for (int i = 0; i < 6; i++)
    {
        // Pick a value between position i and the last position.
        int j = rnd.Next(i, 48);

        // Swap the values in positions i and j.
        int temp = values[i];
        values[i] = values[j];
        values[j] = temp;
    }

    // Display the results.
    Console.Write("Pick " + pick + ": ");
    for (int i = 0; i < 6; i++)
    {
        Console.Write(values[i] + " ");
    }
    Console.WriteLine();
}
This is a lot more work than the simple nested loops that the chapter wanted to demonstrate, but I think it's interesting.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old May 18th, 2014, 01:56 PM
Registered User
 
Join Date: May 2014
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks for your replies

I guess that's what was missing, if you are generating all those number, might as well print them out :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch.2 pg.43 Try It Out Thibbs23 BOOK: Beginning Android Application Development 1 February 21st, 2012 10:40 AM
Pages 43-44: findMToLastElement WayneHeym BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 2nd Ed ISBN: 978-0-470-12167-2 0 May 27th, 2011 02:16 PM
Problem with excercise in page 43 (Ch. 2) Luachan BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 8 January 25th, 2010 03:02 PM
Help with page 42 - 43 of the text Whiteetea BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 October 28th, 2008 10:51 PM
Listing 23-43 wombel BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 0 November 30th, 2006 09:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.