Skip to main content

Exercises 12.18 Exercises

This chapter does not have automatically graded exercises like other ones. Instead, it includes a series of open-ended questions and prompts for further exploration.

1.

In the development environment of your choice, set up a multiple file project. It should consist of:
  • A library (either a module file or a .h/.cpp pair) that contains a simple function like double celsiusToFahrenheit(double celsiusTemp);.
  • A file with tests for that function.
  • A file with a main function.
Make sure that you can compile and run both the tests program and the main program.

2.

Design functions to help solve the following problem.
Given two blood types like AB+ and O-, we want to determine if the first blood type can donate to the second blood type and if the second can donate to the first.

Note 12.18.1. How blood types work.

Blood that has type A antibodies (A or AB) can only be given to people with an A or AB blood type. Blood that has type B antibodies (B or AB) can only be given to people with a B or AB blood type. Blood that has type O antibodies can be given to anyone.
Blood that has the + factor can only be given to someone who also has the + factor. (Blood with the - factor can be given to anyone.)
Our input will be two strings. We should print out two lines of output like:
AB+ can donate to O-: false
O- can donate to AB+: true
Using either top-down or bottom-up design, design some functions that might be useful for solving this problem. Don’t worry about doing the math or writing code, just focus on what tasks need to get done. For each function, write:
  • A prototype for the function
  • Some sample input/output pairs that could turn into a test

3.

Design functions to help solve the following problem.
We would like to calculate blends of HTML colors in the format #RRGGBB where RR, GG, and BB are two-digit hexadecimal numbers. (See Welcome to CS - Hex Data & Colors for more details).
Our input will be lines of text like #406000 #AAFFBB 80% that have two colors and a percentage. The percentage will describe how much of the second color to use. So #406000 #AAFFBB 80% would mean to give the color #AAFFBB 80% weight and the color #406000 20% weight. We should print a new string in the format #RRGGBB that represents the blended color.
Using either top-down or bottom-up design, design some functions that might be useful for solving this problem. Don’t worry about doing the math or writing code, just focus on what tasks need to get done. For each function, write:
  • A prototype for the function
  • Some sample input/output pairs that could turn into a test
You have attempted of activities on this page.