Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Programming Puzzle 11 - More Loops

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programming Puzzle 11 - More Loops

    Date Created: 16-Jun-2011 12:52:29 PM
    Last Updated: 16-Jun-2011 12:52:29 PM
    Created By : Tom
    Updated By : Tom
    Puzzles and Problems

    Beginning Xbasic Programming

    Puzzle 11 - More loops

    Your manager has been to a seminar (so you know trouble is coming!). She's
    decided she can manage sales better if she has more information about the sales
    transactions. For the items in the invoice_items table she wants to know:

    a) what's the average product price for all products in the table?

    b) what's the average quantity sold for all products in the table?

    c) what's the average dollar value for all the invoices reflected in the table?


    Instructions:

    Write an xbasic script that answers these questions.

    Display your answers in one or more message boxes on screen.

    Extra credit: For each product in the table compute the
    quantity that's sold on average. i.e. the average quantity per product
    Last edited by Tom Cone Jr; 06-16-2011, 01:08 PM.

  • #2
    Re: Programming Puzzle 11 - More Loops

    Attached.

    Puzzle_11 - 06-16-2011 -1.txt
    There can be only one.

    Comment


    • #3
      Re: Programming Puzzle 11 - More Loops

      I got different values, maybe an interpretation difference, and went for least number of table openings.
      Attached Files
      Mike W
      __________________________
      "I rebel in at least small things to express to the world that I have not completely surrendered"

      Comment


      • #4
        Re: Programming Puzzle 11 - More Loops

        I think the issue can be interpreted at least two different ways.

        For the items in the invoice_items table she wants to know:

        a) what's the average product price for all products in the table?

        b) what's the average quantity sold for all products in the table?

        c) what's the average dollar value for all the invoices reflected in the table?
        For the items in the invoice_items table, average price of unique items or average price of items sold? I took the complete table out to a spreadsheet and got the answers my script gives, I think.
        There can be only one.

        Comment


        • #5
          Re: Programming Puzzle 11 - More Loops

          Let's blame the manager! Always giving ambiguous instructions.

          "average product price for all products in the table" should be the average selling price for the products, not the average sale amount for all the transactions. i.e. considering the product mix reflected in the items records, what the heck is the average unit price? The price for each product should be summed and then the total is divided by the number of products. If a product is sold more than once it still only should be counted once when figuring the average product price, ok? No weighted averages are desired.

          Comment


          • #6
            Re: Programming Puzzle 11 - More Loops

            I understand the question but...

            No weighted averages are desired.
            So if 99 units of a product selling for $1 are sold and 1 unit of a product selling for $100 are sold, the average selling price is $50.50? And thus the result is meaningless.

            Sorry, let's all agree to meet surreptitiously and shoot the silly, bossy manageress.
            (nine words with two letters repeated consecutively in one 13 word sentence)
            There can be only one.

            Comment


            • #7
              Re: Programming Puzzle 11 - More Loops

              Stan, I think our manager was heavily influenced by the "advice" she received at her seminar. Since the speakers there were from out of town they had to know what they were talking about, right?

              Comment


              • #8
                Re: Programming Puzzle 11 - More Loops

                I took the first question to be as Tom described it again.
                The second, I figured needed to be a whole number, so I took the integer instead of rounding (which would round up) or presenting a fraction of an item. I also took the data to a spreadsheet to confirm the dollar average for all the invoices (attached). I'm still studying Stan's code to see where the difference is with that.
                Attached Files
                Mike W
                __________________________
                "I rebel in at least small things to express to the world that I have not completely surrendered"

                Comment


                • #9
                  Re: Programming Puzzle 11 - More Loops

                  My results agree with Mike Wilson, except I get an average quantity sold for ALL products in the
                  the table to be 3.98. He gets an average of 3. This may be a rounding difference (fractional products
                  can't be sold).

                  For brother Mathews I get a different average product price, and a different average dollar value
                  per invoice. However, our average unit quantity per product per sale agree perfectly.

                  Three different approaches to the problem were employed.

                  Stan and Mike used higher level functions supplied by Alpha, and their scripts are much
                  shorter than mine. To achieve their results they opened multiple instances of the table.
                  For a small table like the one at hand this is not a matter of much importance. If the
                  table was much larger, or if the table was being accessed over a local network, or
                  the internet, these approaches may take subtantially longer.

                  My approach opened a single instance of the table, and employed lower level
                  functions to simply fetch through the records, accumulating values and counts
                  as it went. This can be faster, especially for larger tables retrieved over slower
                  networks. However, much more code is required to be written, tested, and
                  debugged.

                  Comment


                  • #10
                    Re: Programming Puzzle 11 - More Loops

                    My attempt

                    Puzzle_11.txt

                    Comment


                    • #11
                      Re: Programming Puzzle 11 - More Loops

                      Jeb,
                      It looks to me that you have repeated the same strategy in this Puzzle that you did in Puzzle 8 by opening the table and fetching the last record, using that record's recno() value as a count for the number of cycles for your for - next loop. I believe this is a very dangerous approach here for the same reasons explained in puzzle 8. The record number for the last record may not reflect the true number of records in the table and there may be missing (deleted) intervening records. I suggest you stop using this method, as Andy and Tom previously have alluded.
                      Mike W
                      __________________________
                      "I rebel in at least small things to express to the world that I have not completely surrendered"

                      Comment


                      • #12
                        Re: Programming Puzzle 11 - More Loops

                        didn't actually use a loop, just built-in functions, so it probsably doesn't count.
                        Attached Files

                        Comment

                        Working...
                        X