CSCI 1301 – Lab 05

Clément Aubert

August 30, 2018

Reading From the User

  1. Download the PersonalizedWelcomeMessage project.
  2. Extract it, and open it in VS.
  3. Compile and execute it.
  4. You will be prompted with the message

    Please, enter your first name, followed by “Enter”:

    Enter your first name, followed by Enter ↵. You just witnessed an interaction between a program and an user!

  5. Read the source code, and make sure you understand all of it.
  6. Change the code, so that the program would also ask the user’s last name, and print both their first and last name.

Numeric Datatypes

For this part, I recommend opening the web page, printable version or editable version of the document we just studied in class, if you don’t have its physical version. Note that it contains numerous references at its end.

Experimenting

Compute in your head the result of the following operation: 1000000.0 + 1.2 − 1000000.0.

Now, implement it (read as “Create a new project and copy that code in the Main method”) using float, double, and decimal:

Can you explain what you just observed?

Now, execute the following code:

Can you explain the gradual loss of precision?

Making Simple Calculations

This part should be first carried out without using VS.

Assume we have the following statements:

Answer the following:

Operation Legal? Result Datatype
a + d Yes 19.7 double
m + f No _ _
a / b _ _ _
b * f _ _ _
d + f _ _ _
d + b _ _ _
a + m _ _ _
f / m _ _ _
d * m _ _ _

You can check your answers using VS: create a new project, copy the variables declarations and assignments, and write your own statements to perform the calculations in the Main method. For instance, if you want to check that the result of a + d is of type double, write something like

Cast Operator

Create a new project, and then do the following.

  1. Add in your program the following:

    You will get an error that reads

    Can you explain it?

  2. VS is suggesting that we use a “cast” to “force” C# to store the value of the variable floatVar into the variable intVar. To do so, replace the previous statement with the following:

  3. Using a Console.WriteLine statement, observe the value stored in intVar. Can you tell if the value stored in floatVar was rounded or truncated before being stored in the variable intVar? Conduct further experiments if needed to answer this question.