C:/Musimathics_local/Musimat/MusimatChapter9/C091201a.cpp

Go to the documentation of this file.
00001 #include "MusimatChapter9.h"
00002 MusimatChapter9Section(C091201a) {
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 }
00030 
00031 Integer cycle(IntegerList L, Integer Reference pos, Integer inc) {
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 }
00036 
00037 Static Void para1() {
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 }}
00064 
00066 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:26 $ $Author: dgl $ $Name:  $ $Id: _c091201a_8cpp-source.html,v 1.4 2006/09/12 17:37:26 dgl Exp $ */
00067 // The Musimat Tutorial © 2006 Gareth Loy
00068 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00069 // and published exclusively by The MIT Press.
00070 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00071 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00072 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00073 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00074 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00075 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00076 // The Musimat website:                 http://www.musimat.com/
00077 // This program is released under the terms of the GNU General Public License
00078 // available here:                      http://www.gnu.org/licenses/gpl.txt
00079 

Generated on Tue Sep 12 10:14:14 2006 for Musimat Chapter 9 Code Examples by  doxygen 1.4.7