Input the lengths of the legs of a right triangle. (Of course, the lengths may have fractional parts, so we’ll use floating point.) Output the hypotenuse, using the Pythagorean Theorem. The calculation function computes the hypotenuse given the 2 sides, and its prototype should be
double hypotenuse( double side0, double side1 );
The calculation function complains and dies if either arg is negative. An example run of your program (with user input in bold) might go as
leg leg: 3.1 4.1
hypotenuse is 5.140039
Input the total number of eggs our chickens laid today. (Of course, the number of eggs won’t be fractional, so we’ll use a fixed point.) We’re going to fill as many 12-egg cartons as we can. If any eggs are left over, we’ll throw them at our neighbor’s barn. The program outputs the number of cartons and the number of eggs thrown. We’ll have a calculation function that returns the number of cartons given the number of eggs:
unsigned cartons( unsigned eggs );
For instance, if the arg is 1210, cartons returns 100 cartons complains and dies if the number of eggs is more than a million. We’ll also have another calculation function that returns the number of eggs thrown:
unsigned eggsThrown( unsigned eggs );
For instance, if the arg is 1210, eggsThrown returns 10 eggsThrown, complains and dies if the number of eggs is more than a million. An example run of your program (with user input in bold) might go as
total eggs: 1210
We fill 100 cartons and throw 10 eggs.
Another run of your program might go as
total eggs: 0
We fill 0 cartons and throw 0 eggs.
Another run of your program might go as
total eggs: 1000001
Fatal error: too many eggs
Another run of your program might go as
total eggs: Hard boiled, please
Fatal error: input error
It seems to me that making the number of eggs per carton (12) a global constant would be a good way to write this program. I can’t stand global variables, but global constants are fine.
We have a ranch with chickens and goats, and we want to know how many chickens we have and how many goats. We send our assistant out to count how many chickens and how many goats. The assistant (who’s been kicked in the head by a chicken one time too many) returns, saying that instead of counting the number of chickens and the number of goats, he thought it would be more fun to count the number of heads and the number of legs. Write a program that inputs the number of heads (of all the chickens and goats put together) and the number of legs (of all the chickens and goats put together), and outputs the number of chickens and the number of goats. (Each chicken has 1 head and 2 legs, and each goat has 1 head and 4 legs. We’re not running some Dr. Moreau ranch with 7-legged chickens and 3-headed goats.)
You’ll have 2 calculation functions, one that returns the number of chickens, and the other that returns the number of goats:
unsigned chickens( unsigned heads, unsigned legs );
unsigned goats( unsigned heads, unsigned legs );
(For instance, if we had 17 chickens and 12 goats, then we’d have 29 heads and 82 legs. That means that chickens( 29, 82 ) returns 17 and goats( 29, 82 ) returns 12.) In case you’re not in the mood to do the algebra, here are the formulas relating chickens (C), goats (G), heads (H), and legs (L).
H = C + G
L = 2C + 4G
C = 2H - L/2
G = L/2 - H
Both the chickens function and the goats function should complain and die if the number of heads and legs is impossible. For instance, any of the following 3 calls:
chickens( 5, 15 )
chickens( 7, 12 )
goats( 11, 48 )
should complain and die, because you just can’t have a ranch with those numbers of heads and legs. The numbers (5, 15) are impossible because you can’t have an odd number of legs; the numbers (7, 12) are impossible, because if you have 7 heads then you’re sure to have more than 12 legs; the numbers (11, 48) are impossible, because if you have 11 heads then you’re sure to have fewer than 48 legs.
An example run of your program might go as
heads legs: 29 82
That’s 17 chickens and 12 goats.
Another run of your program might go as
heads legs: 0 0
That’s 0 chickens and 0 goats.
Another run of your program might go as
heads legs: 5 15
Fatal error: anatomically impossible
Another run of your program might go as
heads legs: 5 15
Fatal error: anatomically impossible
Another run of your program might go as
heads legs: 11 48
Fatal error: anatomically impossible
Input the number of lines that can be printed on one page, and input the number of lines we want to print. Output the number of pages required.
unsigned pages( unsigned linesPerPage, unsigned totalLines );
For instance,
pages( 60, 121 ) returns 3 (the last page only has 1 line, but that still counts)
pages( 20, 100 ) returns 5 (the last page is full)
linesPerPage must be in [1, 999], or pages complains and dies.
An example run of your program might go as
lines per page: 60
lines to print: 121
3 pages
Input the 3 sides of a triangle, output the area. Your calculation function is
double areaTriangle( double a, double b, double c );
For instance, areaTriangle( 3.0, 4.0, 5.0 ) returns 6.0. (This example happens to deal with a right triangle, but your program should work for triangles of any shape.) The formula for the area of a triangle given the 3 sides a, b, and c, is
Area= √(s(s-a)(s-b)(s-c))
where s (semiperimeter) is defined by
s=(a+b+c)/2
The areaTriangle function should complain and die if the 3 args don’t make a triangle. For instance,
areaTriangle( 5.0, 10.0, 4.0)
should complain and die because you can’t make a triangle with sides 4, 5, and 10. (The rule is that if any side is longer than the sum of the other 2 sides, then you can’t make a triangle.) The args may be present in any order, the function’s not allowed to assume that the caller is putting the args in an agreeable order.
An example run of your program might go as
3 sides: 2 3 4
area is 2.904738
We’re interested in the area of a regular hexagon. (“Hexagaon” means a 6-sided polygon. “Regular” means having all sides the same length and all angles the same value.) The computation function you’ll use is
double areaHexagon( double side );
The arg is the length of any side, and the return value is the area of the hexagon. If the arg is negative, areaHexagon should complain and die. The formula for the area of a regular hexagon given a side S is
Area=3^1.5/2 S^2
For example, areaHexagon( 2.0 ) should return 10.392305.
The program’s job is to input the hexagon’s perimeter (not side) and output the area. (“Perimeter” means the total of all the sides. Since in this program we’re dealing with regular hexagons, the perimeter is just 6 times the length of a side.) If a regular hexagon has sides of length 2, then the perimeter is 12, so an example run of your program might go as
perimeter: 12
area is 10.392305
The most famous equation from Einstein’s theory of relativity is
E=MC^2
This says that there is a kind of equivalence between mass and energy.
M is the number of kilograms of mass. (One kilogram is about 2.205 pounds.)
E is the number of joules of energy. (One joule is the amount of energy expended in one second by a 1-watt light bulb, so a 100-watt light bulb expends 360000 joules of energy in an hour.)
C is the speed of light in meters per second. The speed of light is exactly 299792458 meters per second.
Your calculation function is
double mass( double joules );
The arg is the number of joules of energy, and the return value is the number of kilograms of the equivalent mass.
The mass function complains and dies if the arg is negative.
An example run of your program might go as
joules of energy: 3.85e26
That’s equivalent to 4283702715.806431 kilograms.
(Every second our sun emits about 3.85×〖10〗^26 joules of light energy. According to Einstein, that means that the light emitted by our sun causes it to become about four billion kilograms lighter every second.) It seems to me that making the speed of light a global constant would be a good way to write this program.
We’re interested in converting a temperature from Fahrenheit (F) to Celsius (C). The formula relating the 2 temperature scales is
F=(9/5)C+32
Your calculation function is
double celsius( double fahrenheit );
For instance, celsius(212) returns 100, because the temperature of boiling water is 212°F, which is 100°C.
It’s impossible for anything to be colder than absolute zero, which is -459.67 degrees Fahrenheit. If the arg is less than -459.67 degrees, celsius should complain and die. An example run of your program might go as
Fahrenheit degrees: 98.6
That’s 37.000000 degrees Celsius
(That’s the body temperature of an average healthy human adult.)
For some reason we want to compute the sum of the digits of a 6-digit number. Your calculation function is
unsigned digitSum( unsigned n );
If n is 1000000 or greater (more than 6 digits), digitSum should complain and die. Otherwise, it returns the sum of the digits of n. For instance, digitSum( 91371 ) returns 21. Since you know that there can’t be more than 6 digits, you can either write the function with a loop (which is fine), or by just adding 6 expressions, one for each digit (which is also fine). An example run of your program might go as
Gimme a 6-digit #: 123456
sum of digits is 21
It seems to me that a good way to write this function would be to define a local constant BASE that is set to 10. That way, if we ever want to change the function so that it computes the sum of the digits in some other base, we’d only have to change the constant BASE to some other value.
The formula for the volume of a sphere given the radius r is
Volume=(4/3)πr^3
Your calculation function is
double volume( double radius );
π is 3.14159265358979323846
If the arg is negative, the volume function should complain and die. Your program inputs the radius and outputs the volume. An example run of your program might go as
radius: 4000
volume is 268082573106.329010
(The radius of the earth is about 4 thousand miles, so the volume of the earth is about 268 billion cubic miles.) It seems to me that making π a global constant would be a good way to write this program.