1 / 23

Chapter 11

Case Studies. Chapter 11. Init max While not end of list Read value If (value>max) max  value Report max. Finding Maximum in List. Need to know range of values Need to assign sentinel, or use end-of-file (ctrl-d in UNIX) character. Assume data are positive integers.

gudrund
Download Presentation

Chapter 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Case Studies Chapter 11 Chapter 11: Case Studies

  2. Init max While not end of list Read value If (value>max) max  value Report max Finding Maximum in List • Need to know range of values • Need to assign sentinel, or use end-of-file (ctrl-d in UNIX) character. Finding Maximum in List

  3. Assume data are positive integers. Set sentinel to -999 (or any non-positive integer). Finding Maximum in List Write a do-while loop. Finding Maximum in List

  4. For the moment, to simplify matter, we assume first data is n, the number of elements in the list. Finding Maximum in List Finding Maximum in List

  5. We may initialise first data value to max. Finding Maximum in List Finding Maximum in List

  6. Find the maximum and second maximum of a list of integers. Do not assume the range of values for list elements. Assume first data is list size n, where n >= 2. Problem Problem

  7. Sometimes, we need to test some property in a list. Two common tests are: Existentiality: is there an element with a certain property? Universality: do all elements have a certain property? Existential & Universal Test Existential & Universal Test

  8. Test for existentiality versus test for universality. Is there a black egg among all eggs? This employs existentiality. Are all eggs white? This employs universality. Their codes are different. Existential & Universal Test Existential & Universal Test

  9. Is there a black egg among all eggs? Data representation: 'b' is black, 'w' is white. Existential & Universal Test Existential & Universal Test

  10. What is wrong with this code? Existential & Universal Test Existential & Universal Test

  11. What is wrong with this code? Existential & Universal Test Existential & Universal Test

  12. Are all eggs white? Existential & Universal Test Existential & Universal Test

  13. What is wrong with this code? Existential & Universal Test Existential & Universal Test

  14. What is wrong with this code? Existential & Universal Test Existential & Universal Test

  15. Given a list of integers, determine if the list is in strictly increasing order. For example, (-3, 0, 6, 17) is in strictly increasing order, but (-6, 2, 2, 5) and (5, 7, 8, 6) are not. Do not assume the range of values for list elements. Assume first data is list size n, where n >= 0. (An empty list and a list of one element are trivially in strictly increasing order.) Problem Problem

  16. A quadratic equation in x is of this form: ax2 + bx + c = 0 Assume that a, b, and c are integers, and a is not equal to zero. Examples: 2x2 - 3x -7 = 0; -3x2 + 8x -3 = 0; x2 + 4 = 0 But 4x + 5 = 0; x3 + 3x2 + 2 = 0 are not. Quadratic Equations Quadratic Equations

  17. Write a program to request for the values a, b, c of a quadratic equation and display the equation. See three sample runs below. Quadratic Equations Quadratic Equations

  18. Add a loop in your program so that you can enter many equations in a single run, as shown below. Quadratic Equations Quadratic Equations

  19. Recall that the roots are: [ -b ±  (b2 - 4ac) ] / 2a The discriminant is (b2 - 4ac). If this is positive, then there are two real roots zero, then there is only one real root negative, then there are imaginary roots Quadratic Equations Quadratic Equations

  20. Solve the quadratic equations in your program, if they have real roots. Quadratic Equations Quadratic Equations

  21. Write a program to enter a list of positive integers, using zero as a sentinel to end the input. For each positive integer entered, find out its factors, and display the factors as shown in the example, in increasing order of factors. (Factor 1 is listed only once) Factorisation Factorisation

  22. Sample run. Factorisation Factorisation

  23. Try exercises in preceding slides. Homework Homework

More Related