CSCI 1301 - Lab 06

Clément Aubert

January 29, 2018

Deadlines: This lab needs to be completed before taking Lab 07.

Dependencies: Part I must be completed before Part II.

Part I - First Use of the Debugger

  1. Open one of your project. It can be any project, as long as it uses variables.
  2. Make sure you can compile and run it.
  3. Hit F11 once. Observe what happen. Move the black screen where the printing happen (called the console, or Command prompt) to see your code. Hit F11 a couple of times, and see what happen. You are executing your code line by line, and can track many useful information, including the value and datatype of your variables (in the “Locals” panel / window).
  4. You can find out at https://msdn.microsoft.com/en-us/library/y740d9d3.aspx (VS 2015) or https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger (VS 2017) alternative ways of starting the debugging functionnalities of VS.

Part II - Numeric Datatypes

For this part, I recommend opening the web page or the numeric version of the document we just studied in class. Note that it contains numerous references at its end.

Making Simple Calculations

This part should be carried out without using VS.

Assume we have the following statements:

  1. Answer the following:
  1. For each of the following operations, tell if they are legal, and if so, give the result and its corresponding datatype:

Cast Operator

  1. Start by checking your answers to the previous problem using VS. Create a new project, copy the code above in the Main method, and run the debugger to find out the answers to question a. For question b., write your own statements to perform the calculations. For instance, if you believe that the result of d + f is of type int, write something like
  1. Remember that we can’t assign a float litteral to an int variable. Check it for yourself by adding the following to your program:

You will get an error that reads

  1. VS is suggesting that we use a “cast” to “force” C# to store the value of the variable f into the variable a. To do so, replace the previous statement with the following:
  1. Using the debugger, observe the value stored in intVar. Can you tell if the value stored in f was rounded or truncated before being stored in the variable intVar? Conduct further experiments if needed to answer this question.

  2. Add the following in your code:

And observe, using the debugger or printing instructions, the gradual loss of precision.

Part III - Reading Other Numeric Datatypes

  1. Create a new project
  2. Declare three variables: one of type int, one of type float, and one of type double.
  3. You may remember that we can use the following to read an int from the user (assuming your int variable is named intVar):
intVar = int.Parse(Console.ReadLine());

Similarly, we can read float using float.Parse(Console.ReadLine()), and double using double.Parse(Console.ReadLine()).

  1. Write statements that ask the user to enter an int, a float and a double, store those values in the appropriate variables, and then print them.

Part IV (Optional) - Pushing Further

The following are two independent tasks, to widen your understanding of this class, and to prepare you for the next labs.

  1. Compute in your head the result of the following operation: 1000000.0 + 1.2 - 1000000.0. Now, implement it using float, double, and decimal:
Console.Write("With floats:\n\t");
Console.WriteLine(1000000.0f + 1.2f - 1000000.0f);
Console.Write("With double:\n\t");
Console.WriteLine(1000000.0 + 1.2 - 1000000.0);
Console.Write("With decimal:\n\t");
Console.WriteLine(1000000.0m + 1.2m - 1000000.0m);

Can you explain this behaviour?

  1. Use the .ToString() method introduced in the previous lab (Part III, b.) to convert the int, float and double variables of Part III into string, and store them in a string variable. Use concatenation to create a single string containing those three values, and print them with a single Console.WriteLine instruction.