2021/1/29
Homework 1
Homework 1
Programming Language BSL
Due Date: Friday January 29, 9pm Purpose To write simple functions. Expectations
This will be an individual (non-pair) assignment. For this and all future assignments you must upload one .rkt file in the language specified at the top of the assignment to the Handin Server. Failure to do so invalidates your submission and will therefore, unfortunately, result in a score of 0.
Unless otherwise stated, all functions you write must be accompanied by a signature and a purpose statement, in the format we studied in class.
Finally: Homework problems will sometimes end with questions like What conclusion should be drawn from these findings?, or Interpret this result!. These are intended to encourage you to not just write functions and test them, but to use them to increase our knowledge and make informed decisions. After all, we are problem solvers! We will not grade what you write here (since such conclusions can be subjective), but we may take off points if you write nothing.
Exercise 1 Design a function poly1 that takes a single number as input and calculates the following polynomial. For this and the problem after this, translate the expression after the = into BSLs prefix notation, but do not otherwise rewrite the expression. Enter your code in the definitions window.
poly1(x) = (1/100)*x3 (2/100)*x2 + 2x + 1
Exercise 2 Design a function poly2 that takes a single number as input and
calculates the following polynomial:
poly2(x) = 1 + x * (2 + x * (-2/100 + x / 100))
Now play with both functions in the interactions window, comparing the values they return for a number of inputs x. If you encoded the functions correctly you should find that ? correct, the two BSL functions seem to compute the same mathematical
https://course.ccs.neu.edu/cs2500/hw1.html 1/4
2021/1/29 Homework 1
function. (If that is not what you are finding, go back and check your code for poly1 and poly2.)
Background: The form of the polynomial used in poly2 is known as Horners scheme. Next time you are waiting at the bus stop, think about what advantages of the second form over the first form may be.
Exercise 3 Your friend claims he did the math on paper and believes the two polynomials are actually not the same. We could try to find his math error. But instead we want to test, on a large number of inputs, that they return the same value. (This is not a proof, but it is better than just testing a handful of cases by manual input.)
Here is a cool way to run those tests, along with some visualization.
(a) Define two constants of type Image. The first, called SCENE, defines an empty canvas of size 500500. Use the empty-scene function (look it up in the doc). The second, called DOT, defines a solid circle of size 5, of a color of your choice (not white, please!).
(b) We are now going to visualize the difference |poly1(x) poly2(x)| between the two polynomials. We expect it is zero (but your friend doesnt.) The |.| denotes the absolute value of a real number (find this function in BSL!).
Define a function poly-diff->scene that takes a single number x and produces an image. The image places DOT into the SCENE at position
(x,|poly1(x) poly2(x)|+50)
The +50 is just to scale up the y coordinate a little. For the placement, use the
place-image function.
(c) How do we use this function? in an animation! Load the universe library near the top of your definitions window (where you also load the image library), run, and then type (animate poly-diff->scene) in the interactions window. If you did everything right, you should see what?
Finally, introduce a bug: change poly2 so that it computes a different function than poly1, and run the animation again. One change that works well is: modify the 2 + part to 3 +.
To turn in: summarize your findings briefly in English. Is your friend convinced now?
Exercise 4 Suppose, purely hypothetically, you are taking a course that comes with 12 homeworks, and the homeworks together should constitute 30% of the total course grade. These two facts are non-negotiable.
The initial plan was to make all homeworks worth the same, so they each contribute 30%/12 = 2.5% to the course grade. But then the start of the class was delayed due to some pandemic, and the first class week turned out shorter. Accordingly, the first homework must be shorter and needs to count less than the other homeworks. Here are the exact rules:
The eleven homeworks HW02,,HW12 all count the same.
https://course.ccs.neu.edu/cs2500/hw1.html 2/4
2021/1/29 Homework 1
The first homework, HW01, counts some percentage p of the other homeworks. For example, if p=50, then HW01 counts 1/2 of all the others (50%). The value of p must be a natural number in the range [0,100].
Design a function hw02-12 that takes p as input and returns the percentage that each of HW02,,HW12 contributes to the total course grade, under the above rules. For example, if p=100, we have the special case that HW01 contributes the same to the total course grade as all other homeworks (the original plan). In this case, function hw02-12 should return ? correct, 30/12=2.5.
This problem requires a little bit of mathematical modeling. Show your steps, so we can give you partial credit if you are off. To do that, add these steps in comments to your homework file. You can include a whole block of text as a comment like this:
#|
A multi-line comment.
This is line 2
|#
Exercise 5 Design a function hw01 that takes p as input and returns the percentage that HW01 contributes to the total course grade. For example, if p=100, we have the special case that HW01 contributes the same to the total course grade as all other homeworks (the original plan). In this case, function hw01 should return ? correct, 2.5., as above. What about (hw01 0) ?
Hint: for this problem, do not reinvent the wheel. Consider how (hw01 p) and (hw02-12 p) are related.
Exercise 6 The homework creator designed these two functions and then went to the instructor, to ask her what value of p to use. But the instructor is tired and doesnt want to see a lot of numbers. So the homework creator offers her to present some data about the possible choices for p as a histogram. (Do a quick search on the web to see what a histogram is, in case you are not sure.)
The following is what we call a helper function for the histogram: Design a function histogram-bar that takes a number h as input and draws an outline of a rectangle of width 30, height 50*h-20 (yes, please use that exact formula; it simply scales the height (linearly) to make it easy to see), and a color of your choice.
Exercise 7 Now we are ready to construct a histogram. Design a function histogram
that takes eleven inputs (yes), call them p0,p1,,p10. (These will be eleven possible
choices for value p from above.) Your function draws eleven vertical bars, next to each other in a row, aligned at the bottom. For i=0,,10, bar number i is created using function histogram-bar, to which you pass the value returned by calling hw02- 12 on pi.
Note: A function with eleven inputsthat is pretty poor style, for several reasons. The most obvious is that we really want to draw a histogram for any number of given values, not only eleven! But we have seen only such a small fragment of the BSL language so far that we cannot do that yet.
https://course.ccs.neu.edu/cs2500/hw1.html 3/4
2021/1/29 Homework 1
For the signature of your function, you can denote the fact that it takes eleven inputs by I^11, but you need to replace I by the proper type of these inputs.
Exercise 8 Now its time for some drawing. First test your function using the call (histogram 0 300 600 900 1200 1500 1800 2100 2400 2700 3000)
(yes, in this call the p values are not all in the range [0,100]). If you did it right, the top line of the histogram should roughly show the curve of a well-known and simple mathematical function. Which function? If you cannot figure it out from the picture, look at the formula for function hw02-12, as a function of p.
Now we remember that the p values really should be percentages: how much HW01 counts as a fraction of the other homeworks. So here is a more realistic test case:
(histogram 0 10 20 30 40 50 60 70 80 90 100)
The homework creator presents this graph to your instructor. What conclusion should the two draw from this graph about the right choice for p? Comparing the graphs obtained using the two test cases will help!
Before you submit: Make sure all your function definitions come with a signature and a purpose statement!
https://course.ccs.neu.edu/cs2500/hw1.html
4/4
Reviews
There are no reviews yet.