- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatChapter9/C091201a.cpp File Reference

`#include "MusimatChapter9.h"`

Go to the source code of this file.

## Functions
| 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.

    00031                                                                  {
    00032         Integer i = PosMod(pos, Length(L)); // compute current index
    00033         pos = PosMod(pos + inc, Length(L)); // compute index for next time
    00034         Return(L[i]);
    00035 }

MusimatChapter9Section | ( | C091201a |  | ) |

Definition at line 2 of file C091201a.cpp.

References para1().

    00002                                  {
    00003 Print("*** Cycle ***");
    00004 /*****************************************************************************
    00005
    00006 Cycle
    00007
    00008 This method iterates a sequence either forward or backward. It can either select successive
    00009 elements or skip through the list. When it reaches the end of the list (either end), it starts over at
    00010 the other end. Its inputs are
    00011
    00012 o  The list to traverse
    00013
    00014 o  The previous position in the list
    00015
    00016 o  Whether to move forward (prime) or backward (retrograde)
    00017
    00018 Its output is the next element in sequence based on its previous position in the list. As a side
    00019 effect, it updates its position in the list.
    00020
    00021 If it traverses the list forward, it returns to the head of the list when it goes past the tail. If it
    00022 traverses the list in retrograde, it returns to the tail of the list when it goes past the head.
    00023
    00024 In the following code example, setting inc to 1 moves forward one element every time
    00025 cycle() is called, and setting inc to -1 moves backward one element at a time. Setting inc to
    00026 any other value skips through the list by that amount, wrapping around at the ends.
    00027 *****************************************************************************/
    00028         para1(); // Step into this function to continue.
    00029 }

Static Void para1 | ( |  | ) |

Definition at line 37 of file C091201a.cpp.

References cycle(), and n().

    00037                     {
    00038 /*****************************************************************************
    00039 The pos argument keeps track of the position in the list. We wish to delegate to cycle() the
    00040 job of managing the list position, so we declare pos as a Reference argument. Thus, when
    00041 cycle() updates pos, the corresponding actual argument is changed. (If pos were not a
    00042 Reference variable, any changes cycle() made to its value would be lost when it returns (see
    00043 appendix B, B.1.22).
    00044
    00045 Here’s an example of invoking cycle():
    00046 *****************************************************************************/
    00047
    00048         Print("*** Cycling a list ***");
    00049         IntegerList L(10, 11, 12);
    00050         Integer myPos = 0;
    00051         Integer n = 2 * Length(L) -1; // go 1 less than two times through list
    00052
    00053         For (Integer i = 0; i < n; i = i + 1)
    00054                 Print(cycle(L, myPos, 1)); // 1 = forward direction
    00055
    00056         Print("myPos=", myPos);
    00057
    00058 /*****************************************************************************
    00059 This program prints 10, 11, 12, 10, 11. Last, it prints myPos=2, proving that cycle() is
    00060 changing the myPos parameter.
    00061
    00062 *****************************************************************************/
    00063 }}