Beginning Visual C# 2005
Exercise Answers
Chapter 1: Introducing C#
No Exercises.
Chapter 2: Writing a C# Program
No Exercises.
Chapter 3: Variables and Expressions
Exercise 1
Question: In the following code, how would we refer to the name great from code in
the namespace fabulous?
namespace fabulous
{
// code in fabulous namespace
}
namespace super
{
namespace smashing
{
// great name defined
}
}
Answer:
super.smashing.great
Exercise 2
Question: Which of the following is not a legal variable name:
a) myVariableIsGood
b) 99Flake
c) _floor
d) time2GetJiggyWidIt
e) wrox.com
Answer: b — because it starts with a number
and
e — because it contains a full stop
Exercise 3
Question: Is the string "supercalifragilisticexpialidocious" too big to fit in
a string variable? Why?
Answer: No, there is no theoretical limit to the size of a string that may be contained in a
string variable.
Exercise 4
Question: By considering operator precedence, list the steps involved in the computation
of the following expression:
resultVar += var1 * var2 + var3 << var4 / var5;
Answer: The * and / operators have the highest precedence here, followed by +, <<, and
finally +=. The precedence in the exercise can be illustrated using parentheses as follows:
resultVar += (((var1 * var2) + var3) << (var4 / var5));
Exercise 5
Question: Write a console application that obtains four int values from the user and
displays their product.
Answer:
static void Main(string[] args)
{
int firstNumber, secondNumber, thirdNumber, fourthNumber;
Console.WriteLine("Give me a number:");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Give me another number:");
secondNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Give me another number:");
thirdNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Give me another number:");
fourthNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The product of {0}, {1}, {2}, and {3} is
{4}.",
firstNumber, secondNumber, thirdNumber,
fourthNumber,
firstNumber * secondNumber * thirdNumber *
fourthNumber);
}
Note that Convert.ToInt32() is used here, which isn't covered in the chapter.
Chapter 4: Flow Control
Exercise 1
Question: If you have two integers stored in variables var1 and var2, what Boolean test
can you perform to see if one or the other of them (but not both) is greater than 10?
Answer:
(var1 > 10) ^ (var2 > 10)
Exercise 2
Question: 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.
Answer:
static void Main(string[] args)
{
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))
{
numbersOK = true;
}
else
{
if ((var1 <= 10) && (var2 <= 10))
{
numbersOK = true;
}
else
{
Console.WriteLine("Only one number may be greater than
10.");
}
}
}
Console.WriteLine("You entered {0} and {1}.", var1, var2);
}
Note that this can be performed better using different logic, for example:
static void Main(string[] args)
{
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);
}
Exercise 3
Question: What is wrong with the following code?
int i;
for (i = 1; i <= 10; i++)
{
if ((i % 2) = 0)
continue;
Console.WriteLine(i);
}
Answer: The code should read:
int i;
for (i = 1; i <= 10; i++)
{
if ((i % 2) == 0)
continue;
Console.WriteLine(i);
}
Using the = assignment operator instead of the Boolean == operator is a very common
mistake.
Exercise 4
Question: Modify the Mandelbrot set application to request image limits from the user
and display the chosen section of the image. The current code outputs as many characters
as will fit on a single line of a console application. Consider making every image chosen
fit in the same amount of space to maximize the viewable area.
Answer:
static void Main(string[] args)
{
double realCoord, imagCoord;
double realMax = 1.77;
double realMin = -0.6;
double imagMax = -1.2;
double imagMin = 1.2;
double realStep;
double imagStep;
double realTemp, imagTemp, realTemp2, arg;
int iterations;
while (true)
{
realStep = (realMax - realMin) / 79;
imagStep = (imagMax - imagMin) / 48;
for (imagCoord = imagMin; imagCoord >= imagMax;
imagCoord += imagStep)
{
for (realCoord = realMin; realCoord <= realMax;
realCoord += realStep)
{
iterations = 0;
realTemp = realCoord;
imagTemp = imagCoord;
arg = (realCoord * realCoord) + (imagCoord *
imagCoord);
while ((arg < 4) && (iterations < 40))
{
realTemp2 = (realTemp * realTemp) - (imagTemp *
imagTemp)
- realCoord;
imagTemp = (2 * realTemp * imagTemp) - imagCoord;
realTemp = realTemp2;
arg = (realTemp * realTemp) + (imagTemp *
imagTemp);
iterations += 1;
}
switch (iterations % 4)
{
case 0:
Console.Write(".");