March 6, 2018
Dependencies: The lab needs to be taken in the order presented below. Part III isn’t easy, make sure you decompose the problem in smaller, tractable problems, before trying to solve it.
Read all the instructions in this part before starting to type code. Create a new project, and write portions of code that perform the following:
For each of those questions, write on paper whenever you should use if
, if-else
, if-else-if
or nested conditional structures, and what the condition(s) should be. Once you feel confident, write the code in VS, and then test it intensively: enter all kind of values (positive and odd, negative and even, 0, and remember that 0 is even, etc.) and make sure that what is displayed on the screened is always correct.
Characters are represented by integers: cf. https://en.wikipedia.org/wiki/ASCII#Printable_characters for a mapping between the glyphs (i.e., space, !, etc.) and decimal values (to be read as “integer code”, i.e., 32, 33, 34, etc.). Note that the characters are divided in groups, and that there are 95 printable characters.
Copy the following snippet of code in a Main
method:
int intVar = (int)'C';
char charVar = (char)84;
Console.WriteLine($"'C' is represented as {intVar}\n"
+ $"{charVar} corresponds to the value 84");
And note that we can explicitely convert int
into char
, and char
into int
.
Actually, the conversion from char
to int
could be done implicitely by C#: replace the previous first line with
And note that your program would still compile. Can you also convert implicitely int
into char
?
Exactly as 65 is less than 97, the character associated to 65, A
, is less than the character associated with 97, a
. You can convince yourself by executing the following code:
Note that you can also test if a character is equal to an other by using ==
, as for integer values. This is particularly useful when we want to ask the user for a “yes” / “no” decision.
Write a snippet of code that
To read a single character (instead of a whole string), use
You are asked to write a simple program that compute the total price for a group of people to enter a park.
Your program should:
Some tips:
This lab’s pushing further suggests to take some advance in two topics we will be covering soon: for
loops and string
comparison
Compile it, execute it, understand what its purpose is, and what its structure is.
>
and <
operators. To compare them, we have to use the CompareOrdinal method of the String class. It works as follow:if (String.CompareOrdinal("A", "a") > 0)
{
Console.Write("A is greater than a");
}
else
{
Console.Write("A is less than a");
}
Note that CompareOrdinal
returns an integer, that we then compare with 0.
In the previous example, we tested string made of only one character, but we can compare arbitrarily complex strings:
if (String.CompareOrdinal("Augusta", "Auguste") > 0)
{
Console.Write("Augusta is greater than Auguste");
}
else
{
Console.Write("Augsta is less than Auguste");
}
To conclude with this topic, note that the integer returned actually has a precise value: examine the following code to understand it.
if (String.CompareOrdinal("A", "a") == ((int)'A' - (int)'a'))
Console.WriteLine("Ok, I get it now");
if (String.CompareOrdinal("Ab", "az") == (((int)'A' + (int)'b') - ((int)'a' + (int)'z')))
Console.WriteLine("Yes, I really do.");
else if (String.CompareOrdinal("Ab", "az") == ((int)'A' - (int)'a'))
Console.WriteLine("Or do I?");
if (String.CompareOrdinal("ABCDEf", "ABCDEF") == (int)'f' - (int)'F')
Console.WriteLine("Ok, now I'm good.");
Do you understand how the returning value is computed for these strings?