- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatChapter9/C091201b.cpp

Go to the documentation of this file.

    00001 #include "MusimatChapter9.h"
    00002 MusimatChapter9Section(C091201b) {
    00003 Print("*** Palindrome ***");
    00004 /*****************************************************************************
    00005
    00006 Palindrome
    00007
    00008 We can iterate a sequence in prime order until the last element in the sequence is
    00009 reached, then iterate the sequence retrograde until the first element in the sequence is reached, then
    00010 repeat.
    00011 *****************************************************************************/
    00012         para1(); // Step into this function to continue.
    00013 }
    00014
    00015 Integer palindrome(IntegerList L, Integer Reference pos, Integer Reference inc) {
    00016         Integer curPos = pos;
    00017         Integer x = cycle(L, pos, inc);
    00018         If (curPos + inc != pos){
    00019                 inc = inc * (-1); // change direction
    00020                 pos = curPos;
    00021         }
    00022         Return(x);
    00023 }
    00024
    00025 Static Void para1() {
    00026 /*****************************************************************************
    00027 This method calls cycle() to do most of its work. Like cycle(), this method updates pos,
    00028 but it also must update its increment argument, inc, because whenever it hits the end of the list,
    00029 we want it to reverse the direction of traversal rather than start over. The extra work done by this
    00030 method is to change the increment and reset the position when either end of the list is reached. Here
    00031 is an example of invoking palindrome().
    00032 *****************************************************************************/
    00033
    00034         Print("*** Palindrome ***");
    00035         IntegerList L(10, 11, 12);
    00036         Integer myPos = 0;
    00037         Integer myInc = 1; // can be any positive or negative integer
    00038
    00039         For (Integer i = 0; i < 2 * Length(L); i = i + 1)
    00040                         Print(palindrome(L, myPos, myInc));
    00041
    00042 /*****************************************************************************
    00043 prints 10, 11, 12, 12, 11, 10. Note that the end of the list is printed twice. This makes it a
    00044 so-called even palindrome. It would be an odd palindrome if it were 10, 11, 12, 11, 10. It is left
    00045 as an exercise for the reader to adapt palindrome() to generate odd palindromes.
    00046
    00047 *****************************************************************************/
    00048 }}
    00049
    00051 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:42 $ $Author: dgl $ $Name:  $ $Id: _c091201b_8cpp-source.html,v 1.2 2006/09/09 06:22:42 dgl Exp $ */
    00052 // The Musimat Tutorial © 2006 Gareth Loy
    00053 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00054 // and published exclusively by The MIT Press.
    00055 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00056 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00057 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00058 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00059 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00060 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00061 // The Musimat website:                 http://www.musimat.com/
    00062 // This program is released under the terms of the GNU General Public License
    00063 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00064