CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2011
    Posts
    13

    [RESOLVED] C# do-while? Necklace Problem

    This problem begins with two single-digit numbers. The next number is obtained by adding the first two numbers together and saving only the ones-column-digit.This process is repeated until the “necklace” closes by returning to the original two numbers. For example, if the starting numbers are 1 and 8, twelve steps are required to close the “necklace”:
    1 8 9 7 6 3 9 2 1 3 4 7 1 8
    I need a program that will ask for the user two input 2 numbers ( firstNumber and secondNumber)
    Then the program will show the number of steps until the last 2 numbers are the same.
    For example:

    Enter the first number: 1
    Enter the second number: 8
    1 8 9 7 6 3 9 2 1 3 4 7 1 8
    Your number required 12 steps.

    This is what I have created...



    static void Main(string[] args)
    {
    double firstNumber;
    double secondNumber;
    double variableAnswerOne;
    double variableAnswerTwo;

    Console.Write("Enter the first number:");
    firstNumber = double.Parse(Console.ReadLine());

    Console.Write("\nEnter the second number:");
    secondNumber = double.Parse(Console.ReadLine());

    Console.WriteLine(firstNumber + secondNumber);
    variableAnswerOne = firstNumber + secondNumber;
    Console.WriteLine(secondNumber + variableAnswerOne);
    variableAnswerTwo = secondNumber + variableAnswerOne;

    do
    {
    variableAnswerOne = variableAnswerTwo + variableAnswerOne;
    Console.WriteLine(variableAnswerOne + variableAnswerTwo);
    variableAnswerTwo = variableAnswerOne + variableAnswerTwo;
    Console.WriteLine(variableAnswerTwo + variableAnswerOne);
    }
    while (variableAnswerOne != firstNumber && variableAnswerTwo != secondNumber);


    Console.ReadKey(true);

    }
    }
    }

    Im not sure if it works at the start, but I do know that it doesnt work completely because it goes into an "infinity" loop.

    HELP PLEASE

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: C# do-while? Necklace Problem

    You're not splitting off the ones column
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Oct 2011
    Posts
    13

    Re: C# do-while? Necklace Problem

    Oh your right. How exactly would I do that ?

  4. #4
    Join Date
    Oct 2011
    Posts
    3

    Re: C# do-while? Necklace Problem

    I was bored... I'm hope that this will help you.
    If you have questions don't hesitate to ask .

    Code:
        class Program
        {
            static void Main(string[] args)
            {
                int original_a = 0;
                int original_b = 0;
    
                try
                {
                    Console.Write("Enter the first number: ");
                    original_a = int.Parse(Console.ReadLine());
                    if (original_a < 0 || original_a >= 10) throw new Exception();
    
                    Console.Write("Enter the second number: ");
                    original_b = int.Parse(Console.ReadLine());
    
                    int a = original_a;
                    int b = original_b;
    
                    int steps = 0;
    
                    do
                    {
                        if (steps == 0) Console.Write(a + " " + b);
    
                        if (steps % 2 == 0)
                        {
                            a += b;
                            if (a >= 10) a %= 10;
    
                            Console.Write(" " + a);
                        }
                        else
                        {
                            b += a;
                            if (b >= 10) b %= 10;
    
                            Console.Write(" " + b);
                        }
    
                        steps++;
                    } while (a != original_a || b != original_b);
    
                    Console.WriteLine("");
                    Console.WriteLine("Your number required " + steps + " steps.");
                }
                catch (FormatException e)
                {
                    Console.WriteLine("Error: You have not entered a valid number.");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: You have not entered number between 0 to 9.");
                }
            }
        }

  5. #5
    Join Date
    Oct 2011
    Posts
    13

    Re: C# do-while? Necklace Problem

    Quote Originally Posted by you69 View Post
    Code:
        class Program
        {
            static void Main(string[] args)
            {
    //declares variables equal to 0 ? Why are they equal to 0 ? Shouldn't they be equal to whatever the user inputs?
                int original_a = 0;
                int original_b = 0;
    
    //what is "try" ?
                try
                {
                    Console.Write("Enter the first number: ");
                    original_a = int.Parse(Console.ReadLine());
    //What does this statement do? Especially the "throw new exception()" part
                    if (original_a < 0 || original_a >= 10) throw new Exception();
    
                    Console.Write("Enter the second number: ");
                    original_b = int.Parse(Console.ReadLine());
    
                    int a = original_a;
                    int b = original_b;
    
    //why 0 ? Again...
                    int steps = 0;
    
                    do
                    {
    
    //What goes inside the " " ? Furthermore, What exactly is this statement doing ?
                        if (steps == 0) Console.Write(a + " " + b);
    
    //why do we want to know if it's divisible by 2 ?
                        if (steps % 2 == 0)
                        {
    
    //a += b ???
                            a += b;
                            if (a >= 10) a %= 10;
    
    //whats with the empty " " again ?
                            Console.Write(" " + a);
                        }
                        else
                        {
    
    //b += a ???
                            b += a;
                            if (b >= 10) b %= 10;
    
                            Console.Write(" " + b);
                        }
    
                        steps++;
                    } while (a != original_a || b != original_b);
    
                    Console.WriteLine("");
                    Console.WriteLine("Your number required " + steps + " steps.");
                }
    
    //catch ?  (FormatException e) ?
                catch (FormatException e)
                {
                    Console.WriteLine("Error: You have not entered a valid number.");
                }
    //Exception e ?
                catch (Exception e)
                {
                    Console.WriteLine("Error: You have not entered number between 0 to 9.");
                }
            }
        }
    You said don't hesitate to ask questions... So I didn't. xD
    BTW. THANKS SO MUCH

  6. #6
    Join Date
    Oct 2011
    Posts
    3

    Re: C# do-while? Necklace Problem

    Code:
                int original_a = 0;
                int original_b = 0;
    that's what called to initialize a variable. you should to do that otherwise usually the variable will contain garbage. also it is a good idea to know which value the variable have in the begginig to avoid surprises.

    Code:
                try
                {
    try is part of try-catch statement. You can read more about this here.
    briefly explaining, when there is some kind of exception (in this program the exceptions mean invalid input, but it's can be anything) between the 'try' block it's throw an exception. what it's mean? it's mean that the program stops and goes to the suitable 'catch' block (if exists).

    Code:
    if (original_a < 0 || original_a >= 10) throw new Exception();
    this is if condition that tets if the original number is less than 0 or larger than 10 (because the input number should be between 0 to 9) and if it is it's throw an exception.

    Code:
    int steps = 0;
    initializing variable again (as i explained before).

    Code:
    if (steps == 0) Console.Write(a + " " + b);
    it's mean that we write in one line what we have in variable 'a' then white space and then what we have in variable 'b'.

    Code:
    if (steps % 2 == 0)
    here we check if the number of steps is even or odd; mod 2 of any number will return 0 if it's a even number or the remainde if it's a odd number. if it's even we should change 'a' variable if it's odd we should change 'b' variable.

    Code:
    a += b;
    b += a;
    in other words, it's like to say:

    Code:
    a = a + b;
    b = b + a;

  7. #7
    Join Date
    Oct 2011
    Posts
    13

    Re: C# do-while? Necklace Problem

    Okay thanks . And one last question- What does " if (a >= 10) a &#37;= 10; " do ?

  8. #8
    Join Date
    Oct 2011
    Posts
    3

    Re: C# do-while? Necklace Problem

    Code:
    if (a >= 10) a %= 10;
    it's means that if the new number (a + b) is larger than 10 we take only the right digit (Modulo 10).
    for example (assuming that the numbers are integers):
    14 / 10 = 1.
    14 % 10 = 4 ('4' is the remainder of 14 / 10).

  9. #9
    Join Date
    Oct 2011
    Posts
    13

    Re: C# do-while? Necklace Problem

    Quote Originally Posted by you69 View Post
    assuming that the numbers are integers
    this brings up another point. How would I make sure that the person is to type in a single-digit number/integer. There is nothing stopping them from typing in a decimal ?

  10. #10
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: C# do-while? Necklace Problem

    Quote Originally Posted by Nin9tySe7en View Post
    this brings up another point. How would I make sure that the person is to type in a single-digit number/integer. There is nothing stopping them from typing in a decimal ?
    Read up on Console.Read() here. Essentially, it reads the next character from the standard input stream. As opposed to Console.ReadLine(), which reads the "next line of characters" from the standard input stream.

    There are other ways of handling input, but in your case with digits 0-9, I'd suggest using Read().

    Unless, you are wanting to accept decimals, etc.

  11. #11
    Join Date
    Oct 2011
    Posts
    13

    Re: C# do-while? Necklace Problem

    Final Question.... most likely. xD
    I was studying the coding trying to make sense of everything so what I did was I re-wrote it all in my own style. I have come to understand everything except for the exceptions

    why did the does it need to be written like:

    catch (FormatException e)
    {
    }


    couldn't it be written like:


    catch (FormatException)
    {
    }


    Why is the "e" there ?

  12. #12
    Join Date
    Jan 2010
    Posts
    1,133

    Re: C# do-while? Necklace Problem

    Because you usually don't catch exceptions just so that you can pretend they never happened, but to handle them instead. Handling might involve correcting some variables (maybe the input was invalid) and resuming with the appropriate value if possible, or notifying the user about the error, and providing the appropriate info (because no user likes to see the app crash and throw a stack trace at him).
    When doing this, you might want to show the exception message, or check some properties of a specific exception, and the 'e' enables you to access those. The 'e' is just a variable name that stores an exception object of the given type.
    Exceptions are just objects like any other.

  13. #13
    Join Date
    Jan 2010
    Posts
    1,133

    Re: C# do-while? Necklace Problem

    Quote Originally Posted by Nin9tySe7en View Post
    Okay thanks . And one last question- What does " if (a >= 10) a %= 10; " do ?
    P.S. About that: don't be confused it's all written in one line - that can be re-written as:
    Code:
     if (a >= 10) 
        a %= 10;
    It's just a standard conditional, with single-statement body.

    It's usually written in that form - indentation, blank spaces and line breaks are just a matter of readability and coding style; C# doesn't care much about them. That is usually written in two lines, second line indented, as it's more readable and easier to understand, but when the body of the if-statement is so short, people sometimes do a one-liner.

  14. #14
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Resolved Re: C# do-while? Necklace Problem

    Quote Originally Posted by Nin9tySe7en View Post
    Final Question.... most likely. xD
    I was studying the coding trying to make sense of everything so what I did was I re-wrote it all in my own style. I have come to understand everything except for the exceptions

    why did the does it need to be written like:

    catch (FormatException e)
    {
    }


    couldn't it be written like:


    catch (FormatException)
    {
    }


    Why is the "e" there ?
    The neat part about Exceptions, are they help you locate the problems. Often times you may want to generate an error message, to display to the user of your applications. You have various higher level exceptions such as FormatException, but also lower ones, such as Exception. Regardless, e, allows you to display exception information.

    Code:
    MessageBox.Show("Error", e.Message);
    for example. You can also get access to things such as the stack. For more information on exceptions, visit http://msdn.microsoft.com/en-us/library/ms229005.aspx.

    Regards,
    Quinn

    If this helps, please rate up!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured