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:











Tuesday, March 8, 2016

Loops!

Ada uses loops to run through code several times, either forever or until a condition is met or a test fails.

The different types:

1. Endless loop
2. Loops with condition
3. For loop


Endless Loop


This loop runs forever.
Syntax:

             loop

               [insert code to be repeated];

             end loop;


Loops with Condition


There are three types of condition placement that a loop can use to continue running its code.

Condition in the:

                  1. beginning
                  2. middle
                  3. end

Condition in the beginning uses a test in the beginning to check if it should run a certain looping code. If the condition is met, it will run the code. If not, the code is never executed or if it has already run, will not loop through again.

Syntax:

while [condition] loop

[code to be repeated]

end loop;


Condition in the end uses a test to check if it should run a certain looping code more times. If the condition is met, the code will be repeated. The code in the loop will be ran at least once.

Syntax:



       loop

             [code to be repeated]

               exit when [condition];

       end loop;

Condition in the middle uses a test to check if it should continue to run a looping code or not. If the condition is met, the code will continue. If not, it will exit the loop. It is pretty much the same as the Until loop except the exit appears in the middle instead of the end.

Syntax:

       loop

             [code to be repeated]

               exit when [condition];

             [more code to be repeated]

       end loop;


For Loop



This is used for incrementing a specific variable to control the number of times a looping code is ran.

The syntax:

for I in [low value] .. [high value] loop

         [looping code]

end loop;

OR

for I in reverse [low value] .. [high value] loop

         [looping code]

end loop;


The variable can be incremented up or down to any end value. Normally, the variable to be incremented is type Integer

The starting, end, and incrementing is specified using a Range.

The way in which the variable will increment (up or down) will depend if reverse is used.

The low value and high value MUST be come in the order specified. Low value first, then high value.

You can also loop on Arrays! Syntax:


for I in X'Range loop

           [looping code]
           [to use the current X, you would use X(I)]

end loop;

where X is the Array name.


Examples


Endless Loop:




The output:






Conditional Loops

Condition in beginning:




The output:




Condition in middle or end:




The output:







For Loops


Normal For loop:



The output:




Reverse For loop:



The output: