CSCI 1301 - Lab 14

Clément Aubert

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.

Part I – Basic Conditional Statements

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:

  1. Ask the user for an integer, and display on the screen “You were born after me” if the number is strictly greater than your year of birth.
  2. Ask the user for an integer, and display on the screen “Between −1 and 1” if the number is greater than or equal to −1 and less than or equal to 1.
  3. Ask the user for an integer, and display on the screen “Not between −1 and 1” if the number is greater than 1 or less than −1.
  4. Ask the user for an integer, and display on the screen “Odd” or “Even”, depending if the number is odd or even.
  5. Ask the user for an integer, and display on the screen “Negative” if the integer is negative, “Positive” if the integer in positive, and nothing if the integer is 0.
  6. Ask the user for an integer, and display on the screen “positive and odd” if the number is positive and odd, “positive and even” if the number is positive and even, “negative and odd” if the number is negative and odd, “negative and even” if the number is negative and even, and “You picked 0” if the number is 0.

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.

Part II - Char and Int Conversion, Ordering of Characters

Reading and Understanding

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.

Converting

Copy the following snippet of code in a Main method:

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?

Comparing

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:

Testing for Equality

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

Part III - Problem Solving

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:

Part IV – Pushing Further (Optionnal)

This lab’s pushing further suggests to take some advance in two topics we will be covering soon: for loops and string comparison

  1. Try to understand what the following code does:
for (int i = 32; i <= 126; i++)
    Console.Write((char)i);

Compile it, execute it, understand what its purpose is, and what its structure is.

  1. Comparing strings cannot be done with > 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?