Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a RURPLE program that will print out the result of adding numbers togethe

ID: 3734511 • Letter: C

Question

Create a RURPLE program that will print out the result of adding numbers together. The numbers will be specified by dropping beepers into the world. Each stack of beepers represents one digit in a number. The numbers will be on each row of the world. There will be no blank rows. There will never be any beepers in the left most column (the one where the robot starts). For example, here is a world with five numbers in it: 1 2 3 4 5 6 7 8 9 10 Avenue The robot would need to figure out what each number was, and display the result using a print command. In this world, the result would be 18+37+2+30+12, for a total of 99 Requirements Create a defined command that will add up the total number of beepers on the robot's current row. The command should return the value of the number on that row. The general approach should be to move all the way to the far wall, and start picking up beepers on the right end of the number. Those beepers are the ones digits, so each beeper counts as just 1. The next digit to the left counts as the tens, then the one hundreds, etc. Keep going until you get back to the left wall. This will require some thought about your coding, and about using variables. We will go through the concepts of the algorithm in class, but you will have to fill in the details and code it yourself Call that defined command for each row, adding the result to a running total. At the end, print out the running total.

Explanation / Answer

Integration review

Let us integrate two numbers in the "traditional way", from left to right:

528
+ 634
------
   12 # integrating the units first (8+4)

We note that we have a "1" to carry over the "tens" column. This "carry over" is most likely where your program had quandaries. Let us re-indite it in the "conventional" way and perpetuate.

   1
528
+ 634
------
1162

Ok, that was marginally brief, but I'm sure you were able to follow. In Reeborg's world, we would relish this additament to look as follows:

integrating startlead to integrating end

Let's tackle first the simpler quandary of integrating 8+4.

3. Integrating 8+4 in base 10.

As we have mentioned, the quandary of integrating numbers so that each beeper pile represents a single digit comes when we have to integrate two numbers whose sum is more preponderant than 9 (in base 10). Somehow, we require to keep track of this magic number (10), no matter what two numbers we are going to integrate. I have engendered a world (file: integrating_world.wld) that is sizably voluminous enough to integrate two 7-digit numbers in base 10 (or even in base 29!). Load up that world file and I will guide you so that you can indite a program that can do integrations opportunely.

After loading the world file, if you optically canvass the bottom of the screen, you will visually perceive that Reeborg carries 8 beepers. Indite a program so that Reeborg puts a line of beepers, one at each corner of 10th street, as illustrated below (after my program ended, I utilized the cursor keys to move Reeborg out of the way as he was standing on top of the beeper in the last column.

line of beepers accross 10th street

Now, ascertain you preserve this program afore going any further.

Reload the world file and integrate beepers in the bottom right corners so that the exhibit looks homogeneous to the following, but with Reeborg standing at the inchoation (corner of 1st avenue and 1st street).

8+4

Have Reeborg do the following:

    Put a line of beepers across 10th street as you have done afore.
    Go to the bottom right of the screen, accumulating the two piles (8 and 4) of beepers.
    Spread those 12 beepers on a vertical column as illustrated below.
    (Consequential: you will require to utilize the test
    carries_beepers() which I haven't told you about ... yet! Endeavor something like
    while carries_beepers(): ...)

8+4 spread vertically

Now, we have two beepers above the horizontal line of beepers (the units in the number 12) and an extra beeper on the horizontal line (which we can utilize as the "carry over"). So, all that we you have to do is

    Have Reeborg pick up the 9 beepers below the horizontal line, and discard them (perhaps by putting them all on the horizontal line);
    Have Reeborg keep going north past the last beeper, as illustrated below:

    8+4
    Have Reeborg turn around, pick up one beeper and move, reiterate until Reeborg reaches a corner where there is no more beeper to pick up (below the horizontal line); at this point, Reeborg should be carrying three beepers.
    Have Reeborg carry these beepers down, until Reeborg reaches the wall;
    Have Reeborg put them all down (3), pick one up (the carry over), move west and put the carry over beeper down.

All that is left to do is to move Reeborg out of the way to exhibit the result!

8+4

Well ... authentically, those five steps as I indited them will require inditing a fair bit of code and you might find it marginally arduous to get it right. But you will if you proceed systematically. Endeavor it out!

4 Integrating 3+5

So, you determinately got your program to calculate 8+4. Great! Now endeavor it on 3+5. Does it work? Chances are, it doesn't ... as this doesn't require a carry over. Go back and fine-tune it so that all simple two-digit integrations from 0+0 to 9+9 work felicitously!
5 Integrating multi-digit numbers.

The next thing to do is to generalize this from single-digit integrations to multi-digit ones. My suggestion is to "wrap" the main part of the code inside a while loop, over all the columns. This is left as an exercise.


6. Final challenge

If you ken what it signifies to integrate numbers in bases other than base 10, endeavor to modify your program so that you can integrate numbers in a different base.