Homework assignment 1

CS1311X Spring 2001
Due 8:00 AM, Friday, January 19, 2001

Objectives:

Reading:

For this assignment you should read through section 3.10.

Problem 1
5 points

Explain how computer engineering is different from computer science.

Problem 2
10 points

List the three main computer programming paradigms. Which one of these does scheme belong to?

Problem 3
5 points

What is a programming language?

Problem 4
10 points

List the three main benefits of functional programming.

Problem 5
5 points

What symbols does Scheme use to represent the values true and false?

Problem 6
10 points

Scheme uses a notation called "prefix notation". Explain what this means.

Problem 7
15 points

Express each of the following arithmetic expressions as a Scheme expression. Be sure to preserve the standard order of operations. (note: The symbol '*' denotes multiplication)

a) 9 + 5 * 16 + 11
b) 4 + 28 / (2 + 7)
c) 3.14 * (1 - 0.76) + -11.75
d) (8 + 2 + 7 + 4 + 6) * 3
e) (1 + 4 + 6 + 7) / (1.7 + 1 - 19)
EXAMPLE FORMAT FOR RESPONSE:
(define 7a (+ 1 1))
(define 7b (-13 42))

Problem 8
10 points

What is a parameter? What are the three types of parameters? Explain the differences between the types.

Problem 9
10 points

What is a literal? Give an example of how you'd use a literal in Scheme.

Problem 10
10 points

Explain the differences between compiled and interpreted languages.

Problem 11
5 points

What does it mean to talk about the domain of a function? The range of a function?

Problem 12
5 points

Explain what it means for a function to be one-to-one.

Problem 13
5 points

Explain what it means for a function to be onto.

Welcome aboard, Maties! Captain Kurt the Balding and the crew of the Good Ship CS1311 would like to welcome you to the latest voyage. We also have many new crew members joining us fresh out of Pirate Training Camp. Help them get their sea legs and survive adventures on the High Seas by writing the following functions.

Problem 14
15 points

Let's start with an easy problem to warm up those brain cells. Although a master of looting and marauding, Tysen the Terrible (at math) doesn't remember much geometry. Define a Scheme function called circle-area that will calculate the area of a circle. The function will be passed the radius of the circle. For those of you who don't remember, Area equals Pi times Arrrrr! squared. Use 3.14 as your value for Pi.

Example usage:

>(circle-area 2)
12.56

Problem 15
25 points

Getting lost at sea is a constant problem for pirates. Unfortunately, the ship's navigator Sir Bryan the Hardy has tired of plundering and scurvy and is leaving the crew. The new navigator Nautical Nate has discovered that all of the information in the old navigation books is in statute miles (miles on land) rather than nautical miles! Arrrrrr! We will all surely perish at sea!

a) Help Navigator Nautical Nate by writing a Scheme function called miles-to-nautical that will convert statute miles to nautical miles.

1 nautical mile = 1.15 statute miles

>(miles-to-nautical 100)
86.95652173913044
b) Those crazy land lovers! They can't agree on one system of measurement. Write a Scheme function called kilometers-to-miles that will convert kilometers to miles.

1 statute mile = 5280 feet
1 foot = .305 meters
>(kilometers-to-miles 100)
62.09637357
c) Complete Nautical Nate's set of helpful conversion functions by defining the Scheme function kilometers-to-nautical that will convert kilometers to nautical miles. NOTE: for part C you MAY NOT use any mathematical symbols to compute your answer (in other words, you may not directly use +, -, *, /, or any other mathematical symbol in this function).
>(kilometers-to-nautical 100)
53.99684691946033

Problem 16
10 points

Captain Kurt wants to test their math skills, as pirates are a calculating bunch. The length of the hypotenuse of a right triangle is calculated by computing the square root of the sum of the squares of the lengths of the two sides of the triangle which meet at a right angle. Define a Scheme function called hypotenuse which takes two arguments. The two arguments are numbers which represent the lengths of the legs of a right triangle. The function returns a number representing the length of the hypotenuse of the right triangle.

>(hypotenuse 3 4)
5
>(hypotenuse 9 16)
18.35755975068582
> (hypotenuse 1 1)
1.4142135623730951
HINT:How do you get a decimal instead of a fraction? Experiment with a few simple functions like: (/ 3 4) (/ 3 4.0) (* 1.0 (/ 3 4))

Problem 17
15 points

"Not bad," says Kurt the Balding. "But let's see how cunning a pirate you really are....."

Stephen the Striking aspires to be a cartographer. He maps out the course of the ship wherever it goes and calculates distances between ports. It's tedious to calculate distances with calipers and a scale, but the map has a grid on it, so why don't you make his life easier?

