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

h06: Homework 6

ready? assigned due points
true Tue 10/11 02:00PM Thu 10/13 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/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Read Chapter 3.4 - 4.3 (If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”).

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

    1. (2 pts) What is a flag in a program and of what use is it?
    2. (2 pts) What is variable tracing and how can it be of value to a programmer?
    3. (2 pts) In C++, how would you calculate the absolute value of the fraction -5/6?
    4. (2 pts) In C++, how would you calculate the absolute value of (-19 + 4 + 10)?
    5. (2 pts) In C++, how would you generate a random number between 0 and 100?
    6. (2 pts) In C++, how would you generate a random number between 5 and 15?
    7. (4 pts) List at least 8 pre-defined functions in C++ that come from the cmath library.
    8. (4 pts) Look up cstdlib, which is another popular C++ library. What seems to be the biggest difference between cstdlib and cmath?
    9. (6 pts) Write out a short program that simulates the tossing of 2 dice. When run, the program will print out 2 numbers representing 2 randomly thrown dice and then quits. Are the 2 numbers always different every time the program is run?
    10. (6 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;
    }
    
    11. (8 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's approaching? What is the smallest value of k for s to be shown by the program as exactly equal to that whole number?