Alpha DevCon 2018
Results 1 to 10 of 10

Thread: Programming Puzzle 23 - For / Next Loop

  1. #1
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Programming Puzzle 23 - For / Next Loop

    Write an xbasic script that solves this puzzle using a For / Next loop. Include comments so the "professor" can figure out what you're doing without losing any more of his quickly disappearing hair! Display your result in a message box on screen. For extra credit write your result to the trace window, too.

    Puzzle 23:

    Jennifer arranged for a party and had a basket of strawberries to serve. She ate 5 strawberries before the party began, and gave a friend 3 more. Eight girls came to the party. The first girl took one strawberry. The second girl took three. The third girl took five, and so on. Her basket was empty after the last girl took hers. So, how many strawberries did she start with?

    Have fun!




    Puzzle courtesy easycalculation.com

  2. #2
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default Re: Programming Puzzle 23 - For / Next Loop

    Answer is "enough"
    Attached Files Attached Files
    Andrew

  3. #3
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 23 - For / Next Loop

    Very nice, Andrew!

    Professor Pickypicky asked me to remind you that you can't always count on Alpha Five to convert your data types for you. At line 38 you pass a numeric value to an argument in the ui_msg_box() function that is expecting a text string. In this case Alpha autoconverts your numeric to character data so the msg box does not fail. You cannot always count on autoconvert to correct data type mismatches. The professor thinks a better approach would be to convert the numeric value to a string of characters and pass that string to the msg box.

    The professor was wondering, also, whether you can think of a way to populate array elements 3 through 10 using a loop, to avoid having to repeat the same code over and over... ? The professor's intern wants to know something else... is there some way to compute the sum for the 8 "partiers" using a For .. Next loop that does not involve an array ? If so, maybe that would be a tiny bit more efficient.

    Thanks for playing!

    -- tom

  4. #4
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default Re: Programming Puzzle 23 - For / Next Loop

    Second attempt
    Attached Files Attached Files
    Andrew

  5. #5
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 23 - For / Next Loop

    Sweet!

  6. #6
    Member
    Real Name
    Tony Evans
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    46

    Default Re: Programming Puzzle 23 - For / Next Loop

    Answer: Puzzle 23.txt

    I'd be interested to find an example of showing the result in the trace window grammatically. I attempted using debug(1) and trace.write(), but to see the value of the variables, had to enter them in the debug screen.

  7. #7
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 23 - For / Next Loop

    Tony,

    Add this to the end of your script.

    Then run the script, then open Trace Window from the View Menu.

    Code:
    ' tom's code follows:
    
    dim vc_berries_count as C = ""   ' initialize character value variable
    vc_berries_count = ltrim(str(Stawberries_taken,10))   'convert numeric to string, trim leading blanks
    
    ' display in trace window
    trace.writeln("Jennifer started with " + vc_berries_count + " strawberries.")

  8. #8
    Member
    Real Name
    Tony Evans
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    46

    Default Re: Programming Puzzle 23 - For / Next Loop

    Thanks Tom,

    Turns out that I didn't know what the Trace Window was. That will be helpful going forward.

    I'm curious about why you create a type=C variable rather than allowing A5 to handle the conversion through context here. Is this a momentum and/or comfort and/or "haven't sufficiently tested the new thing" thing, or do you understand there to be perils in using the context based conversion?

  9. #9
    "Certified" Alphaholic
    Real Name
    Andrew Schone
    Join Date
    Dec 2005
    Location
    Kansas
    Posts
    1,044

    Default Re: Programming Puzzle 23 - For / Next Loop

    There is potential for perils in context conversion of data types.

    Some languages I have used you had to explicitly convert the data types to use them in a different context. So for some this is a hold over of having to it manually. For others it is done to make sure issues do not occur in the future.

    For me, I try to always convert the data types instead of having Alpha5 do it, but sometimes I get lazy and/or forget.
    Andrew

  10. #10
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,298

    Default Re: Programming Puzzle 23 - For / Next Loop

    Tony, I explicitly convert all data types as a matter of course. In part this is because I don't know the "limits" for the built in auto-conversion routines. Do they work in all contexts? For all data types? I don't know. Guess I'm too lazy to figure it out. I also think that handling this explicitly simplifies debugging. If I screw up the data conversion I see it right away and can fix it. If Alpha screws up the data conversion the trail to the problem is not as clearly marked.

    And, yes, the trace window is a very very handy tool. The professor thinks maybe its underutilized, so that's why she frequently offers extra credit for solutions displayed there.

Similar Threads

  1. Programming Puzzle 2 - Another Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 16
    Last Post: 12-20-2014, 10:38 AM
  2. Programming Puzzle 22 - What do you Mean?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 5
    Last Post: 05-23-2012, 07:09 AM
  3. Programming Puzzle 1 - Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 32
    Last Post: 12-26-2011, 01:38 PM
  4. Programming Puzzle 7 - Yet another nested loop!
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 4
    Last Post: 08-08-2011, 05:41 PM
  5. Programming Puzzle 6 - Another nested loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 6
    Last Post: 08-08-2011, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •