Alpha DevCon 2018
Results 1 to 8 of 8

Thread: Programming Puzzle 14 - Sum the Squares

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

    Default Programming Puzzle 14 - Sum the Squares

    Write an xbasic script that prompts the user to supply two integer values,
    let's call them N1 and N2. Then your script should fetch through the integer
    values from N1 to N2 (inclusive) and compute the sum of the squares of
    each integer in the range.

    Instructions:

    Display your answer in a message box. Your answer should show the input
    values, and then the sum of the squared values.

    Your script should provide a graceful way for the user to cancel if they
    decide not to supply either input value.

    Your script should verify that N2 is greater than N1. If it is not then
    an error message should be displayed to the user.

    Export your script and attach it to your reply here.

    Have fun!

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

    Default Re: Programming Puzzle 14 - Sum the Squares

    No one has attempted this yet? If I have time tonight after work I will take a crack at it. Really curious to see others try it first.
    Attached Files Attached Files
    Last edited by aschone; 06-30-2011 at 10:28 PM.

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

    Default Re: Programming Puzzle 14 - Sum the Squares

    Uploaded my script; very minimal testing was done, so please be kind

  4. #4
    Member pmanandhar's Avatar
    Real Name
    Pratik
    Join Date
    Apr 2011
    Location
    Sterling Heights, Michigan, NEPAL
    Posts
    15

    Default Re: Programming Puzzle 14 - Sum the Squares

    Puzzle_14.txt
    Here is my code for this puzzle... Please forgive me for no comments..

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

    Default Re: Programming Puzzle 14 - Sum the Squares

    Very nice work guys.

    Two different approaches.

    Andy's code for the input dialog looks complex, but was largely generated by action scripting. It's an example of xdialog code. His code anticipates that the user might enter a non-integer, and discards the decimal portion of both input values. The heavy lifting is done in a function he designed. Notice that the Function can be positioned anywhere in his script. He has it located AFTER his END statement. In the Function he loops through the integer values in the user supplied range without using a For ... Next loop. Instead, he uses While ... End While. Such a code block is executed repeatedly until the condition expression at the top of the loop evaluates to False. Notice that Andy does not pass the input values to his function. The function can "see" the shared variables in the top part of the script. Prof. Pickypicky gives Andy high marks for limiting the input range to integer values, but dings him a couple of demerits for relying on "shared" variables, instead of passing them in as arguments to the Function. Prof. Pickypicky is lazy. If the Function included two numeric arguments for the input values it could be saved to the Code page of the control panel and called from any other place in the application. Code reuse means less work down the line. Less error checking. Easier maintenance. Very satisfying to lazy programmers like Pickypicky.

    Pratik's approach gets the same results as Andy's, provided input values are limited to integer values. However, if the user ignores instructions and enters values that have decimals, there is no error trapping or correction to prevent the calc from proceeding. Notice that Pratik's approach relies upon a mathematical expression that does all the heavy lifting in one line, without using a loop. Very handy if you're a trained mathematician. However Prof Pickypicky finds the absence of explanatory comments troubling. ( Perhaps because he's not a trained mathematician ! )

    Thanks to both for tackling this. Stay tuned for other puzzles coming soon.
    Last edited by Tom Cone Jr; 07-02-2011 at 06:13 PM.

  6. #6
    Member
    Real Name
    Jeb Richardson
    Join Date
    Aug 2011
    Location
    Bowling Green, KY
    Posts
    26

    Default Re: Programming Puzzle 14 - Sum the Squares

    Puz 14 attempt.

    puz 14.txt

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

    Default Re: Programming Puzzle 14 - Sum the Squares

    Hey, Jeb. Prof. Pickypicky wants to know why the program approves input values if all the user does is click the cancel buttons....

    In his experience it's ususally best to provide a way for the user to bail out if they've started the script by mistake, especially in cases where they're asked to supply input values.

  8. #8
    Member
    Real Name
    Jeb Richardson
    Join Date
    Aug 2011
    Location
    Bowling Green, KY
    Posts
    26

    Default Re: Programming Puzzle 14 - Sum the Squares

    Revised

    puz 14.txt

Similar Threads

  1. Programming Puzzle 1 - Simple Loop
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 32
    Last Post: 12-26-2011, 01:38 PM
  2. Programming Puzzle 3 - Fractions, anyone?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 7
    Last Post: 11-08-2011, 05:40 AM
  3. Programming Puzzle 11 - More Loops
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 11
    Last Post: 08-20-2011, 04:50 PM
  4. Programming Puzzle 13 - Paycheck Amount?
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 3
    Last Post: 08-12-2011, 02:27 PM
  5. Programming Puzzle 12 - Perfect Squares
    By Tom Cone Jr in forum Xbasic Programming Puzzles
    Replies: 4
    Last Post: 08-12-2011, 02:21 PM

Bookmarks

Posting Permissions

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