6.12. ExercisesΒΆ

number: 1 Use the drawsquare function we wrote in this chapter in a program to draw the image shown below. Assume each side is 20 units. (Hint: notice that the turtle has already moved away from the ending point of the last square when the program ends.)

Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it.

Write a nonfruitful function
drawPoly(someturtle, somesides, somesize)
which makes a turtle draw a regular polygon. When called withdrawPoly(tess, 8, 50)
, it will draw a shape like this:

Draw this pretty pattern.

Write a nonfruitful function
drawEquitriangle(someturtle, somesize)
which callsdrawPoly
from the previous question to have its turtle draw a equilateral triangle.

Write a fruitful function
sumTo(n)
that returns the sum of all integer numbers up to and including n. SosumTo(10)
would be1+2+3...+10
which would return the value 55. Use the equation (n * (n + 1)) / 2.

Write a function areaOfCircle(r) which returns the area of a circle of radius r. Make sure you use the math module in your solution.

Write a nonfruitful function to draw a five pointed star, where the length of each side is 100 units.

Extend your program above. Draw five stars, but between each, pick up the pen, move forward by 350 units, turn right by 144, put the pen down, and draw the next star. You’ll get something like this (note that you will need to move to the left before drawing your first star in order to fit everything in the window):
What would it look like if you didn’t pick up the pen?

Extend the star function to draw an n pointed star. (Hint: n must be an odd number greater or equal to 3).

Write a function called drawSprite that will draw a sprite. The function will need parameters for the turtle, the number of legs, and the length of the legs. Invoke the function to create a sprite with 15 legs of length 120.

Rewrite the function
sumTo(n)
that returns the sum of all integer numbers up to and including n. This time use the accumulator pattern.

Write a function called
mySqrt
that will approximate the square root of a number, call it n, by using Newton’s algorithm. Newton’s approach is an iterative guessing algorithm where the initial guess is n/2 and each subsequent guess is computed using the formula: newguess = (1/2) * (oldguess + (n/oldguess)).

Write a function called
myPi
that will return an approximation of PI (3.14159...). Use the Leibniz approximation.

Write a function called myPi that will return an approximation of PI (3.14159...). Use the Madhava approximation.

Write a function called fancySquare that will draw a square with fancy corners (sprites on the corners). You should implement and use the drawSprite function from above. For an even more interesting look, how about adding small triangles to the ends of the sprite legs.