Skip to main content

Exercises 12.18 Exercises

Guidelines for Design Exercises.

This chapter does not have automatically graded exercises. Design, by its nature, is open-ended. It does however have some exercises that ask you to design functions. Here are some guidelines for how to answer those exercises:
Do so by writing:
  • A prototype for each function.
  • A description of what the function does (written as a comment).
  • Some sample input/output pairs that could turn into a test.

Example 12.18.1. Example Exercise/Answer.

We want to write a program that takes a variable definition in snake_case and converts them to camelCase. So, if we started with the input int my_nice_variable;, we would want to output int myNiceVariable;. The declaration could also have an initial value, like double another_variable_name = 3.14;. If it does, there will always be a space between the variable name and the assignment operator.
Design some functions that would help solve this problem.
Listing 12.18.1. Sample answer
// Function 1
// gets the variable name from a declaration statement
string getVariableName(const string& declaration);
// Sample uses
getVariableName("int my_nice_variable;") -> "my_nice_variable"
getVariableName("double another_variable_name = 3.14;") -> "another_variable_name"

// Function 2
// converts a snake_case variable name to camelCase
string snakeToCamel(const string& snakeName);
// Sample uses
snakeToCamel("my_nice_variable") -> "myNiceVariable"
snakeToCamel("another_variable_name") -> "anotherVariableName"

// Function 3
// convert a single character to uppercase
char toUpperCase(char c);
// Sample uses
toUpperCase('a') -> 'A'
toUpperCase('z') -> 'Z'

// Function 4
// takes a full variable declaration in snake_case and returns it in camelCase
string convertDeclarationToCamel(const string& declaration);
// Sample uses
convertDeclarationToCamel("int my_nice_variable;") -> "int myNiceVariable;"
convertDeclarationToCamel("double another_variable_name = 3.14;") -> "double anotherVariableName = 3.14;"
Note that we have not described how any of the functions do their job. We have simply identified useful tasks and written down what inputs and outputs they should have. Not all of these functions might be needed. (For example, we could probably just use toupper from <cctype> instead of writing Function 3). And we might need to break up some of the functions more.

1.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
We will start with 6 doubles, x1, y1, x2, y2, x3, and y3, that represent the coordinates of the three vertices of a triangle. We want to compute and print the area of the triangle.

2.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
We have a string containing two cards of a hand in blackjack, like "KH 7D" (the king of hearts and the seven of diamonds) or "AS 9C" (the ace of spades and the nine of clubs). We want to compute the total value of the hand. The king, queen, and jack are worth 10 points each. The ace is worth either 1 point or 11 points, whichever is more favorable to the hand without going over 21. All other cards are worth their numeric value.

3.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
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.2. 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.)
We will write a program that asks for two blood types. We should print out two lines of output like:
AB+ can donate to O-: false
O- can donate to AB+: true

4.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
Given a month/year date like "12/2025" or "1/2023", print a calendar page for that month and year.
For example, a calendar page for December 2025 might look like this:
    December 2025
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

5.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
In Roman numerals the letters I, V, X, L, and C represent the values 1, 5, 10, 50, and 100 respectively. Numbers are formed by combining these letters and adding their values. For example, the Roman numeral for 3 is III, which is three ones added together. The Roman numeral for 12 is XII, which is a ten plus two ones. The Roman numeral for 27 is XXVII, which is two tens plus a five plus two ones.
In real Roman numerals, there are some special rules for avoiding more than three of the same letter in a row. But we will ignore those.
We want to write a program that reads in two Roman numerals (both less than 100), adds them together, and outputs the result as a Roman numeral.

6.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
The perceptual brightness of a color can be estimated with the formula:
\begin{equation*} \text{brightness} = (0.2126 * R) + (0.7152 * G) + (0.0722 * B) \end{equation*}
Given a string with a color value in the format #RRGGBB where RR, GG, and BB are two-digit hexadecimal numbers. (See Welcome to CS - Hex Data & Colors for more details), we want to calculate the perceptual brightness.

7.

Design functions to help solve the following problem. Use the answer guidelines in Guidelines for Design Exercises.
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.
You have attempted of activities on this page.