Chapter 6

Chapter 6

Exercises

  1. Using the top-down method, please describe an algorithm for a program to solve the real roots of a quadratic equation

        \[ax^2+bx+c=0\]


    Your algorithm should ask the user for the coefficients. To calculate the roots of the equation, your algorithm should calculate the discriminant D given by:

        \[D=b^2-4ac\]


    If D > 0, the algorithm displays a message: “The equation has two roots” and then displays the roots.
    If D = 0, the algorithm displays a message: “The equation has one root”, and then displays the root.
    If D < 0, the algorithm displays a message: “The equation has no real roots”. Create a flowchart for your algorithm.
  2. Using the top-down method, please describe an algorithm for a program that calculates the cost of a car rental according to the following price schedule:
Type of CarRental Period
1-6 Days7-27 Days28-60 Days
Class B$27 per day$162 for 7 days, +$25 for each additional day$662 for 28 days, +$23 for each additional day
Class C$34 per day$204 for 7 days, +$31 for each additional day$810 for 28 days, +$28 for each additional day
Class DClass D cannot be rented for less than 7 days$276 for 7 days, +$43 for each additional day$1,136 for 28 days, +$38 for each additional day

The algorithm asks the user to enter the rental period and type of car. The algorithm should display the appropriate cost. If a period longer than 60 days is entered, a message “Rental is not available for more than 60 days” should be displayed. If a rental period of less than 6 days is entered for Class D, a message “Class D cars cannot be rented for less than 6 days” should be displayed. Create a flowchart for your algorithm.