Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 22nd, 2012, 12:07 AM
Registered User
Points: 19, Level: 1
Points: 19, Level: 1 Points: 19, Level: 1 Points: 19, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 12 Exercise 4

Hi,

apologies for the double post, but I'm happy to have found this place. I've been looking a the above exercise and the solution suggested is:
Code:
    private void swap01<T>(T[] data, int pos1, int pos2)
    {
        T temp;

        temp = data[pos1];
        data[pos1] = data[pos2];
        data[pos2] = temp;
    }
Additionally, this upset the compiler and I had to change the calls to swap to
Code:
swap(data, start, end);
I do see that this passes the data directly to the method from getPivotPoint, but I'm not understanding why this would remove class scope and make the method less sloppy.
  #2 (permalink)  
Old October 23rd, 2012, 01:08 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Privatizing swap()

As a rule, any method should do one task, and do it well. Additionally, it should perform that task "in the dark". That is, as a user of the routine, you don't need to know the gory details of how it works...only that it does work. This is the Black Box approach and is, in fact, one of the strengths of interfaces. By making the swap() method private, you make the sort routine a "pure" sort routine...the user cannot call swap() on their own. Also, swap() really is a helper method and has no need to be exposed to the outside world. To me, that's a good thing.

Dr. Purdum
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #3 (permalink)  
Old October 23rd, 2012, 07:52 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Chapter 12, Question 4

I forgot to comment on the code fragment you listed. I just checked the original code and it is the code you were able to compile. (I was also able to compile it with the new Visual Studio 2012 Express edition, which is the IDE used with the new book.) I'm not sure where the code you listed in the first section came from, but it is not the book's code. You may want to download the code for Chapter 12 again.

Dr. Purdum
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #4 (permalink)  
Old October 24th, 2012, 11:31 PM
Registered User
Points: 19, Level: 1
Points: 19, Level: 1 Points: 19, Level: 1 Points: 19, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by DrPurdum View Post
I forgot to comment on the code fragment you listed. I just checked the original code and it is the code you were able to compile. (I was also able to compile it with the new Visual Studio 2012 Express edition, which is the IDE used with the new book.) I'm not sure where the code you listed in the first section came from, but it is not the book's code. You may want to download the code for Chapter 12 again.

Dr. Purdum
I'm not sure what you mean by 'the code you were able to compile'. I agree that the swap method in listing 12-4 works fine, but the solution you offer makes the compiler unhappy.

The solution to chapter 12, exercise 4 reads:
The solution means that you must pass the data to be swapped into the method. One solution is:
Code:
    private void swap01<T>(T[] data, int pos1, int pos2)
    {
        T temp;

        temp = data[pos1];
        data[pos1] = data[pos2];
        data[pos2] = temp;
    }
Now, as Swap01() gets called by:
Code:
    public void quickSort(int first, int last)
    {
        int start;      // Index for left partition
        int end;        // Index for right partition

        start = first;  // Keep copy of first element of array... 0
        end = last;     // ...and the last one- 99. (if 100 values)

        if (last - first >= 1)   // Make sure there's enough data to sort
        {
            int pivot = data[first];    // Set partitions- what if this is zero?

            while (end > start) // While indexes don't match...
            {   // Do left partition
                while (data[start] /*first value*/ <= pivot && start <= last && end > start)
                    start++;
                // Do right partition
                while (data[end] > pivot && end >= first && end >= start)
                    end--;
                if (end > start)    // If right partition index smaller...
                    swap01(start, end); // ...do a swap
            }
            swap01(first, end); // Swap last element with pivot
            quickSort(first, end - 1); // Sort around partitions
            quickSort(end + 1, last);
        }
        else
        {
            return;
        }

    }
, intellisense informs us, understandably, that there is no method swap01 that takes only two arguments.
Additionally, in the swap method, we get the complaint that you cannot implicitly convert type 'T' to type 'int'.

This is where my confusion was. I'm not sure what adjustments to make as quicksort only returns two values, while swap01 expects three.
  #5 (permalink)  
Old October 25th, 2012, 09:39 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Not my code

I was referring to the code I wrote for the generic sort routine. The code you're showing is not the code I wrote. Take your code and single step through it and compare it to my code and you'll likely see where you're having problems.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 12 exercise 4 outside US Joakim BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 December 16th, 2011 10:51 AM
Chapter 12 - Last try it out exercise pgmrbs BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 August 13th, 2010 12:04 PM
Chapter 12, Exercise 5 Answer cree8uk BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 3 June 5th, 2010 02:33 PM



All times are GMT -4. The time now is 08:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.