University of Engineering and Technology Lahore Department of Computer Science and Engineering Programming Fundamentals Problem Set #21 Due: before 8:15 a.m., November 18,2012 in CS-3 Reading • Section 1.1 of SICP 1 Theory 1. Define a procedure pythagoreanTriple? that takes as input three integers, and returns #t if these form a Pythagorean triple and #f otherwise for example (pythagoreanTriple? 3 4 5) should return #t and (pythagoreanTriple? 4 4 3) should return #f. 2. Define a procedure called sign that takes a number as its argument and returns 1 if the number is positive, -1 if the number is negative, and 0 if the number is 0. 3. Write a procedure power-close-to that takes as arguments two positive integers b and n, and returns the smallest power of b that is greater than n. That is, it should return the smallest positive integer e such that be > n. You may use the Scheme procedure expt, which raises a given number to a given power. (power-close-to 2 1000) 10 (expt 2 10) 1024 4. Ben Bitdiddle is confused about the concepts of applicative order and normal order. He has written the following procedures in the definitions window. (define (foo a) (acme (+ 1 a))) (define (acme a) (foo (+ 2 a))) 1 Lab section has been copied from the mit ocw website of 6.001 1 (define (someProcedure a b) (if (< a 2) a b)) (a) He types in the expression (someProcedure 1 2). What will an applicative order interpreter print? What will a normal order interpreter print? (b) Now he types in the expression (someProcedure 1 (foo 1)). What will an applicative order print? What will a normal order interpreter print? 5. Exercise 1.6 of SICP 2 2 Lab Millions of years ago, when the dinosaurs roamed the earth, the Pakistani cricket team preferred cricket to betting and match-fixing. Now it is common knowledge that a cricket ball is a classical particle which obeys Newton’s laws. However, what is not common knowledge is the fact that the successs of the Pakistani cricket hinged on their ability to predict the motion of a cricket ball using Newtonian mechanics. For that purpose, the team used to have a resident physicist, who would lecture them on projectile motion. In fact it is rumoured that after a heroic struggle with projectile motion, the great Khan himself admitted that the only regret he has in his life is that he did not study physics at Oxford 2 . Unfortunately, times changed and evil began to prevail and the ancient art of Newtonian mechanics got lost. However, its practitioners, who call themselves Newtonians, did not die, but just went into hiding. Evil has been hunting the Newtonians ever since, and you are the last Newtonian left. However, Evil is closing in fast on you and your time is running out very quickly. To make matters worse, the intellectual abilities of the the national cricket team have sunk to such a low level that there is no hope of teaching the dunces anything about projectile motion in the meagre time you have left. Your only hope is that you can write a computer programme, which will help ordinary people gain some understanding about projectile motion after you are gone. That would begin to change the intellectual environment of the country so that a few centuries after you are dead, the cricket team would be chosen from a population where Newtonian physics is common knowledge. It is a meagre hope, but that is all you have. You have been hiding and running away from Evil for so long that you no longer have the energy to fight another day. You are extremely depressed and contemplating giving yourself up. But at this darkest moment in your life, the Spirit of Newtonians appears and says “You cannot lose hope. Giving up is a guarantee for failure. But if you try....anything is possible. Time and again the human spirit has accomplished what others have believed to be impossible. Follow my instructions and we will defeat Evil. Fist accomplish these five tasks. Once you have done so, more instructions will follow”. Buoyed by this vision, you sit up and turn on your laptop. The Spirit of Newtonians says, 1. “So what happens when a cricket ball is hit? For the moment, model a cricket ball as a particle that moves along a single dimension with some initial position u, some initial velocity v, and some initial acceleration a. The equation for the position of the ball at time t, given a, v, and u is ut = 1/2 at2 + vt + u. Note that this denotes a first order differential equation in time. Later, we can apply this equation to either the horizontal (x) component of ball motion, or the vertical (y) component of ball motion. Write a procedure that takes as input values for a, v, u, and t and returns as output the position of the ball at time t. (define (position a v u t) YOUR-CODE-HERE) Test your position code for at least the following cases: 2 To his credit, he never gave up till he became an expert in Newtonian mechanics 3 (position (position (position (position (position 0 0 0 2 5 0 0 5 2 5 0 0) ; -> 0 20 0) ; -> 20 10 10) ; -> 60 2 2) ; -> 5 5) ; -> In addition, you should add some test cases of your own to these to cover other boundary and typical conditions. 2. One of our goals is to determine how far a ball will travel in the air, if it is hit with some initial velocity at some initial angle with respect to the ground. To do this, we will need to know when the ball hits the ground, and for that we’ll want to find when the y coordinate of the ball’s position reaches zero. This can be discovered by finding the roots of the y position equation, and selecting the one that is larger (later in time). The proper tool for this is the quadratic formula. Given the coefficients of the quadratic equation az 2 + bz + c = 0, write a procedure to find one of the roots (call this root1), and another procedure to find the other root (call this root2). (define (root1 a b c) YOUR-CODE-HERE) (define (root2 a b c) YOUR-CODE-HERE) You may notice that, depending on how you wrote your procedures, for some test cases you get an error. For example, try (root1 5 3 6). What happens? If you get an error, which is likely if you wrote your code the straightforward way, figure out how to change it so that your procedure returns a false value in those cases where there is not a valid solution. 3. Given an initial upward velocity (in meters per second, or m/s) and initial elevation or height (in meters, or m), write a procedure that computes how long the ball will be in flight. Remember that gravity is a downward acceleration of 9.8m/s2 . Note that to solve this you will need a root of a quadratic equation. Try using root1, and using root2. Only one of these solutions makes sense. Which one? And why? Use this to create a correct version of the procedure below. (define (time-to-impact vertical-velocity elevation) YOUR-CODE-HERE) In some cases, we may want to know how long it takes for the ball to drop to a particular height, other than 0. Using your previous procedure as a template, write a procedure that computes the time for the ball to reach a given target elevation. (define (time-to-height vertical-velocity elevation target-elevation) YOUR-CODE-HERE) 4 4. Suppose the ball is hit with some velocity v, at a starting angle α relative to the horizontal (in degrees), and from an initial elevation (in meters). We wish to compute the distance in the horizontal direction the ball will travel by the time it lands. Remember that some of the velocity vector goes into the x direction, and some into the y. Checking the Scheme manual, you will find procedures sin and cos. To use these (which require angles in radians rather than degrees), you may also find the procedure degree2radian useful. It is given below: (define (degree2radian deg) (/ (* deg pi) 180.)) Write a procedure travel-distance-simple that returns the lateral distance the ball thrown with given velocity, angle, and initial elevation will travel before hitting the ground. (define (travel-distance-simple elevation velocity angle) YOUR-CODE-HERE) Try this out for some values. Using your code, determine the time to impact of a ball hit at a height of 1 meter (right down the middle of the plate) with an initial velocity of 45 m/sec (about 100 mph about what a really good professional player can do without steroids or betting), at angles of 0, 45 and 90 degrees (be sure to use radian units or degree units depending on how you provide input to sin and cos, but remember that those procedures expect arguments in units of radians). How far does the ball travel in each case? Notice the distance traveled in feet for a ball hit at a 45 degree angle, with this bat speed. Seems incredible right? Well come back to this in a little bit. 5. Before we figure out why professional players dont normally hit 200 meter sixes, lets first see if we can find out the optimal angle at which to launch a ball, in order to have it travel the furthest. Write a procedure find-best-angle that takes as arguments an initial elevation and an initial velocity, and which finds the best angle at which to hit the ball to optimize distance traveled. You will probably want to write a recursive procedure that tries different angles between 0 and pi/2 radians, sampled every 0.01 radians (say) or between 0 and 90 degrees, sampled every 1 degree (depending on whether your code works in radians or degrees either way be sure that you provide the right kind of unit to your trigonometric functions). (define (find-best-angle velocity elevation) YOUR-CODE-HERE) Use this for same sample values of elevation and velocity. What conclusion can you reach about the optimal angle of hitting?” 5