Sunday, March 20, 2016

How to make methods and writing a binary search program in C++

Hello World,

Today we will be discussing methods. This may seem like a really basic lesson that is fundamental to all programmers, but every language is unique in stating procedures/methods. So let’s first talk about the basics. Methods are used when code is a little more complex than to just throw everything in the main method. In most programs, programmers try to keep their work separate so it is easy to follow and understand. There are locations for calculations to be made, places where one can run the program and places to test instances or catch errors. Basically dividing up your code into different sections is essential for many programs.

If you have been following my blog up until this point, you may have noticed the main method in all my programs. The main method is just int main(). This is slightly different from Java. In Java, aside from deciding whether to make your program methods private, protected, or public, you also have to add void or static for different instances. Methods are real basic in C++. Just like the method int main() you can make your own method by typing int desiredmethod() or any text in there.

Today rather than just writing a simple program to display the purpose and use of a method, I will show you how to successfully run a binary search. For starters, binary search is a type of program that takes a sorted array (an array is a list of numbers) and keeps cutting the list in half until the user finds the output that they are looking for. Arrays are zero based so when the program finds a given number and for example states that that number was found at index of 2, it actually means it was found at the third element in the array (0, 1, 2 and 2 is the index in the example). Although binary searching is a very efficient and fast way of finding specific elements in an array there is a catch. In order for you to find a given element, the array must first be sorted and will not function correctly with an unsorted list. I have a program that uses 2 methods the main method and a new method for displaying what happens in a binary search.

Below is the code for the program


This first part shows how to make a method for starters. Int binarySearch is the method. It takes some parameters which is the array, the size of the array, and a variable called search value. It first starts with low being set to 0. This is the first element of the array. High is set to the length of the list minus 1 to include all elements of the array. There must also be a mid variable. This variable will exist to sway one way or the other depending on the number you are looking for. Say you have an array that consists of 10 elements and the number 22 is the one you are looking for. 22 is located at the second element of the list so it’s at index 1. The program first takes the list and sets the number at index 0 to low and the number at the last element of the list to high. Mid is set to (high+low)/2. So in this case it is 10+0=10/2=5. It then sees that the number you are looking for is lower than the mid so it sets high equal to the number just below the mid and low stays the same. If the desired number where in the 9th spot then it would set low equal to one higher than the mid. Nonetheless, it takes the number at index 4 which is now the high and finds the midpoint again. Low=0 and high=4. (4+0)/2=2 and mid is set to 2. Now since the number 22 is in the 2nd index location, it is at the same spot as mid. The program now knows to return the number in the mid location because it found the correct number and thus returns 22. If it does not find the desired number after it keeps cutting the list in half, then it determines that the number you are looking for does not exist.


This next piece of code is simply making the array and populating it with specific ordered numbers, prompting the user to enter a number and calling the other method in this main method. It is also displaying the number found or not found depending on what the user enters. Below I entered some test outputs.


Hope this makes sense. If you are still having trouble, watch this video because it helped me understand the program a lot better. Video: https://www.youtube.com/watch?v=vohuRrwbTT4


Thanks!

No comments:

Post a Comment