1
h04
CS16 F19
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
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: Chapter 4: Predefined and programmer defined functions

ready? assigned due points
true Thu 10/10 09:00AM Thu 10/17 12:30PM

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

UPLOAD A PDF OF YOUR ANSWERS TO GRADESCOPE BEFORE THE DUE DATE. ASSOCIATE EACH QUESTION WITH A SPECIFIC PAGE IN YOUR HOMEWORK AT THE TIME OF SUBMISSION. There is NO MAKEUP for missed assignments;


Please:

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

Read Chapter 4, sections 4.1 - 4.3. Please turn in your homework in lecture.

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

    1. (4 pts) What is type casting and how is it performed in C++?
    2. (4 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)
    
    3. (6 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  }
    
    4. (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;
    }
    
    5.(10 pts) Write a function declaration and a function definition for a function named 'check_descending' that takes three arguments of type int and returns true if the arguments are in descending order; otherwise, it returns false. You can use any type of C++ statements that we've gone over in class so far to accomplish this goal.