Thursday, March 17, 2016

Binary Search

Using a binary search within arrays is quite simple with C#. There was some brilliant person on the creation team for this language that decided to provide a method to accomplish this task, all built in to the generic C# commands. This is a built in function that we can call at anytime, just as we can call the "Math." functions.

We will begin our example using a string list array that will be called group which will do as we expect, hold strings. We will use this to store the names of people who are on a group project together. Sometimes people may need to be added or removed, 'tis the way it goes. We will fill the list with values to begin, and immediately sort the list to improve performance:


Now we will write code to search for Jojo. The code to produce this will use group.BinarySearch to find Jojo, and if she is not in the group, then she will be added at the correct location, being in alphabetical order. What this feature does exactly is it looks for the item you list, then we have to test what the return value of it is. If it returns -1, then the item is not within our list. If it returns any number greater than or equal to 0, then we know the item exists in our list already. 

The code to produce this output is shown below:


So, if the index is not found in the list for Jojo, the program will insert her into the array at the location of ~nameOne, which will do the ground work for us and find the correct location. The output is shown below:

As you can see, Jojo has been added at the correct location for her name in alphabetical order.

So using the BinarySearch feature to look for an item within an index is very quick and simple in C# because of the built in functionality of Visual Studio. 

Methods

The path which we have followed with C# being very similar Java definitely continues when it comes to methods, and how we set up programs in general.

To begin, let's start with a blank program. Then we will create a new class outside of our "Program" class and we will call it "Customer". Further, we will create public variables Course and isVegetarian in order to distinguish what types of orders we will be dealing with. The idea here will be to determine how much a customer owes a restaurant for an order that is made. We will keep it simple and give customers two options for Appetizer and Entree, being vegetarian, and non-vegetarian.

In order to figure out what we will need to set our price to, there has to be methods within the Customer class that tell the cooks what to serve, and the cashier how much a customer owes. So we will create two methods in Customer which can be called from our main program class that do these tasks. Code to create this class is shown below:


Note the use of the "this." within the methods. This shows that there must be values being brought into the Customer class to help the program function. So we can see that a non-vegetarian item will cost more in all cases. While entrees are the more expensive than appetizers, as we would assume.

Now this can all be simply placed below our main Program class. There is no need to open up another tab or anything of that sort like we would do in Eclipse. We can do this all from one window, helping us see everything that will be happening once we run the program.

So we have the Customer class variables and methods ready to go. Now we must create instances of this class within our main Program class.

In order to accomplish this task, we will have to use the syntax that we are familiar with of 


Then, we must set the values for the variables in our Customer class in order for the methods to work the way we would expect. A quick way to do this is to initiate these items using this syntax:


Now that these values are set, we can create print statements to tell us the order, and their value:


Note the use of "{int}" and "{0:C}" in the print statements. This is a way of interpreting strings to print. The 0 and 1 will be the location of the course and vegetarian preference, while the 0:C converts the value of the order to dollars.

So we will create a few more instances of the Customer class in Program to produce other menu items, and the output will appear as such below:


Now this is a relatively basic example of how to use methods in C#, but it is more than enough to get you started with creating your own. These can become as simple or as complex as you like, and it provides us with the ability to do much more with our programs more efficiently.