Here's your very first CS 1321X homework assignment. Curiously,
it looks very much like last year's very first CS 1321X assignment
(but not entirely), and it adds a little bit of last year's second
assignment as well. It's not very big, but it's enough to give you
some respectable practice with the fundamentals we've covered so far
in class. (If you want more practice, just ask your friendly teaching
assistants...they'll be glad to send more problems your way.) Everything
you need to know to solve these problems has been covered in the
first four lectures. It looks simple, and it really is, but
give yourself enough time to get Dr. Scheme downloaded, figure
out how Dr. Scheme works (or doesn't) on your computer, figure out
how to get your solutions turned in, ask TAs or me whatever questions
you have, and so on. In other words, don't wait until the last
minute. Trust me.
Always keep in mind that you're programming for people, not for the
benefit of the computer. Write your code so that other people can
understand it easily. We'll talk more about this issue in the days
to come. On this assignment, your solutions will be so tiny that
all we ask is that you use indentation to make your functions look
pretty and that you use meaningful names. When we tell you to give
your function a specific name, use that name exactly. You may use
only the following predefined Scheme functions in constructing your
solutions to the these problems:
< > = <= >= + - * / and or not if cond define
We haven't introduced COND yet, but a few of you already know about it,
so go ahead and use it if you like, so long as you use it correctly.
And we haven't talked about logical operators like AND, OR, and NOT, but
if you know what these things are used for and you think you need them,
feel free to do so.
1) Let's start with something real easy. Construct a function called
CUBE-IT which takes one argument, a number, as input and returns the
cube of that number.
example:
> (cube-it 3)
27
> (cube-it 1.414)
2.8271459439999993
>
2) Let's get slightly more complicated. Define a function called
HEIGHT-IN-INCHES which takes two arguments as input. These
arguments represent a person's height in feet and inches as follows:
if, for example, the person is 5 feet 10 inches tall, the first
argument would be 5 and the second argument would be 10. If the
person is exactly 6 feet tall, the first argument would be 6 and
the second argument would be 0. This function returns one number
which represents the person's height in inches.
example:
> (height-in-inches 5 10)
70
>
3) Feeling a tad pudgy? According to some sources, you can
determine if you're overweight by plugging the appropriate
values into this formula:
704 * your weight in pounds
INDEX = -------------------------------
2
(your height in inches)
If the value of INDEX is 25 or less, you're in great shape.
If the value of INDEX is 30 or more, you're at significant
risk for heart disease and other weight-related problems
(if you believe the formula).
Define a function called INDEX which takes three arguments as input.
The first argument is a number representing a person's weight in
pounds. The second and third arguments represent a person's height
as described in problem 2. The function returns the value indicated
by the formula above.
examples:
> (index 190 6 0)
25.80246913580247
> (index 190 5 10)
27.29795918367347
>
4) If we use a little algebra, we can rewrite the formula above to
give us something we can use to compute our ideal weight. Assuming
that an index of 25 is optimal, we can then find the ideal weight
as follows:
2
25 * (your height in inches)
IDEAL-WEIGHT = ---------------------------------
704
Define a function called IDEAL-WEIGHT which takes two arguments
as input. These two arguments represent the person's height in
feet and inches as described earlier in problem 2. The function
returns a numeric value representing the person's ideal weight
as calculated by the re-engineered formula just a few lines above.
examples:
> (ideal-weight 5 10)
174.0056818181818
> (ideal-weight 6 2)
194.46022727272728
>
5) When converting between temperatures in degrees Fahrenheit and
degrees Celsius, it is useful to note that -40 degrees Fahrenheit
equal -40 degrees Celsius. This observation makes for the following
symmetric conversion formulas:
C = (F + 40) * 5/9 - 40
F = (C + 40) * 9/5 - 40
Construct conversion functions, F-TO-C and C-TO-F, using these
formulas. Each function takes exactly one argument. The F-TO-C
function takes a number representing a temperature in Fahrenheit and
returns a number representing the same temperature in Celsius. The
C-TO-F function takes a number representing a temperature in
Celsius and returns a number representing the same temperature in
Fahrenheit.
Examples:
> (f-to-c 32)
0
> (f-to-c 98.6)
37.0
> (c-to-f 100)
212
The solutions to the problems given above don't need recursion (and if
you came up with a recursive solution for any of them, you might want
to go back and reread the problem). The solutions to the problems
below will involve recursion.
6) Write a function called DIGITS that will be passed a positive
integer and return the number of digits in that integer.
For example:
(digits 12345) -> 5
(digits 3621) -> 4
7) Write a function called MY-REMAINDER that mimics the functionality
of the built-in REMAINDER function. The function takes two
arguments, the dividend and the divisor, and returns the
remainder after dividing the dividend by the divisor. For
example:
(my-remainder 10 3) -> 1
(my-remainder 3 10) -> 3
Here's the catch: you can't use the built-in REMAINDER function, and
just to be evil we're taking away the use of the built-in division
function commonly known as /.
If you need additional functions to complete this problem, you must
create them yourself. Also, keep in mind that this function must
work for both positive and negative values passed as the dividend
or the divisor.
8) The greatest common divisor (GCD) of two positive integers is
the largest number that evenly divides both of them. For example,
the GCD of 18 and 12 is 6. In about 300BC, Euclid published an
algorithm for calculating the GCD of two numbers. Renowned
computer scientist Donald Knuth has described this algorithm as
"the granddaddy of all algorithms, because it is the oldest
nontrivial algorithm that has survived to the presend day."
Here is a brief description of Euclid's algorithm:
Given two positive integers, u and v
1. Does v divide u evenly (i.e., does u mod v = 0)?
If so, the GCD is v. If not, continue to step 2.
2. Replace u with the current value of v. Replace v
with the remainder of u/v. Go back to step 1
using the new values of u and v.
Write a function called MY-GCD which uses Euclid's
algorithm as described above to calculate the GCD of two
positive integers. You may not use the predefined
Scheme function named GCD.
9) Write a function called SUM-BETWEEN that will be passed two positive
integers and return the sum of all integers (inclusive) between the two
given integers. You may assume that the first number will be
less than or equal to the second. For example:
(sum-between 2 5) -> 14
(sum-between 2 2) -> 2
10) Write a function called MY-EXPT that mimics the functionality
of the built-in EXPT function. The function takes two
arguments and returns the value obtained by raising the first
argument to the power of the second argument. Assume that the
power (the second argument) will be a positive number, greater
than or equal to zero. For example:
(my-expt 2 5) -> 32
(my-expt 5 2) -> 25
(my-expt -2 5) -> -32
Of course, you can't use the predefined EXPT function.
Copyright (c) 2003 by Kurt Eiselt. All rights reserved, with
the exception of stuff that belongs to somebody else.
Last revised: August 27, 2003