1
h04
CS16 M18
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: Predefined and programmer defined functions

ready? assigned due points
true Tue 07/17 08:00AM Tue 07/24 11:00AM

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,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores).


Reading: Sections 4.1 - 4.3

  1. (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
    • Filling in your name and umail address.
    • Also: For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.
  2. (6 pts) What is a flag in a program and of what use is it?
  3. (6 pts) What is type casting and how is it performed in C++?
  4. (8 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)
    
  5. (10 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  }
    
  6. (10 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?