<< Chapter < Page Chapter >> Page >

A box with a menu overlaid. The item 'Add Element' is highlighted in blue.
Adding Shift Register Elements
A box with a blue box 'N' in the upper left corner and a blue box 'i' towards the lower left. A pair of down arrows exists on the far left side and an up arrow exist on the right side opposite the down arrows.
Adding Shift Register Elements

To illustrate the use of the shift registers , the following example computes the Fibonacci number Fib(n).

Fib ( n ) = { 0 , n = 0 1 , n = 1 Fib ( n - 1 ) + Fib ( n - 2 ) , n > 1

In the Front Panel window, select an integer 32 numeric input and output controls and labeled them n and Fib(n) respectively. Arrange the diagram as shown in [link] .

A form with two fields labeled 'n' and 'Fib(n)' from left to right. Both fields contain the value '0'. Form left two right there is an icone labeled 'n', a box with a blue box 'N' in the upper left corner and a blue box 'i' towards the lower left. A pair of down arrows exists on the far left side and an up arrow exist on the right side opposite the down arrows. Our the far right is another icon labeled 'Fib(n)'.
Shift Register Example

Add a 0 and 1 numeric constants to initialize the elements of the shift register and wire them to the i-1 and i-2 elements respectively. Add the add operator in the for loop and complete the program wiring as shown in [link] .

This image consists of several icons connected by lines. From left to right there is an icon in a blue box and below that are two other small blue boxes containing '0' and '1'. All of these boxes are connected via a blue line  to items in a larger box. They are connected to an 'N', and two down arrows respectively. The two down arrows are connected to a triangle pointing to the righ containing a '+'. Below This is an 'i'. The triangle is connected via a line to an up arrow which is connected to an icon outside the big box labeled 'Fib(n)'.
Fibonacci G Program

For n = 0, the for loop iterates 0 times and passes 0 to Fib(n) , therefore Fib(0)=0. For n = 1, the for loop the values in i-1 and i-2 shift register elements are added (0+1) and saved in the i shift register element (1). Since the loop iterates once only, the resulting value is passed to Fib(n) , therefore Fib(1)=1. For n = 2, the first iteration produces the value of 1. Prior to the next and final iteration, the values are shifted in the register as follows:

The value in the i-2 shift register element is discarded

The value in the i-1 shift register element is shifted to the i-2 shift register element

The value in the i shift register element is shifted to the i-1 shift register element

To start the 2 nd and final iteration, the i-1 shift register element contains 1 and the i-2 shift register element contains 0. These are added to produce 1, which is passed to Fib(n) and, therefore, Fib(2)=1. This process is repeated for values of n >2.

Save this program as Fibonacci.vi . [link] shows the result of Fib(8).

A form with two fields 'n' and 'Fib(n)' with values '8' and '21' respectively.
Fib(8) = 21

Auto-indexing

Auto-indexing allows input array elements to be operated on and output array elements to be aggregated automatically in a for loop. It is not required to wire the Loop Counter . The for loop automatically reduces the array dimensionality by one.

Three icons in a row. From left to right there is an icon labeled 'Input Array' connected to a large box with an 'N' in the upper left and connects to an orange square which is labeled 'Auto-Indexed Tunnel'. This square is connected via an orange line to a sine wave icon and then to another orange square and finally to another icon labeled 'Output Array'.
For Loop Auto-Indexing

Disabling auto-indexing

It is sometimes necessary to disable auto-indexing. In this example, the For Loop is used to scan the elements of the array taking advantage of the auto-indexing feature. However, the result is a single number. Wiring the result through the For Loop with auto-indexing enabled results in a broken data type wire.

A screen capture of 'Broken Auto-Indexing'. From left to right there is an 'Array' icon above a blue square containing '0'. These are connected to a large box containing a blue box containing an 'N', a little square, a blue down arrow and a blue box containing 'i'. The icons outside the big box are connected to the little square and down arrow in the big box via a blue line. Another line connects these icons to two blue dots which then connect to a triangle containing '>'. Lines connect the triangel and two dots to a larger triangle  which is connected to a small square and an up arrow along the far right side of the box. To the right of the box is a red 'x'and to the right of that is an icon labeled 'Maximum'.
Broken Auto-Indexing

To disable auto-indexing, right click on the target Auto-Indexed Tunnel and select Disable Indexing from the pop-up menu.

A screen capture of 'Broken Auto-Indexing'. From left to right there is an 'Array' icon above a blue square containing '0'. These are connected to a large box containing a blue box containing an 'N', a little square, a blue down arrow and a blue box containing 'i'. The icons outside the big box are connected to the little square and down arrow in the big box via a blue line. Another line connects these icons to two blue dots which then connect to a triangle containing '>'. Lines connect the triangel and two dots to a larger triangle  which is connected to a small square and an up arrow along the far right side of the box. To the right of the box is a red 'x'and to the right of that is an icon labeled 'Maximum'. A menu is ovberlaid on top of this diagram with the item 'Disable Indexing' highlighted in blue.
Disabling Auto-Indexing

The final diagram with the Auto-Indexed Tunnel disabled is shown in [link] .

A screen capture of 'Broken Auto-Indexing'. From left to right there is an 'Array' icon above a blue square containing '0'. These are connected to a large box containing a blue box containing an 'N', a little square, a blue down arrow and a blue box containing 'i'. The icons outside the big box are connected to the little square and down arrow in the big box via a blue line. Another line connects these icons to two blue dots which then connect to a triangle containing '>'. Lines connect the triangel and two dots to a larger triangle  which is connected to a small square and an up arrow along the far right side of the box. To the right of that is an icon labeled 'Maximum'.
Disabled Auto-Indexing

While loop

The While Loop conditionally iterates executing the statements within the structure. The Loop Condition establishes whether the loop iterates or terminates. The Loop Iteration is a zero-based iteration execution reference similar to the For Loop.

There is a box containing a blue box 'i' on the lower left and a green square red circle on the lower right. The 'i' is labeled 'Loop Iteration'. The other icon is labeled 'Loop Condition'.
While Loop Structure

Loop condition

Stop if true

The default loop condition is to continue if the Boolean condition is False (or stop if True ). The while loop in [link] will iterate while Iterations is less than Loop Iteration is False or, equivalently, will stop iterating when Iterations is less than the value in Loop Iteration.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to g programming. OpenStax CNX. Mar 15, 2010 Download for free at http://cnx.org/content/col11192/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?

Ask