CSCI 3300
Fall 2013
Exercises for Quiz 1

  1. What is the value of C++ expression 5 * 4 - 6 * 3 - 9?

    1. -7
    2. -19
    3. 1
    4. 11
    5. 33
    Answer

  2. What is the value of C++ expression 9/4 + 1?

    1. 1.25
    2. 2
    3. 2.25
    4. 3
    5. 3.25
    Answer

  3. Function g is defined below. What is the value of expression g(3)?

    1. 0
    2. 6
    3. 9
    4. 3
    5. 12

  4.   int g(int n)
      {
        if(n > 3) return 0;
        else return 2*n+3;
      }
    Answer

  5. Using function g from the preceding exercise, what is the value of expression g(g(3))?

    1. 0
    2. 9
    3. 18
    4. 21
    5. 25
    Answer

  6. Suppose that variable y, of type double, already exists and has a value. Write a statement or sequence of statements that (1) creates a variable called frame of type double, and (2) makes frame hold value (y2 - 7y + 4)/9. Be sure to use C++ notation correctly. The expression shown is not in C++ notation, but in standard mathematical notation. Answer

  7. Suppose that variable u has type double, and has already been given a value. Write a statement or statements that create variable m, of type double, and make m hold the absolute value of u. For this exercise, do not use the C++ abs function, or any close relative of it. (The absolute value of 4.0 is 4.0, and the absolute value of -4.0 is 4.0.) Answer

  8. Using your solution to the preceding exercise, write a definition of function absoluteValue(u), where u has type double. It should return the absolute value of u. Answer

  9. You have an integer variable n, which already has a value. Write a C++ statement that creates integer variable y and makes y hold the absolute value of n-1. Use your function absoluteValue to do the absolute value computation. Answer

  10. A contract of a function

    1. tells exactly what the function does, and what it requires from its caller.
    2. tells how the function works.
    3. tells where the function fits into the overall solution of a problem.
    4. tells why the function was written.
    Answer

  11. Write a C++ definition of function seconds(ms), which takes an integer value ms representing some number of milliseconds (thousandths of a second) and returns a real number that is ms in seconds. For example, seconds(2001) = 2.001. Answer

  12. You have two integer variables n and m each holding a number of milliseconds. Write a C++ statement or sequence of statements that creates variable y, of type double, and makes y hold the number of seconds that is equivalent to n + m milliseconds. Use your function from the preceding problem to do the calculation. Answer

  13. Write a C++ definition of function sum(a, b) that takes two integers a and b and returns the sum a + (a + 1) + (a + 2) + ... + b. For example, sum(9,14) = 9 + 10 + 11 + 12 + 13 + 14. If b < a, then sum(a,b) should return 0. Use a loop to do this.

    (Hint. You will need two variables, one to count from a to b and the other to keep track of how many of the sum.)

    Answer

  14. You are given a function prime(n), which returns true if n is a prime number and false if n is not prime. (Do not write prime; just use it.)

    Write a C++ definition of function countprimes(a, b), which returns the number of primes that are greater than or equal to a and less than or equal to b. For example, countprimes(4,11) = 3, since the prime numbers from 4 to 11 are 5, 7 and 11, and there are three of them. Use a loop to do this.

    (Hint. You will need two variables, one to count from a to b and the other to keep track of how many of them are prime. Use the prime function to ask whether a number is prime. Keep your definition simple.)

    Answer