The distance between two points is defined as follows: First, find the difference between the y-coordinates and the difference between the x-coordinates. Square the results and add them together. The square root of this sum is the distance between these points. Write a Scheme function called distance that will take four arguments: the x- and y-coordinates for points one and two. It should return the distance between these points.

Examples:

> (distance 0 3 4 0)   ;; points (0,3) and (4,0)
5
> (distance 0 0 2 3)   ;; points (0,0) and (2,3)
3.605551275
>(distance 5 7 4 9)    ;; points (5,7) and (4,9)
2.236067978

Problem 18
20 points

Some cities report their temperature in Celsius, while others report it in Fahrenheit. Just when the pirates get used to one system, they sail on to another city which uses the other. Long-boat Leo isn't very good at converting between the two. If he's not sure about the temperature, then he can't decide if he needs to put on his longjohns before the attack.

When converting between temperatures in degrees Fahrenheit and degrees Celsius, it is useful to note that -40 degrees Fahrenheit equals -40 degrees Celsius. This observation makes for the following symmetric conversion formulae:

     C = (F + 40) * 5/9 - 40
     F = (C + 40) * 9/5 - 40
Write two 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. This function should display the answer in the form of a decimal number.

Problem 19
10 points

Mad Mitchell is in charge of buying supplies for the ship. While he makes up a list of items to buy, he sends Adriatic Abe and David the Devious to change some gold doubloons into the local currency. Just to be on the safe side, he wants to know ahead of time how much money they should be bringing back. Write a Scheme function called thomas-cook-exchange that will be passed the amount of doubloons to exchange and the current exchange rate of the local currency into gold doubloons. (Since Deceitful Abe and David the Devious are pretty scary guys, tellers will not add additional costs to the exchange).

Assuming that a doubloon is worth 20 FF and we want to exchange 4 doubloons:

>(thomas-cook-exchange 4 20)
80
Mad Mitchell will be expecting 80 FF.
>(thomas-cook-exchange 13 6.8)
88.4
>(thomas-cook-exchange 26 0.659)
17.134

Problem 20
10 points

Sailin' Sam has an interesting hobby. He likes to stand on his head. But he's not content to be the only one upside-down; he also loves to take the reciprocal of numbers. Write a Scheme procedure called reciprocal that takes one argument, and returns that argument's reciprocal. You may assume that the given number will be non-zero.

> (reciprocal 1)
1
> (reciprocal -8)
-1/8
> (reciprocal 3/4)
4/3

Problem 21
15 points

Pirates of Old like to tell tales of an island found at the edge of the map. While this island has a name, nobody seems to remember it. This mysterious island is only known by its call number: 2430. It's rumored that there are pirates living on this island who are vicious beyond compare. These pirates would have no qualms about trying your reciprocal function with zero, and they will laugh and shout and celebrate if they can make your function give an error. However, Robert the Gashing has decided to prepare himself for these brutes by writing a new function that won't give an error if it's given zero as an input.

It's your job to help Robert out. Adapt the reciprocal procedure you wrote in problem 16 to handle any numeric input. If 0 is passed as an argument, your procedure should return the value that Scheme uses for false. Otherwise, the reciprocal of the input should be returned. Call this new function reciprocal-safe. (Do NOT modify the reciprocal function from problem 15; leave that one as it is and copy/change that code for this function)

Problem 22
5 points

When our pirate crew isn't sailing or plundering or pillaging, some of them like to race model boats. Two of the top racers are Becca the Buccaneer and Jolly Jim the Gruesome. The way the races work is much like a drag race on land. The boats are timed racing a known distance, and the boat with the lowest time wins. What's also interesting to note is how fast the boats went. Write a function called speed which takes in two arguments: the course length and the time it took for the boat to cross the course. Your function should return the average speed of the boat.

> (speed 20 34.1)
0.5865102639296187

Problem 23
20 points

It's a well known secret that Vinnie "Break your knees" Fiano runs a small gambling operation aboard ship. He needs an easy way to tell who wins a race. Write a function called find-winner which will take in the length of the course, the time for Becca's boat to run the course, and the time for Jim's boat to run the course. It will return a list of the winner's name (as a literal) and their winning speed. If it's a tie, it will return the word tie as a literal and the tying speed.

> (find-winner 100 20 30)
(becca 5)

> (find-winner 60 15 15)
(tie 4)


Problem 24
5 points

If there's one thing that Amazing Ahmed hates it's repetition. Doing things over and over and over gets him madder than a lobster in a boiling tea-kettle. Luckily, there's a programming technique called recursion that lets Amazing Ahmed keep his cool. Ahmed knows about this technique, but do you? Let's find out. In your own words, give a definition of recursion.