RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

loop practice
counting from a til b by k

Tasks:

  1. Write a function with a while loop, which merely prints the numbers 0..49 (stopping before 50).
    Hint: What is a good name for your loop-control variable? (An idiomatic, one-letter variable-name is great.)
    for pedagogy only: Functions which print but don't return aren't typically of any use.
  2. Modify the above to (print and) sum the numbers 0..49; your function should return the sum.
    Hint: What is a good name for your accumulator/“so-far” variable?
    (Hint: the answer will be 49*50/2 = 1225. But for this lab, use a loop, and not any formula.)
  3. Modify the above to (print and) sum the numbers [0,n).
    Hint: What is a good name for your parameter? (An idiomatic, one-letter variable-name is great; it's mentioned in this very bullet. :-)
    (One test-case you just made was which uses n=50.)
  4. Make a second function to (print and) sum the numbers 0,3,6,9,…,48 (stopping before 50, as before).
    (Hint: the answer should be 3*(16*17/2) = 4081)
  5. Make a further function to (print and) sum the numbers 100,103,106,109,…,148 (stopping before 150).
    (Hint: the answer you get should be 408 + 17*100.)
  6. Finally: Make a function to (print and) sum the numbers in [a,b), counting by k. This function should not print anything — just return the sum.
    All previous functions are one-line calls to this most-general version.
Submit this last function on D2L as lab05a.


1 The 16 occurs because ⌊50/3⌋ = 50/3 = 16. But we mention that just to note that there is a formula; again, for this lab let's practice our loops!      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.