Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual 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 August 18th, 2003, 08:20 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default Chapter 5 Exercises

1) b & d can be implicitly converted, a can't short isn't large enough, c not sure.
2) if there are less than 256 colours, then you could base the enum on byte
3)
Code:
    struct imagNum
    {
        public double real,imag;
    }
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            imagNum coord, temp;
            double realTemp2, arg;
            double realMin=-0.6,realMax=1.77,imagMin=-1.2,imagMax=1.2;        //limits
            double realInc=0.03,imagInc=0.05;
            //work out inc's
            imagInc=(imagMax-imagMin)/48;
            realInc=(realMax-realMin)/78;  //uh why not 79??

            int iterations;
            for (coord.imag=imagMax;coord.imag>=imagMin;coord.imag-=imagInc)
            {
                for (coord.real=realMin; coord.real<=realMax;coord.real+=realInc)
                {
                    iterations=0;
                    temp=coord;
                    arg=(coord.real*coord.real)+(coord.imag*coord.imag);
                    while((arg<4)&&(iterations<40))
                    {
                        realTemp2=(temp.real*temp.real)-(temp.imag*temp.imag)-coord.real;
                        temp.imag=(2*temp.real*temp.imag)-coord.imag ;
                        temp.real=realTemp2;
                        arg=(temp.real*temp.real)+(temp.imag*temp.imag);
                        iterations+=1;
                    }
...
though I can't see why you'd do it this way, only one bit of code as gotten simpler!
4) No quotes around the string.
 
Old August 18th, 2003, 09:04 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default

5)
Code:
            string inString, outString="";
            Console.WriteLine("Please enter a string:");
            inString=Console.ReadLine();
            char[] inChars=inString.ToCharArray();
            int i=inChars.Length-1;
            while (i>=0)
            {
                outString=outString + inChars[i];
                i--;
            }
            Console.WriteLine("'{0}' reversed is '{1}'",inString,outString);

I tried for ages to do it as a for loop but couldn't
Whats wrong with:
Code:
for(int i=inChars.Length-1;i=0;i--)
 
Old August 18th, 2003, 09:12 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default

6)
Code:
string inString,outString;
Console.WriteLine("Please enter a string:");
inString=Console.ReadLine().ToLower();
outString=inString.Replace("no","yes");
Console.WriteLine(outString);
Don't like it though, can I make the replace bit case insensitve?


 
Old August 18th, 2003, 09:23 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default

7)
Code:
string inString, outString="";
Console.WriteLine("Please enter a string:");
inString=Console.ReadLine();
string[] words;
char[] sep={' '};
words=inString.Split(sep);
foreach (string word in words)
{
  outString=outString + "\"" + word + "\"";
}
Console.WriteLine("New String: {0}",outString);
Ok that works but a few q's
1) why do I have to put outString="" in the decleration? in bombs if i don't...
2) do I have to do the Split exactly like that? I tried just .Split(' ') but it didn't like that...seems long winded.

 
Old August 20th, 2003, 11:25 PM
Registered User
 
Join Date: Aug 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1)on C bool and string can never be implicitly converted
5)

         string inString, outString="";
            int i;
            Console.WriteLine("Please enter a string:");
            inString = Console.ReadLine();
            char[] inChars = inString.ToCharArray();
            for (i = inChars.Length - 1; i >= 0; i--)
            {
                outString=outString + inChars[i];
            }
            Console.WriteLine("'{0}' reversed is '{1}'", inString, outString);

That is if you wanted to do it in a for loop, I believe that works.

7)

         string inString, outString="";
            Console.WriteLine("Please enter a string:");
            inString = Console.ReadLine();
            string[] words;
            words = inString.Split();
            foreach (string word in words)
            {
                outString = outString + "\"" + word + "\" ";
            }
            Console.WriteLine("Everything with quotes: {0}", outString);

I think this works too, and slightly shorter.

Everything else looks great. Thanks for posting this. Because for some reason when I tried to do these questions the day after I read it, I was drawing a blank. Your answer 5 really helped jog my memory ;)

From what my friends tell me the reason you need outString="". Is that c# likes it if you can define your variables, so that you can manipulate it even if it is a blank space. Before in languages like VB you didn't have to, and it cause alot of crashing problems. Btw this is what someone told me off hand so it might not be 100% right.
 
Old August 21st, 2003, 03:33 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default

I knew I should be able to do it in a for loop! thx!

Yea after posting and reading more I found the reason for the need of outString="".

The string outString, actually does pretty much naff all! and more importantly it doesn't allocate any memory for outString, so by putting "" into outString all the memory allocation stuff is done. Which half explains it, but why does this have to be done before the loop, why can't it be done on the first loop? maybe sommit to do with the compiler optimzations...

 
Old October 17th, 2003, 11:16 AM
Registered User
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

5) neatest solution is:

Console.Write("Please enter string : ");
string userstring=Console.ReadLine();
Console.Write("String backwards is : ");
for (int i=userstring.Length;i>0;i--) //loop back through string
{Console.Write(userstring[i-1]);} //and write each letter


 
Old October 17th, 2003, 11:22 AM
Authorized User
 
Join Date: Aug 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Chris Beach Send a message via AIM to Chris Beach Send a message via MSN to Chris Beach Send a message via Yahoo to Chris Beach
Default

mmm maybe, but you haven't actually reversed the string, so depending on your interpretation of the question its either right or wrong:)




 
Old October 22nd, 2003, 07:54 AM
Registered User
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

5)
I agree the wording implied writing a full string rather than characters, so here is amended version:

Console.Write("Please enter string : ");
string userstring=Console.ReadLine();
string backwards="";
for (int i=userstring.Length;i>0;i--)
{backwards+=userstring[i-1];}
Console.Writeline("String backwards is : " + backwards);








Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM





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