MTA Microsoft Technical Associate (Exam 98-361) Core Programming
Index
- Core Programming
- Computer Storage
- Data Structures
- Algorithms
- Decision Making
- Repetition
1 Computer Storage
The way computers storage the information internally is well known by all of you probably. The word binary pops in your mind probably at this stage. The origin of this word comes just because of there is an electricity signal or not (one or zero). With big chains of this
binary data we are able to represent the different characters and symbols that we use when speaking. These chains of zeros and ones are saved in the dynamic memory of our computer (Random Access Memory RAM) because that is the place where the processor is able to execute the code of any application. Our processor is not able to execute the code against the hard disc for instance, which is a long term storage.
This does not mean the developers create their apps just writing ones and zeros. They use to work with, what we call: compilers. The developer use particular instructions and syntax (programming languages: ruby, c++, java,...) depending of the compiler they are using, and this is responsible of transforms those instructions into binary data.
Computers deal with memory addresses where they save some information relevant for them to work properly. In order to write code successfully, first you need to understand some keywords: variable, constant and data types.
Variables: is a container which has a memory connection. It has a name and help us to access some information saved in memory in an easy way. It can be used to save, edit and retrieve values. Here is an example:
string CustomerName = "Roberto";
Constant: is similar to a variable but we need to assign his value when the constant is created. During program execution we are not able to change his value. There are two different types: constants and literal constants (like letter 'c' or the number '1', they never change, one will always be one). Constants are useful to save values that for example, you need to use several times in your code and also to understand better the code in the application. Example of constant in C#:
const int MondthsInYear = 12;
Data types: are part of every programming language. They help us to say to our application which kind of value we want to store in our variable. If the value is a number (int, float), a date (datetime) or just text (string) we will chose one data type or another. This data types help our computer to find out how to work with each of the different variables in an application, some examples:
int myAge = 30; // Age is a number, we use integer data type.
string myAge = "30"; // Age saved as text.
string myName = "Mike"; // The name is text so we use string as data type.
double myHeight = 1,86; // Height is decimal so we use double instead of integer.
2 Data Structures
Here are listed some of the main data structures used in most typical programming languages. I have enclosed some examples in c# with the "OUTPUT" you will get in case of you decide to execute the code.
Arrays: they are designed to store different values but all the same data type. Arrays are collections of objects... but always the same type (numbers, text,...). Arrays is a kind of sheet which contains two columns: an Index and a Value. The Index always starts with zero. So if we want to access the first value in the Array we need to point to the position zero instead of position one. Arrays look like this:
// Array sample. We've created an array of datatype = strings
string[] myArray = new string[] { "Item1", "Item2", "Item3", "Item4" };
Console.WriteLine("Item at index 2 is: " + myArray[2]);
// OUTPUT => Item at index 2 is: Item3
Console.WriteLine("This is the contents of the array.");
foreach (string value in myArray)
{
Console.WriteLine(value);
}
// OUTPUT:
// This is the contents of the array.
// Item1
// Item2
// Item3
// Item4
Stacks: are a
LIFO (Last Input First Output) collection. Compared with a stack of paper on a desk, it provides a powerful and simple data structure. You "put" papers on the top of the stack (push) and "take" them off of the top (pop). Remember you cannot go directly to the first element in the stack, you need to pop all the elements before:
It's also the way a computer works, it has a list of instructions and when we need to execute a particular command, we push that command on the top of the stack and then we also push in the stack all the information (data, variables) that the command needs to be executed. This way, when our processor gets that command, all the information and variables were being collected already.
// Stack sample
Stack myStack = new Stack();
myStack.Push("Item1");
myStack.Push("Item2");
myStack.Push("Item3");
myStack.Push("Item4");
myStack.Push("Item5");
myStack.Push("Item6");
Console.WriteLine("Initial stack count: " + myStack.Count);
// OUTPUT:
//Initial stack count: 6
while (myStack.Count > 0)
{
Object item = myStack.Pop();
Console.WriteLine("Popped" + item.ToString() + " off the stack");
Console.WriteLine("Stack count: " + myStack.Count);
}
// OUTPUT:
// PoppedItem6 off the stack
// Stack count: 5
// PoppedItem5 off the stack
// Stack count: 4
// PoppedItem4 off the stack
// Stack count: 3
// PoppedItem3 off the stack
// Stack count: 2
// PoppedItem2 off the stack
// Stack count: 1
// PoppedItem1 off the stack
// Stack count: 0
Queues: a collection of objects, accessed by queuing and dequeuing. Similar to a line at the motor vehicle license branch. If you think about it, is the same as a stack but instead of LIFO it's
FIFO (First input First Output), example in C#:
// Queue sample
Queue myQueue = new Queue();
myQueue.Enqueue("Item1");
myQueue.Enqueue("Item2");
myQueue.Enqueue("Item3");
myQueue.Enqueue("Item4");
myQueue.Enqueue("Item5");
myQueue.Enqueue("Item6");
myQueue.Enqueue("Item7");
myQueue.Enqueue("Item8");
// Gets the first item
Console.WriteLine(myQueue.Peek());
// OUTPUT:
// Item1
Console.WriteLine(myQueue.Count);
// OUTPUT:
// 8
Object qValue = myQueue.Dequeue();
Console.WriteLine(qValue.ToString());
Console.WriteLine("Queue count is: " + myQueue.Count);
Console.WriteLine(myQueue.Peek());
// OUTPUT:
// Item1
// Queue count is: 7
// Item2
Dictionaries: a collection of objects that are accessed by using a key. Is pretty similar to an Array with the only difference that the values are accessed by a Key instead of an Index like you use to work when use a normal dictionary. Example:
// Dictionary sample
Dictionary myDictionary = new Dictionary();
myDictionary.Add("key1", "First Item");
myDictionary.Add("key2", "Second Item");
myDictionary.Add("key3", "Third Item");
myDictionary.Add("key4", "Fourth Item");
Console.WriteLine("Item represented by the key 'key3': " + myDictionary["key3"]);
// OUTPUT:
// Item represented by the key 'key3': Third Item
myDictionary.Remove("key4");
3 Algorithms
Consider an algorithm as a solution to a problem. To make it easier I want to show you an example from real life. Imagine when you wake up in the morning and prepare your breakfast... Pancakes yeah!
What we have here is all the steps you need to complete your pancakes showed throw a
flow chart diagram (this kind of graphs are really useful to explain how your algorithms work): add eggs, some flour, add water and mix. Then you check the consistency and if it's too thick then you put more water and mix and if not, you need to put more flour, then water and mix again. If then the consistency is ok, you fried your pancakes, serve and eat. Finished.
That is the way a developer thinks when he is making a algorithm in one application. Of course, the solutions a developer wants to achieve with his algorithms are not like this example. A typical algorithm for instance is the
Bubble Sort, which tries to put in order a chain of numbers by going two by two putting always the smaller in from of the chain and starting from the beginning until it reaches the end, here is the flow chart:
4 Decision Making
To be able to follow the previous flow charts we will need decision structures. This pieces of code help the developer to move the execution to the lines they are interested in. We have several decision structures but by now let's focus on these ones:
- if: this statement evaluates a condition and if it's true, then the execution goes into the if statement.
// if sample
int condition1 = 1;
if (condition1 == 1)
{
Console.WriteLine("The condition is true!");
}
Console.WriteLine("This executes after the if, regardless the condition.");
//OUTPUT:
// The condition is true!
// This executes after the if, regardless the condition.
- if / else: similar to the previous example with the only difference when the condition is false then the "else" statement is executed and we don't need to take care about the first bunch of instructions. In this example the condition is always true because the value of condition1 is one, but if you change that value by 3 then the condition will be false and the second bunch of instructions will be executed inside the else {}
// if-else sample
int condition1 = 1;
if (condition1 == 1)
{
Console.WriteLine("condition TRUE");
}
else
{
Console.WriteLine("condition FALSE");
}
- if / else if / else: grouping the two previous examples we get to if/else/if. You can join as much if/else as you want. In this example, none of the conditions are true so the third bunch of instructions inside of the last "else" will be executed ("Neither condition is true.").
// if-else-if sample
int condition1 = 1;
int condition2 = 2;
if (condition1 == 2)
{
Console.WriteLine("condition1 is TRUE");
}
else if (condition2 == 1)
{
Console.WriteLine("condition1 is TRUE");
}
else
{
Console.WriteLine("Neither condition is true.");
}
- switch or select case: imagine you have to compare a variable with a lot of different numbers, not just two or three. Instead of create a bunch of if/else/if statements is a good practice use the select case statement. Here below I enclose an example. Take a look at the break reserved word, this is used to not continue evaluating the variable with the rest of condition, when it's true pop out the select case. Besides there is an optional (you can include it or not) parameter at the end called "default", this is executed just when none of the previous conditions are not true and in our example tries to print the real value of the variable:
// switch sample
int switchCondition = 3;
switch (switchCondition)
{
case 1:
Console.WriteLine("Value is 1");
break;
case 2:
Console.WriteLine("Value is 2");
break;
case 3:
Console.WriteLine("Value is 3");
break;
case 4:
Console.WriteLine("Value is 4");
break;
default:
Console.WriteLine("Value is " + switchCondition);
break;
}
5 Repetition
This is a point where the computers are really good, executing something one time and another... The bubble sort is something where we need to repeat or loop over one data structure to get the numbers sorted and when this happens finish and not continue looping.
Here are some examples:
- for loop: this repetition structure tries to run a certain lines of code while a condition is true. Whenever that conditions evaluates to false the loop is finished and the execution will pop out the loop. Look at the following example with his output:
// for loop sample
Console.WriteLine("for loop");
for (int forCounter = 0; forCounter < 4; forCounter++)
{
Console.WriteLine("forcounter is at " + forCounter);
}
// OUTPUT:
// forcounter is at 0
// forcounter is at 1
// forcounter is at 2
// forcounter is at 3
This statement initializes the forCounter variable to 0, then evaluates it in the forCounter < 4 and if it is true, the code line between the curly braces will be executed, then forCounter increases in one with the forCounter++ statement, at some point the evaluation of the forCounter variable will be false and the execution will pop out of our for loop, but imagine a loop where this condition is always true and the loop never finishes, this could be very funny in terms of coding when it happens to other developer hehehe.
- while: is similar to the for loop but with small structural differences. We need to initialize our counter before the loop starts and we also need to increase it inside of the loop, where the whileCounter++ appears. We just write the condition close to the "while" statement like in this example here:
// while sample
int whileCounter = 0;
while (whileCounter < 3)
{
Console.WriteLine("whileCounter is at " + whileCounter);
whileCounter++;
}
// OUTPUT
// whileCounter is at 0
// whileCounter is at 1
// whileCounter is at 2
- do while: pretty similar to our previous example for the while loop. Just check the following code where I've adapted the while loop into the do/while loop:
// do while sample
int doCounter = 0;
do
{
Console.WriteLine("doCounter is at " + doCounter);
doCounter++;
} while (doCounter < 3);
// OUTPUT
// doCounter is at 0
// doCounter is at 1
// doCounter is at 2
// doCounter is at 3
You probably already realised about the difference between while and do/while and it is just the time when the evaluation of the loop condition is made. That is why we got a different output between these two examples.
- recursion: this is a little bit more complicated but has the same root as our previous examples. Recursion tries to loop over a function for certain number of times. The point is that you need to design that function to be able to stop at some point because you will be calling that function in order to get a final result. For example let's create a function which calculates the factorial of a number. For those who are not familiar with this mathematical process, factorial tries to multiply a group of numbers just by telling him the top limit, this way, factorial of 4 will be 4! = 4 * 3 * 2 * 1 equals 24. See the example below with the code to achieve the factorial of a number:
// recursion sample
long value = Factorial(10);
Console.WriteLine(value);
static long Factorial(int n)
{
if (n == 0)
{
return 1;
}
return n * Factorial(n - 1);
}
We are trying to get the factorial of ten which is 3.628.800 by sending first 10 into our Factorial function, there, ten is compared to 0 and because is false we give back 10 multiplied by the call to the same function, but this time we will send to that function 9 instead of 10.
At this point we have this return:
10 * Factorial(9); waiting for the answer of the Factorial function. With that 9 we go again into the function, evaluate 9 and we give back that 9 and the answer from the Factorial but by 8 this time.
So if we concatenate these two first calls we will have:
return 10 * Factorial( 9 * Factorial (8) ) ... and so till we call Factorial(0), because that's when we will give back 1 but without calling our function again. At this point we are not assigning that 1 into our variable: value, because what we're going to do here is coming back throw all the different Factorial calls that we've made, something like this:
Factorial(10 * Factorial(9 * Factorial(8 ... * Factorial(2 * Factorial(1 * 1)
Which is nothing more and nothing less than: 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. I know this example could be more complicated but it turns really useful when you need to work with mathematical algorithms, you also can get a better understanding on how recursion works
debugging the example provided..
That's all folks! I hope you enjoy this post and don't hesitate to leave a comment, doubt or claim :)
Cheers!