Each problem description includes a "sample program execution" showing a dialog between the user and a program that solves the problem correctly. In each such dialog, text that is underlined corresponds to input provided by the user. Text that is not underlined corresponds to output (including user prompts) produced by the program.
The first two terms in the Fibonacci sequence are 0 and 1, respectively, and each subsequent term is the sum of the previous two. Using this definition to calculate the first several terms in the sequence, we get
Let us define the Fibonacci roots of a positive integer n to be the two smallest consecutive Fibonacci numbers whose sum is greater than or equal to n.
Develop a program that, given as input a positive integer, produces as output its Fibonacci roots. The program should repeat until the user enters zero as input.
Sample Program Execution:
Enter input: 31 Fibonacci roots: 13 21 Enter input: 89 Fibonacci roots: 34 55 Enter input: 0 |
In statistics, the mode of a collection of values is defined to be any value that occurs with highest frequency in the collection. The mode need not be unique, as there may be two or more values that occur with the same highest frequency.
Develop a program that, given as input a collection of integers, lists any that qualify as a mode of the collection. The program should repeat until the user indicates that the collection of numbers to be analyzed is empty.
Sample Program Execution:
Enter number of items in the collection: 16 |
A set is collection of elements, or members. A set S is said to be a subset of the set T if every member of S is also a member of T. S is said to be a proper subset of T if S is a subset of T and, in addition, at least one of the members of T is not a member of S.
Develop a program that determines whether one set of identifiers (strings containing only upper case letters) is a proper subset of another. The user enters the identifiers in each set all at once, with the identifiers separated by commas and with the entire list enclosed in parentheses. No set given as input will contain more than ten members, and no identifier will be longer than twelve characters. The output is simply YES or NO, the former if the second set is a proper subset of the first and the latter otherwise. The program should repeat until the user enters as a first set one containing no members.
Sample Program Execution:
Enter 1st set: (APPLE,GRAPE,PEAR,ORANGE,GRAPEFRUIT) Enter 2nd set: (ORANGE,GRAPE) YES Enter 1st set: (APPLE,GRAPE,PEAR,ORANGE,GRAPEFRUIT) Enter 2nd set: (ORANGE,BANANA,GRAPE) NO Enter 1st set: () |
When contractors lay floor tile in a rectangular room, they should make sure that each tile on the perimeter has the same size as the one on the opposite side of the room. This gives a more appealing look to the final result.
Develop a program that, given as input
Partial tiles are ones that have been cut in order to reduce either their length, their width, or both. Such tiles are to be laid only on the perimeter of the floor. (The only tiles that need to be cut twice —in order to reduce both length and width— are corner tiles, the number of which is always either zero or four.)
Tiles are to be laid out so that grout gaps exist only between adjacent tiles, and never at the perimeter of the floor area. Thus, the number of tiles in any "row" or "column" is always exactly one greater than the number of grout gaps.
Sample Program Execution:
Enter floor length: 25.0 Enter floor width: 19.0 Enter length/width of tiles: 4.75 Enter grout gap: 0.25 12 full tiles 6 length-reduced tiles of length 2.375 8 width-reduced tiles of width 1.875 4 corner tiles Enter floor length: 324.5 Enter floor width: 200.0 Enter length/width of tile: 6.0 Enter grout gap: 0.5 1500 full tiles 0 length-reduced tiles 100 width-reduced tiles of width 2.25 0 corner tiles Enter floor length: 128.8 Enter floor width: 171.8 Enter length/width of tile: 4.1 Enter grout gap: 0.2 1200 full tiles 0 length-reduced tiles 0 width-reduced tiles 0 corner tiles Enter floor length: 0.0 |
Develop a program that accepts as input two hexadecimal numbers and that calculates and outputs their sum (which is also to be expressed as a hexadecimal number). Each input will have no more than six digits. The program should repeat until both inputs are zero.
The hexadecimal (i.e., base 16) number system is analogous to the more familiar decimal (i.e., base 10) system in that both are positional number systems, meaning that the "contribution" made by each digit in a number depends not only upon its value but also upon its position within the number. Where they differ is that the hexadecimal system employs sixteen distinct one-digit numbers rather than only ten.
In the decimal system, we use the ten digits 0, 1, 2, ..., 9.
Going from right to left, the positions in a number correspond to
units of 1, 10, 100, 1000, etc.
(Notice that these are the powers of 10: 100 = 1,
101 = 10, 102 = 100,
103 = 1000, etc.)
The expression 45038, for example, is to be understood to mean
The hexadecimal number system is just the same, except that it employs not only 0 through 9 as digits, but also symbols that represent the values 10 through 15. By convention, these symbols are 'A' (for 10), 'B' (for 11), ..., and 'F' (for 15). The positions in a hexadecimal number correspond to the powers of 16, namely 1 (160), 16 (161), 256 (162), etc.
Thus, for example, 5B3C16 (the subscript indicates that the
number is to be interpreted as being in base-16) is to be
understood to mean
It should not be surprising that the procedure for adding two decimal numbers (which you learned in elementary school) applies to hexadecimal numbers as well.
Indeed, just as with decimal numbers, we "line up" the two addends (so that digits in corresponding positions appear one above the other) and proceed from the rightmost (i.e., least significant) to the leftmost (i.e., most significant) position. (If one number has more digits than the other, we pad the latter with leading zeros.)
For each position, we add the incoming carry to the sum of the two digits in that position (one for each addend), yielding a value in the range [0..31] (which in hexadecimal is the range [0016 .. 1F16] The digit of lesser significance goes into the corresponding position of the result; the digit of greater significance (which is always either zero or one) is carried into the next position.
Sample Program Execution:
1st number: 6D3 2nd number: 32A Sum: 9FD 1st number: 713 2nd number: 62B Sum: D3E 1st number: 94A7B 2nd number: 995 Sum: 95410 1st number: E3 2nd number: 4A Sum: 12D 1st number: 0 2nd number: 0 |
Write a program that, given as input
Reminder: There are 5280 feet in a mile and 3600 seconds in an hour.
Sample Program Execution:
Enter distance between checkpoints: 50.0 Enter time between checkpoints: 0.6 Enter speed limit: 55.0 SPEEDING Enter distance between checkpoints: 2.5 Enter time between checkpoints: 0.035 Enter speed limit: 55.0 NOT SPEEDING Enter distance between checkpoints: 0.0 |