# Thread: Programming Puzzle 14 - Sum the Squares

1. ## 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. ## 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.

3. ## Re: Programming Puzzle 14 - Sum the Squares

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

4. ## Re: Programming Puzzle 14 - Sum the Squares

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

5. ## 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.

6. ## Re: Programming Puzzle 14 - Sum the Squares

Puz 14 attempt.

puz 14.txt

7. ## 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. ## Re: Programming Puzzle 14 - Sum the Squares

Revised

puz 14.txt

#### Posting Permissions

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