Wrox Programmer Forums
|
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
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
 
Old May 13th, 2011, 01:46 AM
Registered User
 
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Chapter 4 - Summary Excercises

Hey all!

Got a quick question and a sneeky neenjuh feeling that I already have the answer.

For the excercise:

Quote:
2. Write an application that includes the logic from Exercise 1, obtains two numbers from the user, and displays them, but rejects any input where both numbers are greater than 10 and asks for two new numbers.
Now, I did this. But when I compared my code to the Appendix code - I was all .... yea, I thunk I didded it wrong... Funny part? My code works.

So, answering my own question, there are different ways to code - just some is more efficient?

Their Code (example 2):

Code:
bool numbersOK = false;
double var1, var2;
var1 = 0;
var2 = 0;
while (!numbersOK)
{
Console.WriteLine("Give me a number:");
var1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Give me another number:");
var2 = Convert.ToDouble(Console.ReadLine());
if ((var1 > 10) && (var2 > 10))
{
Console.WriteLine("Only one number may be greater than 10.");
}
else
{
numbersOK = true;
}
}
Console.WriteLine("You entered {0} and {1}.", var1, var2);
My Code:

Code:
double firstNum, secNum;
            Console.WriteLine("This program decides which one of 2 numbers is greater than 10.");
            Console.WriteLine();
            Console.WriteLine("What is your first number?");
            firstNum = Convert.ToDouble(Console.ReadLine()); //defines firstNum
            Console.WriteLine("What is your second number?");
            secNum = Convert.ToDouble(Console.ReadLine()); //defines secNum
            if ((firstNum > 10) && (secNum > 10))  //evaluates both numbers to be greater than
            {
                Console.WriteLine();
                Console.WriteLine("Both of your numbers are greater than 10.  Please try again.");
            }
            else if ((firstNum > 10) && (secNum < 10))  //evaluates firstNum to be greater
            {
                Console.WriteLine();
                Console.WriteLine("Your first number {0} is greater than 10 and your second number {1} is less than 10.", firstNum, secNum);
            }
            else if ((firstNum < 10) && (secNum > 10))  // evaluates secNum to be greater
            {
                Console.WriteLine();
                Console.WriteLine("Your first number {0} is less than 10 and your second number {1} is greater.", firstNum, secNum);
            }
            else  // just in case both numbers are less than 10
            {
                Console.WriteLine("Both of your numbers are less than 10!");
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
Obviously I could've used bool and some other techniques now seeing their code. I guess the shortcuts come with time? Or, did I miss an important lesson while taking notes?
 
Old May 23rd, 2011, 07:11 AM
Registered User
 
Join Date: Apr 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

soopahNerd

There are always more than one way to write code that will perform a task. Efficiency/brevity comes with looking at how more experienced coders achieve the same sort of outcome and using their techniques in your code.

In general, if you find yourself using a bunch of sequential "if else" statements you should be using a loop and/or a switch statement instead.

From a functional viewpoint, the main difference I see between your code and the text book code is that yours will terminate after each execution whereas the text book one keeps looping until the user quits. Nothing wrong with termination if that's what you want to do at that point.

C/
 
Old May 23rd, 2011, 10:13 AM
Registered User
 
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Actually forgot I posted this...

I mean, don't get me wrong and I'm appreciative you responded. But I guess the short answer is "Experience"? I mean, all I know about C# was thru Chapter 4 when I wrote that.

My code may not have been ideal, and sure I coulda copied or downloaded the answer....but for a guy to sit down and write his own code from scratch without help - and it function?

Yea, using the loops and whatnot comes from "Experience".

Even Super Man had to learn how to fly :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# - Answers to Excercises Christopher BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 5 November 4th, 2009 04:34 PM
Why are there no formal excercises in this book? misom99 BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 October 3rd, 2007 09:44 AM
validation summary MunishBhatia ASP.NET 2.0 Professional 1 May 11th, 2007 08:00 AM
Summary in footer Blueman137 ASP.NET 1.0 and 1.1 Basics 1 March 30th, 2004 06:48 PM





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