Wrox Programmer Forums Chapter 3 Exercise 4 Solution
 |
BOOK: Beginning Visual C# 2010
 This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266 Read more about Beginning Visual C# 2010 or buy the book from your favorite retailerDownload the code for Beginning Visual C# 2010
 Welcome to the p2p.wrox.com Forums. You are currently viewing the BOOK: Beginning Visual C# 2010 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

May 18th, 2010, 10:19 PM
 KurtBergman Registered User Join Date: May 2010 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts
Chapter 3 Exercise 4 Solution

in the appendix, the solution to this exercise is:

resultVar += ((var1 * var2) << (var4 / var5));

I could not find an explanation for "<<"

thanks
kb

May 19th, 2010, 12:33 AM
 KurtBergman Registered User Join Date: May 2010 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts

okay...
found the answer (sort of) here:
http://msdn.microsoft.com/en-us/libr...(v=VS.71).aspx

which leaves me more confused than ever as to why that is the answer to Chapter 3 Exercise 4 Solution

June 22nd, 2010, 10:38 PM
 wwpowell_ga Registered User Join Date: Jun 2010 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts
Typo

I believe that's just a typo and the correct answer is:

resultVar += (((var1 * var2) + var3) % (var4 / var5));

July 15th, 2010, 04:22 AM
 bon_chan Authorized User Join Date: Mar 2010 Posts: 14 Thanks: 0 Thanked 1 Time in 1 Post

Yes it is a typo error.

resultVar += ((var1 * var2) + ((var3 % var4) / var5));

*, % and / have the same priority and in that case it is the leftmost operator that operates first.

July 15th, 2010, 09:20 PM
 tariq Authorized User Join Date: Jun 2010 Posts: 27 Thanks: 8 Thanked 0 Times in 0 Posts
Operator Priority

According to the book:
* has first priority
/ has second and
% has third
page 51 of Beginning Visual C# 2010

So the corrct answer should be:
resultVar += ((var1 * var2) + (var3 % (var4 / var5)));

Tariq

July 16th, 2010, 05:24 AM
 bon_chan Authorized User Join Date: Mar 2010 Posts: 14 Thanks: 0 Thanked 1 Time in 1 Post

No tariq it is not the right answer :

*, / and % have the same priority.

The order displayed in the book or in any website doesn't mean anything.
what I mean is : it is the same whether they write
*, /, %
or
/, *, %

What only matters is the rank (Higher to Lowest)

Try it out on Visual!

July 16th, 2010, 07:57 AM
 tariq Authorized User Join Date: Jun 2010 Posts: 27 Thanks: 8 Thanked 0 Times in 0 Posts
Thanks

Thanks for the clarification:
What you are saying is that which ever operator comes first out of */and % the calculation will start from there:

Am i right?

For example in this case:
resultVar += (var1 * var2) + (var3 % var4) / var5;

Please correct me if i am on the wrong track.

Tariq

July 16th, 2010, 08:27 AM
 bon_chan Authorized User Join Date: Mar 2010 Posts: 14 Thanks: 0 Thanked 1 Time in 1 Post

Yes you got it!

That's exactly it

So in your example, the processing will be performed in this order:
*
%
/
+
+=

That is only if you write it without brackets, I mean like that:
resultVar += var1 * var2 + var3 % var4 / var5;

If you use brackets like for example: resultVar += var1 * (var2 + var3) % var4 / var5;
The order will be:
+
*
%
/
+=

Let me know if you understand my explanation (as English is not my first language).

Bon_chan

Last edited by bon_chan; July 16th, 2010 at 08:34 AM..

July 16th, 2010, 08:13 PM
 tariq Authorized User Join Date: Jun 2010 Posts: 27 Thanks: 8 Thanked 0 Times in 0 Posts
That is perfect:

Thanks for explaining.

Hope to c u in some other discussion.

Regards
Tariq

 Similar Threads Thread Thread Starter Forum Replies Last Post Chapter 3 - Exercise 4 - Solution gives no output Cryotic BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 April 19th, 2010 08:57 AM Chapter 13 Exercise 3 solution firblazer BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 19th, 2008 07:16 AM Solution to exercise 7, Chapter 2 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 May 28th, 2006 04:28 AM exercise solution uddessjava Java Basics 1 December 29th, 2005 04:57 AM Exercise Solution jgrat BOOK: Beginning Access VBA 3 October 18th, 2004 04:13 PM