1
h04
CS16 F17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone")

h04: More Loops and Functions

ready? assigned due points
true Thu 10/19 02:00PM Thu 10/26 02:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

    1. (20 pts) The infinite series: s = 1 + (2/3) + (4/9) + (8/27) + .... is a geometric series that converges to a whole rational number (i.e. like 2 or 3 or 4). Below is an unfinished C++ program that will calculate s to the kth position (so, for example, if k = 1, then s = 1 + (2/3) = 1.666...). Fill in the missing code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        double s(0.0);
        int k(0);
        cout << "Enter k: ";
        cin >> k;
        for (____________________________________________________) { 
        
            s = ____________________________________________________; }
            
        cout << "Series converges to: " << s << endl;
        return 0;
    }
    
    2. (20 pts) Compile and run the above completed program. What is the value of s if k = 5? if k = 20? What whole rational number does s look like it is approaching? What is the smallest value of k for s to be shown by the program as exactly equal to that whole number?
    3. (20 pts) Go to this URL and save the program called stars.cpp: http://cs.ucsb.edu/~zmatni/cs16f17/hwk/stars.cpp
    Read the program and try to understand what it does. You can compile and run it as well to see what it does: in the program, the function, boxOfStars, returns a string that, when printed, yields a box of stars of width w and height h. The function, lineOfStars, is a function that returns a string that when printed, yields a string containing a sequence of stars of length len, without a new line character.
    Following the examples on pages 160-161 and 208-211 (in either the 9th or 10th edition textbook), write a new definition of the function boxOfStars that does NOT contain an explicitly nested loop. Instead, it should have a call to the function definition of lineofStars to accomplish the same goal as the original program
    You can test what you’ve done by compiling and running your changed code. Submit the program you wrote by printing it (it should all fit on one page) and submitting that paper with the other homework sheet.
    4. (20 pts) For this question and the following one, you can write the answers on the extra sheet that you are submitting to answer the programming question #3. Consider the ORIGINAL version of boxOfStars that has a nested loop above. Suppose that we made a mistake in the inner loop variable in the boxOfStars function, and instead of
    for (int j = 0; j < width; j++)
    we wrote:
    for (int j = 0; j < width; i++)
    (note the mistake—incrementing i as in Ivan in this loop header instead of j as in Jill.) What would happen, and why?
    5. (20 pts) Consider the example of the NEW boxOfStars program as re-written by you to use the lineOfStars function (i.e. in the CHANGED program that you submitted for question #6). Suppose that we changed the loop variable in the lineOfStars function from j (as in "Jill") to i (as in "Ivan"). Would there be any problem with that? If so, what? If not, explain why not, especially in light of what we’ve discussed about the scope of variables.