-
October 10th, 2011, 02:18 PM
#1
[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
-
October 10th, 2011, 03:23 PM
#2
Re: C# do-while? Necklace Problem
You're not splitting off the ones column
Always use [code][/code] tags when posting code.
-
October 10th, 2011, 03:40 PM
#3
Re: C# do-while? Necklace Problem
Oh your right. How exactly would I do that ?
-
October 10th, 2011, 04:32 PM
#4
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.");
}
}
}
-
October 10th, 2011, 04:46 PM
#5
Re: C# do-while? Necklace Problem
Originally Posted by you69
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
-
October 10th, 2011, 05:09 PM
#6
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.
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.
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.
in other words, it's like to say:
Code:
a = a + b;
b = b + a;
-
October 10th, 2011, 05:36 PM
#7
Re: C# do-while? Necklace Problem
Okay thanks . And one last question- What does " if (a >= 10) a %= 10; " do ?
-
October 10th, 2011, 05:53 PM
#8
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).
-
October 10th, 2011, 06:05 PM
#9
Re: C# do-while? Necklace Problem
Originally Posted by you69
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 ?
-
October 10th, 2011, 06:36 PM
#10
Re: C# do-while? Necklace Problem
Originally Posted by Nin9tySe7en
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.
-
October 10th, 2011, 06:52 PM
#11
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 ?
-
October 10th, 2011, 07:12 PM
#12
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.
-
October 10th, 2011, 07:20 PM
#13
Re: C# do-while? Necklace Problem
Originally Posted by Nin9tySe7en
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.
-
October 10th, 2011, 11:48 PM
#14
Re: C# do-while? Necklace Problem
Originally Posted by Nin9tySe7en
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|