---
title: CSCI 1301 -- Lab 05
author: Clément Aubert
lang: en
fontsize: 10pt
documentclass: scrartcl
numbersections: true
links-as-notes: true
papersize: letter
geometry: margin=1in
keywords:
- Computer Science
- Problem solving methods
- Syllabus
- Augusta University
- Bachelor of Science
---

# Reading From the User

#. Download the [PersonalizedWelcomeMessage](PersonalizedWelcomeMessage.zip) project.
#. Extract it, and open it in VS.
#. (If you are using VS on MAC, follow the instructions at <https://stackoverflow.com/a/49056993/> to have your project "Run on external console". You will need to do that for every solution where the user is supposed to enter values.)
#. Compile and execute it.
#. You will be prompted with the message

        Please, enter your first name, followed by "Enter":
    
    Enter your first name, followed by <kbd>Enter ↵</kbd>.
    You just witnessed an interaction between a program and the user!

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

# Numeric Datatypes

For this part, I recommend opening the [web page](../../shared/datatypes/), [printable version](../../shared/datatypes/index.pdf) or [editable version](../../shared/datatypes/index.docx) of the document I mentionned 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 write in the `Main` method the code below, to display at the screen the result of this computation, as computed by C#") using `float`, `double`, and `decimal`:

~~~~~~~{.cs}
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);
~~~~~~~

Compile and execute your program.
Can you explain what you just observed?

Now, execute the following code:

~~~~~~~{.cs}
decimal decVar = 12344321.4999999991M;
double douVar = (double)decVar;
float floVar = (float)douVar;
Console.WriteLine($"With decimal: {decVar} \nWith double: {douVar} \nWith float: {floVar}");
~~~~~~~~

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:

~~~~~~~{.cs}
int a = 21, b = 4;
float f = 2.5000000f;
double d = -1.3;
decimal m = 2.5m;
~~~~~~~
 
Answer the following: 

- How many variables are declared?
- What are their datatype?
- What are their values?
- What are their names?
- Consider the following expressions. For each of them, tell if they are legal, and if so, give the result and its corresponding datatype.  The first two are given as examples.


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

~~~~~~~{.cs}
double tempVariable1 = a + d;
Console.WriteLine($"The value of d+f is {tempVariable1}");
int tempVariable2 = a + d; // This line should give you an error.
~~~~~~~

<!-- 
There have to be a better way!
https://stackoverflow.com/questions/11634079/how-can-i-get-the-data-type-of-a-variable-in-c
Check the actual type!
-->
