• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Stuff I Didn’t Publish This Year

    December 12, 2007 Ted Holt

     

     

     

    Note: The code accompanying this article is available for download here.

     

    My wife and I recently visited our local Cracker Barrel, which is one of our favorite restaurants. While we were waiting for our food, I picked up the peg game that unfailingly occupies patrons at the tables of that establishment and, for the umpteenth time, failed to solve it.

    The Peg Game

    In case you’re not familiar with a peg game, here’s how it works. The board has 15 holes arranged in a triangle and 14 of the holes are occupied by pegs. To move, you jump a peg over an adjacent peg and land in the unoccupied hole on the other side, then remove the jumped peg from the board. The object of the game is to leave one peg on the board. It should be easy, but I can’t figure it out. I always leave at least two or three pegs, which places me in the ignoramus category, I think.

    As my wife and I sat there, trying to think of something to talk about other than the kids, it occurred to me that, while I’m too stupid to find a solution to the puzzle, I’m not too stupid to make a computer find a solution. A week or two later, I threw together an RPG program to print all possible solutions to the peg game. If you’re interested, download it here.

    With the number one hole left blank, here are four solutions.

    1. 040201
    2. 060504
    3. 010306
    4. 070402
    5. 100603
    6. 120805
    7. 130906
    8. 020509
    9. 030610
    10. 151006
    11. 060913
    12. 141312
    13. 111213
    1. 040201
    2. 060504
    3. 010306
    4. 100603
    5. 070402
    6. 130804
    7. 140905
    8. 020407
    9. 030508
    10. 110704
    11. 040813
    12. 121314
    13. 151413
    1. 060301
    2. 040506
    3. 110704
    4. 020407
    5. 130804
    6. 070402
    7. 151413
    8. 121314
    9. 100603
    10. 140905
    11. 010306
    12. 060504
    13. 020407
    1. 060301
    2. 151006
    3. 080910
    4. 100603
    5. 020509
    6. 140905
    7. 121314
    8. 070402
    9. 030508
    10. 010204
    11. 040813
    12. 141312
    13. 111213

    Each move is a six-digit number made of a two-digit beginning position (From), a two-digit position to be jumped (Over), and a two-digit ending position (To). The first move of the first solution, 040201, means “the peg in hole 4 jumps the peg in hole 2 and lands in hole 1.”

    I numbered the holes one to 15, like this:

                       1
                   2      3
                 4     5     6
               7    8     9    10
             11   12   13    14   15
    

    According to my program, there are 29,760 solutions to the peg game. I can’t find even one of them. Ignoramus is too generous.

    Indicators: *ON, *OFF, *NEITHER?

    One of the first things I learned about RPG was that an indicator can be either off or on. This information was crucial because I was using the RPG II compiler on a System/3 Model 12. We didn’t have any of these fancy IFxx, CABxx, CASxx, and DOxxx op codes or *INxx variables.

    Fast-forward to the 21st century and witness how much we have progressed. Now a variable can be off, on, or neither off nor on. A three-valued indicator? Have we arrived or what? Here’s how it’s done.

    Declare a two-byte character variable.

    D SomeVar         s              2a   inz(*off)
    

    SomeVar is *ON if it holds two ones, *OFF if it contains two zeros, and is neither *ON nor *OFF if it has one one and one zero. Now you can code expressions like these:

    if SomeVar = *on;
       DoWhatever();
    endif;
    
    if SomeVar = *off;
       DoThis();
    endif;
    
    if SomeVar <> *on and SomeVar <> *off;
       DoSomething();
    else; 
       DoSomethingElse();
    endif;
    
    if SomeVar = *on or SomeVar = *off;
       DoSomething();
    else; 
       DoSomethingElse();
    endif;
    

    Don’t those last two ifs look weird? Believe it or not, it’s possible for the else branches to execute. And no, before you ask, I’ve never found a use for this technique.

    Store a Binary Tree in an Array

    Years ago, when I was earning my computer science degree, I used linked lists to implement binary trees in Pascal programs. Since linked lists don’t work too well in RPG, it may be better to use an array instead. “How does that work?” I hear you ask.

    Consider the following binary tree:

             Joe
            /   \
           /     \
          /       \
         /         \
       Bob         Sam
       / \         / \
      /   \       /   \
     /    \      /     \
    Jim   Bud   Don    Abe
    

    The same can be stored as an array. The left leaf of node nis in element 2n. The right leaf is in element 2n + 1.

    Joe

    Bob

    Sam

    Jim

    Bud

    Don

    Abe

    To find the parent of a node, divide the element number by two and throw away the remainder.

    Logical Shortcuts

    I have known people who took pride in writing a program as concisely as possible. If memory serves me correctly, the old Data Network magazine, the forerunner to Midrange Computing, often presented programming problems in which the object was to achieve the desired output with as few lines of RPG or OCL as possible.

    I still like to keep programs short, but not at the expense of readability. Some shortcuts are clearly better, at least in my opinion. For example, I much prefer this:

    /free
       *in25 = not *in25;
    

    To this:

    /free
       if *in25;
          *in25 = *off;
       else;
          *in25 = *on;
       endif;
    

    I offer the following logical shortcuts with a healthy dose of skepticism that they are better than their longer equivalents.

    Problem 1: Verify that an order is ready for shipment (i.e., all lines of an order are complete). A line is complete if the status code is E.

    Long way:

    ReadyToShip = *on;
    dow '1';
       read OrdLine;
       if %eof();
          leave;
       endif;
    
       if Status <> 'E';
          ReadyToShip = *off;
       endif;
    
    enddo;
    

    Short way:

    ReadyToShip = *on;
    dow '1';
       read OrdLine;
       if %eof();
          leave;
       endif;
    
       ReadyToShip = ReadyToShip and Status = 'E';
    
    enddo;
    

    One line has replaced three lines. Here’s a similar example.

    Problem 2: Is at least one line of an order ready to ship?

    Long way:

    OneLineIsReady = *off;
    dow '1';
       read OrdLine;
       if %eof();
          leave;
       endif;
    
       if Status = 'E';
          OneLineIsReady = *on;
       endif;
    
    enddo;
    

    Short way:

    OneLineIsReady = *off;
    dow '1';
       read OrdLine;
       if %eof();
          leave;
       endif;
    
       OneLineIsReady = OneLineIsReady or Status = 'E';
    
    enddo;
    

    What do you think? Better or worse?

    While I’m on the subject of logic, here’s a bit of code from a program I worked on recently. Do you see the logic error?

    C     SOMEKEY    CHAIN     SOMEFILE                97 
    C     *IN97      IFEQ      '0'                                   
    C     *IN97      DOWEQ     '0'                                   
     ... more stuff 
    C                ENDDO
    C                ENDIF
    

    Enjoy the Holidays

    My family and I will soon be celebrating Christmas. I used to muddle through the Christmas season with a bit of cynicism, relating to the words of Henry Wadsworth Longfellow.

    And in despair, I bowed my head:
    “There is no peace on earth,” I said,
    “For hate is strong, and mocks the song
    Of peace on earth, good will to men.”

    But I finally figured out that misery, cynicism, and other negative emotions don’t solve any problems. Just the opposite is true. There is enough misery in the world without my choosing to be miserable, too.

    So now I enjoy Christmas. I enjoy the special songs and carols, the lights and decorations, the excitement in my children’s faces, the gatherings with family and friends. I receive more pleasure from giving than from getting. These days I think on other words from Longfellow:

    Then pealed the bells more loud and deep:
    “God is not dead, nor doth He sleep;
    The wrong shall fail, the right prevail,
    With peace on earth, good will to men.”

    Whatever holidays you celebrate, if any, I wish for you a season of happiness and peace. I hope that 2008 will be the year when everyone on earth becomes as happy as I am.

    Answer to the logic question:

    Since do-while is a top-tested loop, the if is unnecessary.

     

     



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    iTech Solutions

    Choose Your Own IBM i OS Upgrade Adventure

    Choice 1:

    • Plan for 3 months
    • Check hardware & software compatibility
    • Check Lan Console, MQ, Domino, SMB, Ciphers, WebSphere, Java
    • Test, test, and test again
    • Prepare for potential downtime
    • Hope the OS Upgrade goes smoothly
     

    Choice 2:

    • Strategically plan alongside a team of IBM i experts
    • Work with experienced system admins to ensure hardware & software compatibility
    • Receive full analysis of Lan Console, MQ, Domino, SMB, Ciphers, WebSphere, Java
    • Know which PTFs are required for the upgrade
    • Relax, and let iTech handle the rest

    Not every OS Upgrade has to be an adventure. We make the process easy for you.

    Having completed thousands of upgrades, we have the experience, know-how, and expertise to get the job done seamlessly. We know what can go wrong, what to plan for, and can act quickly if problems arise.

    No matter where you are in your journey, we’re here to help. Take a look at the video below to ensure you’re on the right path when it comes to your next IBM i OS Upgrade.

    [Video] What You Need to Know to Successfully Upgrade to IBM i 7.4 and 7.5

    An IBM i OS upgrade isn’t complete until all the boxes are checked. Is your list up to date?

    In this video, Pete Massiello covers what’s new on IBM i 7.5, planning tips, pre-requisites, and post-installation requirements for a successful OS Upgrade.

    [ Watch Now ]

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    looksoftware:  Present your core System i applications in Outlook, Google and Notes COMMON:  Join us at the annual 2008 conference, March 30 - April 3, in Nashville, Tennessee NowWhatJobs.net:  NowWhatJobs.net is the resource for job transitions after age 40

    IT Jungle Store Top Book Picks

    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Now’s the Time to Review Business Continuity Strategy, SunGard Says A New Year, A New IBM Systems and Technology Group

    Leave a Reply Cancel reply

Volume 7, Number 43 -- December 12, 2007
THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
DRV Technologies

Table of Contents

  • Use SQL User-Defined Functions to Avoid Data Repetition Problems
  • Stuff I Didn’t Publish This Year
  • Admin Alert: Getting Started with Trial Capacity on Demand, Part 2

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • IBM i 7.3 TR12: The Non-TR Tech Refresh
  • IBM i Integration Elevates Operational Query and Analytics
  • Simplified IBM i Stack Bundling Ahead Of Subscription Pricing
  • More Price Hikes From IBM, Now For High End Storage
  • Big Blue Readies Power10 And IBM i 7.5 Training for Partners
  • IBM Delivers More Out-of-the-Box Security with IBM i 7.5
  • Groundhog Day For Malware
  • IBM i Community Reacts to IBM i 7.5
  • Four Hundred Monitor, May 11
  • IBM i PTF Guide, Volume 24, Number 19

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.