Lab 4: How Do You Sum an Elephant?

In this hands-on activity we will be making a calculator to determine the value of very large sums.

Example

Today we'll be answering age-old questions, like why is the sky blue, where do tax rates come from, and more importantly, what is 1+2+...+1000?

For example, in JavaScript:

// Input Variables
var cutoff = 1000;

// Let's calculate the answer
var sum = 0;
for (var x=1; x <= 1000; ++x){
    sum += x;
}

// Let's format the message
var message = "The total of 1+2+...+"+cutoff+
              " is "+sum+
              ".";

// And let's output the message and be done
console.log(message);

Questions

Part 1

  1. Answer for each language: What is the syntax for for loops in your language? How would you write a for loop to print the first 100 integers? Demonstrate your answer with screenshots.
  2. Answer for each language: What is the syntax for while loops in your language? How would you write a while loop to print the first 100 integers? Demonstrate your answer with screenshots.
  3. Between for and while loops, which do we say is "definite" and which do we say is "indefinite"?
  4. What are the three parts to for loops that go inside the parentheses (in most languages)?
  5. What data type must the condition of a while loop resolve to?
  6. Answer for each language: How do you get random numbers in your language? How would you create a variable with a random real-number value between zero and one (for example: 0.1, 0.45351, 0.8741)? Demonstrate your answer with screenshots.
  7. Answer for each language: How do you get numeric input from the user via the keyboard in your language? How would you ask the user to enter their age and get the response? Demonstrate your answer with screenshots. (Note: if you are using RexTester for this step, be sure to ask me how the "keyboard" works on this website)

Part 2

  1. Pick one language from your group. What language did you choose for this task and why?
  2. In your chosen language, program the sum calculator described above. Change the value of the cutoff variable to 2000. Demonstrate your answer with screenshots.

Part 3

For each of the following, demonstrate your answer with screenshots.

  1. Revise your sum calculator so that it computes powers instead (like 2*2*...). Using your new "power calculator," what is the value of 3 to the 16th power?
  2. Revise your sum calculator so that it sums random numbers (between zero and one) and computes their average instead. Using your new "mean-value calculator":
    • What is the mean of 10 random numbers?
    • What is the mean of 100 random numbers?
    • What is the mean of 1000 random numbers?
    • Does it appear that these averages are approaching some specific value, and if so, why do you believe that is, although we are using random values?
  3. Revise your sum calculator so that it uses a while loop instead of a for loop like so: Before the loop, ask the user for a number; Add that number into your sum; Then while their number was not zero, ask for another number, and add that number into the sum; Finally, after the loop, display the result of their sum. Using your new "interactive calculator," compute 1915+1683+1000_1798+2000+1324.