<< Chapter < Page | Chapter >> Page > |
To illustrate the use of the shift registers , the following example computes the Fibonacci number Fib(n).
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] .
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] .
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).
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.
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.
To disable auto-indexing, right click on the target Auto-Indexed Tunnel and select Disable Indexing from the pop-up menu.
The final diagram with the Auto-Indexed Tunnel disabled is shown in [link] .
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.
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.
Notification Switch
Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?