/Users/garethloy/Musimathics/Musimat1.2/MusimatChapter9/C091201a.cpp File Reference

#include "MusimatChapter9.h"

Go to the source code of this file.


 MusimatChapter9Section (C091201a)
Integer cycle (IntegerList L, Integer Reference pos, Integer inc)
Static Void para1 ()

Function Documentation

Integer cycle ( IntegerList  L,
Integer Reference  pos,
Integer  inc 

Definition at line 31 of file C091201a.cpp.

        Integer i = PosMod(pos, Length(L)); // compute current index
        pos = PosMod(pos + inc, Length(L)); // compute index for next time
MusimatChapter9Section ( C091201a   )

Definition at line 2 of file C091201a.cpp.

References para1().

        Print("*** Cycle ***");
         This method iterates a sequence either forward or backward. It can either select successive 
         elements or skip through the list. When it reaches the end of the list (either end), it starts over at 
         the other end. Its inputs are
         o  The list to traverse
         o  The previous position in the list
         o  Whether to move forward (prime) or backward (retrograde)
         Its output is the next element in sequence based on its previous position in the list. As a side 
         effect, it updates its position in the list.
         If it traverses the list forward, it returns to the head of the list when it goes past the tail. If it 
         traverses the list in retrograde, it returns to the tail of the list when it goes past the head.
         In the following code example, setting inc to 1 moves forward one element every time 
         cycle() is called, and setting inc to -1 moves backward one element at a time. Setting inc to 
         any other value skips through the list by that amount, wrapping around at the ends.
        para1(); // Step into this function to continue.
Static Void para1 (  )

Definition at line 37 of file C091201a.cpp.

References cycle(), and n().

         The pos argument keeps track of the position in the list. We wish to delegate to cycle() the 
         job of managing the list position, so we declare pos as a Reference argument. Thus, when 
         cycle() updates pos, the corresponding actual argument is changed. (If pos were not a 
         Reference variable, any changes cycle() made to its value would be lost when it returns (see 
         appendix B, B.1.22).
         Here's an example of invoking cycle():
        Print("*** Cycling a list ***");
        IntegerList L(10, 11, 12);
        Integer myPos = 0;
        Integer n = 2 * Length(L) -1; // go 1 less than two times through list
        For (Integer i = 0; i < n; i = i + 1)   
                Print(cycle(L, myPos, 1)); // 1 = forward direction
        Print("myPos=", myPos);
         This program prints 10, 11, 12, 10, 11. Last, it prints myPos=2, proving that cycle() is 
         changing the myPos parameter.