Beginning Visual C# Exercises - Chapter 05
1. Answer a - int does not convert implicitly to short [2004/08/13 - missed this on original posting]
Answer c - boolean (and string) data types do not convert implicitly
2. enum color : short
{
// Per Sir Isaac Newton's set of seven rainbow colors plus black and white
black,
red,
orange,
yellow,
green,
blue,
indigo,
violet,
white
}
Byte could replace the underlying data type in the above example.
However, if you were to fine tune the color gradient to include
more than 256 colors, then you would exceed the byte range
3. ....Add into existing code Ch04Ex06 after top of class
struct imagNum
{
public double real, imag;
}
.... insert (or rewrite) into existing code Ch04Ex06 after "int iterations;" line
// Maintain console window dimensions: 49 rows, 80 columns
// Lose one row (49 -1) and column (80 -1) as starting points
// Reduce imaginary input by 2.4 (48 rows * .05 step)
double imagConstraint = 2.4;
// Increase real input by 2.36 (79 columns * .03 step) - .01 [rounding adj]
double realConstraint = 2.36;
imagNum myStart;
Console.WriteLine("Enter a two-place decimal number between -2.00 and 2.00");
myStart.imag = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter a two-place decimal number between -2.00 and 2.00");
myStart.real = Convert.ToDouble(Console.ReadLine());
.... Replace (or rewrite) next two "for ..." lines
//for (imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)
for (imagCoord = myStart.imag; imagCoord >= myStart.imag - imagConstraint; imagCoord -= 0.05)
{
//for (realCoord = -0.6; realCoord <= 1.77; realCoord += 0.03)
for (realCoord = myStart.real; realCoord <= myStart.real + realConstraint; realCoord += 0.03)
{
4. No, there are two errors in the second line of code:
* An array of 5 elements may be accessed with an index of 0 through 4;
the supplied index of 5 will generate an "IndexOutOfRangeException"
* The assignment value requires quotes around it, so will not compile
5. Console.WriteLine("Enter a phrase to turn around:");
string thePhrase = Console.ReadLine();
char[] thework = thePhrase.ToCharArray();
thePhrase = "";
for (int i = thework.Length -1; i >= 0; i--)
thePhrase += thework[i];
Console.WriteLine("Here is your response in reverse order:");
Console.WriteLine(thePhrase);
6. Console.WriteLine("Answer my imaginary question with a NO included:");
string thePhrase = Console.ReadLine();
// Handle 3 possible "no" cases
thePhrase = thePhrase.Replace("no", "yes");
thePhrase = thePhrase.Replace("No", "Yes");
thePhrase = thePhrase.Replace("NO", "YES");
Console.WriteLine("Here is the answer you meant to write:");
Console.WriteLine(thePhrase);
7. Console.WriteLine("What has been the best occurrence today?");
string thePhrase = Console.ReadLine();
Console.WriteLine("Let me quote each word you stated:");
Console.WriteLine("\"" + thePhrase.Replace(" ", "\" \"") + "\"");
|