Monday, February 8, 2016

Variables, Constants, and Arithmetic

Declaring Variables

Declaring variables in C# is very much similar to how you would do it in Java. For today's post, we will create a new project called Variables.

To begin, let's declare a couple int variables, x and y:


This is easy, right? As you would probably imagine, you can initialize these variables when you declare them (even on the same line), or after declaration, as shown below:


All of these declarations are legal, and can be used by preference for a developer. 

The calculations for these variables shown above, as you would expect, will result in 7 and 10 for x, s, q, and y, t, w respectively. This is straight forward, and we can validate these with our print statement: Console.WriteLine



Different Types of Variables

Now, obviously, int is not the only variable type that you can declare. Using the same syntax, you can declare other variables of the following types : (Source: https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx)

Defining Constants

Constants are declared in a very similar manner, we just have to use some more code to get there. By more code, I really mean one word: const. We define a constant by adding this before a variable declaration to make our value constant, like so:


Once again, easy. Not a whole lot to it as constant declaration is just as quick as variable declaration.

Getting Into It -- Distance Formula

Now this is all nothing all that new to someone with relative experience. So let's dive in!

Let's write a quick program to calculate the distance between two points in a plane. For example purposes, let's calculate the distance between (3,6) and (7,-3). 
  1. We will need to declare 4 int variables named x1, y1, x2, y2, and initialize them to our points.
  2. Then, we will declare a double variable named distance.
  3. To get the value of our distance, we set distance equal to the distance formula. To do this, we will need the help of the Math. functions built in to Visual Studio. We will use Math.Sqrt(number) to obtain a square root, and Math.Pow(number, exponent).
  4. After writing our formula for distance, we will print our value with Console.WriteLine(distance);
    Console.ReadKey();

    IMPORTANT: It is worth noting that the Console.ReadKey(); is necessary to keep our console open to validate we have the correct answer. Without it or Console.ReadLine(); Visual Studio will kill the program upon completion.
  5. The code, and output of our program is shown below:


Quadratic Formula

We will follow the same practices in order to calculate the quadratic formula, to find a value for x. The quadratic formula can be represented by the following equation:
Where the equation is solving for x, we will refer to the following original form to solve for x:


Now to write the code, we will do something similar to our distance formula.
  1. Let a = 2, b = 8, and c = 4.
  2. Let xPlus be equal to the solution of the quadratic formula when we are adding on top, and xMinus for when we are subtracting.We will once again need the assistance of the Math. functions in Visual Studio.
  3. After the calculation, we will print our result. The code, and output is shown below.

This time, we enlist the help of the Math.Round() function to round our answer to 2 decimal places, making the output look cleaner.



So you can see, you can do anything with C# similar to how we have in the past using Java, or Python. It is a matter of declaring the proper variables with the correct methods to obtain what we want. At least that is the case so far into our exploration of C#.

2 comments:

  1. This language has some cool math functions to allow you to compute different mathematical problems. What other type of math functions does this language offer?

    ReplyDelete
    Replies
    1. C# offers many math functions that you would expect for basic arithmetic, as well as built in capabilities in the "Math." features. These include anything from absolute value, to sin/cos/tan computation, min/max finding, remainder, logarithms, hyperbolic trig functions, and more! The complete list of all functions that "Math." covers can be found here:

      https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx

      Delete