Monday, March 21, 2016

Methods/Functions and the Classic Binary Search

One main use of methods in Ada, or in any programming language, is to prevent redundancy in code.

There are two types of methods in Ada:

1. procedures
2. functions

A distinct feature of function or procedure parameters is the ability to specify what exactly the parameter will do regarding the variables passed in.

These specifications are:

in
     the default specification for parameters of methods. This makes the method use the variable passed in as a CONSTANT and thus only allows reading. Changes will not happen to the actual variable passed in.

out
     the value of the variable passed in as a parameter does not matter. It has no value when passed in and needs to be assigned a value in the method. The variable will receive a value in the method and that will be saved as the value of the variable.

in out
     the value of the variable passed in as a parameter can be read and written to. It can be used as a constant or changed. This is a mixture of IN and OUT.

Procedures

Procedures are methods without return values. They are similar to a function in Java with return value of VOID.

The syntax with 2 parameters:
procedure name(parameterName :  in/out/in out   parameterType; parameterName2 :  in/out/in out   parameterType2is

          [insert declarations]

         begin

end name;                 

Calling the procedure syntax:

name(parameter1, parameter2);

Here is an example of a procedure in use using the in parameter type:


And the output:




Notice how the variables passed in were used as constants and read.

Here is an example of a procedure in use using the out parameter type:



And the output:


Notice how the method disregarded the values of the variables passed in and changed them.


Functions

Functions in Ada must use return values and therefore must be used in expressions.

The syntax of a function with 1 parameter:

function name(parameter1 : parameter1Type) return returnType is

            [declarations]

           begin

                   [stuff to do]

end name;


Calling the function syntax:

name(parameter1)

^ MUST BE USED IN AN EXPRESSION

Here is an example of a function with a parameter of type IntArray:



This function adds up all the values in an array passed in as a parameter. In this case, it is used the array anArray to do its calculation.

This is the output:



I can call these procedures and functions as many times as needed without writing the same code over and over. This is what makes them so useful.

The Binary Search Example

A binary search is searching for a number in an array with numbers ordered from lowest to highest. Although it seems simple, we want to do this in the most efficient way possible:

1) take the mid point index of the array and get the value. Compare the value being search with it. If it equals the mid point index's value, the number has been found. If not, check what half of the array it is on.
         1a) if the number being searched is larger than the mid point index's value, search the upper half of the array.
         1b) if the number being searched is smaller than the mid point index's value, search the lower half of the array.

2) each time a new sector of the array is chosen, a new mid point and a new start or end index value will be set according to whether the upper or lower half of the sector of the array is being checked, respectively.

3) this will continue until the number has been found or there are no more numbers to check -- the number is not found in the array.


A little on Arrays...

To do this in Ada, you must also know how to create arrays in Ada.

First, you must create and declare the type of array you will use for your actual array.

Syntax of an INTEGER array:

type nameOfTypeOfArray is array (Positive range <>) of Integer;

Next, you must declare the actual array with its values and start and end index.

The start index should be 1 (yes, not 0.) and the end index will be the last index of your array.

Syntax for an Integer array with 3 values:

arrayName nameOfTypeOfArray (1.. 3) := (50, 70, 80);

Back to the Binary Search Problem:


Here is my solution in code to this problem using methods:





num is the number being searched for in the method findIndex.
anArray is the array being searched through.

Here is the output when num = 93:
 
I expose the mid values to demonstrate where the mid-index is at each check. 
I also expose the number of checks to demonstrate the efficiency of this method.
Instead of 13 checks, only 3 are done.

Here is another example when num = 150, a number not in the array and larger than all the numbers in the array:



And the output:



Another example when num = 0, a number not in the array and smaller than all the numbers in the array:



And the output:











No comments:

Post a Comment