1
h07
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"

h07: Homework 7

ready? assigned due points
true Thu 10/13 02:00PM Thu 10/20 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 thru 4.5 (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!
FOR BEST RESULTS, PRINT THIS PAGE AS A PDF, THEN PRINT THE PDF

1.(5 pts) Which of these uses of type casting will NOT ensure that f is 1.5? Answer should be (ex1), (ex2), (ex3), or (ex4) (or a combination of those).

int a(1), b(2), c(2), d(2), e(2);
double f;

f = (a + b)*c / static_cast<double>(d + e);	// (ex1)
f = static_cast<double>(a + b)*c / (d + e);	// (ex2)
f = (a + b)*static_cast<double>(c) / (d + e);	// (ex3)
f = static_cast<double>((a + b)*(c) / (d + e));	// (ex4)

2.(3 pts) We talked about three concepts that are very important to keep straight, and not confuse: (a) function declaration, (b) function definition, and (c) function call. Here is a short C++ program, with line numbers. Please indicate after the program which line number (or range of line numbers, e.g. 3-5 or 7-14) contains the function prototype, function definition, and function call for the isDivisibleBy function.

1  #include <iostream>
2  using namespace std;
3
4  bool isDivisibleBy(int a, int b);
5
6  int main() {
7     cout << "result for (15,5) is " << isDivisibleBy(5,15) << endl;
8     cout << "result for (15,5) is " << isDivisibleBy(5,15) << endl;
9     return 0;
10  }
11
12  bool isDivisibleBy(int a, int b) {
13    return ( a % b == 0 );
14  }

3.(8 pts) Write a function declaration and a function definition for a function that takes one argument of type int and one argument of type double, and that returns a value of type double that is the average of the two arguments?

4.(12 pts) First, re-read the nested loop section on pages 160-161, and read the text and code on pages 208-211. Next, go to this URL and save the program called x stars.cpp: http://cs.ucsb.edu/~zmatni/cs16/hw7/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 example from the 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.

5.(6 pts) 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?

6.(6 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 #4). 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.