Search

2.10 — Chapter 2 comprehensive quiz

Quick Review

Integers are used for holding whole numbers. When using integers, keep an eye out for overflow and integer division problems. Use the int type when the size of an integer doesn’t matter. Use fixed-width integers when the precise size of an integer is important (either due to range or memory usage concerns).

Floating point numbers are used for holding real numbers (which can have fractional components). When using floating point numbers, keep an eye out for precision issues, rounding errors, and comparison issues.

Boolean values hold only true and false. They do not have any major issues.

Char values are integers that can be interpreted as an ASCII value. When using chars, be careful not to mix up ASCII code values and numbers, and watch for overflow and integer division problems.

Use the const keyword to declare symbolic constants instead of #define. It’s safer.

Comprehensive quiz

Question 1

Why are symbolic constants usually a better choice than literal constants? Why are const symbolic constants usually a better choice than #defined symbolic constants?

Show Solution

Question 2

Pick the appropriate data type for a variable in each of the following situations. Be as specific as possible. If the answer is an integer, pick either int, long, or a a specific fixed-width integer type (e.g. int16_t) based on range. If the variable should be const, say so.

a) The age of the user (in years)
b) Whether the user wants color or not
c) pi (3.14159265)
d) The number of pages in a textbook (assume size is important)
e) The price of a stock in dollars (to 2 decimal places)
f) How many times you’ve blinked since you were born (note: answer is in the millions)
g) A user selecting an option from a menu by letter
h) The year someone was born (assuming size is important)

Show Solution

Question 3

Write the following program: The user is asked to enter 2 floating point numbers (use doubles). The user is then asked to enter one of the following mathematical symbols: +, -, *, or /. The program computes the answer on the two numbers the user entered and prints the results. If the user enters an invalid symbol, the program should print nothing.

Example of program:

Enter a double value: 6.2
Enter a double value: 5
Enter one of the following: +, -, *, or /: *
6.2 * 5 is 31

Hint: You can check if the user has entered a plus symbol using an if statement, covered in section 2.6 -- Boolean values.

Show Solution

Question 4

This one is a little more challenging. Write a short program to simulate a ball being dropped off of a tower. To start, the user should be asked for the initial height of the tower in meters. Assume normal gravity (9.8 m/s2), and that the ball has no initial velocity (the ball is not moving to start). Have the program output the height of the ball above the ground after 0, 1, 2, 3, 4, and 5 seconds. The ball should not go underneath the ground (height 0).

Your program should include a header file named constants.h that includes a namespace called myConstants. In the myConstants namespace, define a symbolic constant to hold the value of gravity (9.8). See section 2.9 -- Symbolic constants and the const keyword for a reminder on how to do this.

Use a function to calculate the height of the ball after x seconds. The function can calculate how far the ball has fallen after x seconds using the following formula: distance fallen = gravity_constant * x_seconds2 / 2

Sample output:

Enter the initial height of the tower in meters: 100
At 0 seconds, the ball is at height: 100 meters
At 1 seconds, the ball is at height: 95.1 meters
At 2 seconds, the ball is at height: 80.4 meters
At 3 seconds, the ball is at height: 55.9 meters
At 4 seconds, the ball is at height: 21.6 meters
At 5 seconds, the ball is on the ground.

Note: Depending on the initial height, the ball may not reach the ground in 5 seconds -- that’s okay. We’ll improve this program once we’ve covered loops.
Note: The ^ symbol isn’t an exponent in C++. Implement the formula using multiplication instead of exponentiation.

Show Solution

3.1 -- Operator precedence and associativity
Index
2.9 -- Const, constexpr, and symbolic constants

390 comments to 2.10 — Chapter 2 comprehensive quiz

  • RryanT

    This is how I did question 3. Is there anything wrong?

  • Will

    After rading it all cant do question 3, let alone question 4. Not sure if coding is for me 🙁

    • MikeL

      If this is your first time programming, then don’t worry. Try your best and then look at the answer until you really understand it. Also, if you think about anything else that is not clear, make changes to your program to see what happens.
      This is the way that everyone learns, so don’t get too discouraged!

  • Weckersduffer

    Why it doesn´t runs?

    • nascardriver

      Hi Weckersduffer!
      You’re missing semicolons in line 10 and 11.
      In line 4 you need to add the data type of sec.

      Also, the ^ operator is not the power function you’re used to from math, use sec * sec instead.

      • Weckersduffer

        Thanks, but I continue having errors. Maybe the "calc" function?

        • nascardriver

          Your compiler will output an error message. For your code it will output something like

          It tells you where the error is: "function main, line 16, column 78".
          It tells you what the error is: "I don’t know what calcu is".

          • Weckersduffer

            The error message still appears

            • nascardriver

              Your code compiles for me. What is the error message?

              • Weckersduffer

                The same as before:

                I use Code::blocks

                • nascardriver

                  That’s odd, did you save the file before compiling? It looks like at one point you spelled calc with a capital ‘C’ and the compiler is trying to compile that version.

  • merocom

    Enter the height of the tower in meter:
    100
    At 0 seconds, the ball is at height: 100 meters at speed: 0 m/s.
    At 1 seconds, the ball is at height: 95.095 meters at speed: 9.81 m/s.
    At 2 seconds, the ball is at height: 80.38 meters at speed: 19.62 m/s.
    At 3 seconds, the ball is at height: 55.855 meters at speed: 29.43 m/s.
    At 4 seconds, the ball is at height: 21.52 meters at speed: 39.24 m/s.
    At 5 seconds, The ball is on the ground at max speed is: 49.05 m/s.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter