1.5 Exercises

  1. Imagine that you are creating a pocket calculator. You have created the functionality for all the buttons except x 2, the button that squares a number, and exp, which allows you to calculate baseexponent, where exponent is an integer. You may use any other functionality a calculator would normally have: for example, (+, -, *, /, =).

    1. Create the functionality for the x 2 button.

    2. Create the functionality for the exp button.

  2. In the third-grade math class of French mathematician Carl Gauss, the teacher needed to give the students some busywork. She asked the class to compute the sum of the first 100 numbers (1 to 100). Long before the rest of the class had finished. Carl raised his hand and told his teacher that he had the answer: 5,050.

    1. Craft an algorithm that will sum the first n numbers (assuming n ≥ 1). How many steps does your algorithm take to complete when n = 100? How many steps does it take when n = 1,000?

    2. Can you create an algorithm like Gauss’s where the number of steps does not depend on n?

  3. A palindrome is a word or phrase that reads the same way forward and backward, like “racecar.” Describe a sequence of steps that determines if a word or phrase is a palindrome.

  4. Consider the three mazes shown in Figure 1-3. Describe two different algorithms for solving a maze. Discuss advantages and disadvantages of each algorithm. Then look at the maze and predict which algorithm will complete first. See if your predictions were correct by applying your algorithms to the mazes.

    Three mazes for Exercise 4
    Figure 1-3. Three mazes for Exercise 4
  5. Figure 1-4 shows an alternative way to represent an algorithm. (Note: we introduce this construct in detail later on. If it looks too intimidating, skip it until after you’ve read Chapter 4.)

    1. Starting at the circle labeled “Start” work your way through the figure. What is the purpose of this algorithm?

    2. Translate the figure into simple language. Note that a diamond in the figure represents a condition that may be true or false.

    Alternative representation of an algorithm for Exercise 5
    Figure 1-4. Alternative representation of an algorithm for Exercise 5
  6. A cable company must use cables to connect 15 homes together so that every home is reachable by every other home. The company has estimated the costs of different cable routes (Figure 1-5 shows the numbers associated with each link). One engineer provides an algorithm, shown in Figure 1-5, that will find the cheapest set of routes to pick. Does the engineer’s algorithm work for this case? Why or why not?

    Cable company dilemma for Exercise 6
    Figure 1-5. Cable company dilemma for Exercise 6

    Engineer’s Algorithm:

    1. Pick one cable route with the lowest cost not already picked. Add this route to the set of cheapest routes.

    2. Check if every house is connected to every other house through any series of cables. If it isn’t, go back to step 1. If every house is connected, then the cheapest set of routes has been found.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57