230 likes | 244 Views
Explore case studies on finding the maximum value in a list, testing properties, and solving quadratic equations. Learn about existential and universal tests, along with practical coding examples.
E N D
Case Studies Chapter 11 Chapter 11: Case Studies
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
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
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
We may initialise first data value to max. Finding Maximum in List Finding Maximum in List
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
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
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
Is there a black egg among all eggs? Data representation: 'b' is black, 'w' is white. Existential & Universal Test Existential & Universal Test
What is wrong with this code? Existential & Universal Test Existential & Universal Test
What is wrong with this code? Existential & Universal Test Existential & Universal Test
Are all eggs white? Existential & Universal Test Existential & Universal Test
What is wrong with this code? Existential & Universal Test Existential & Universal Test
What is wrong with this code? Existential & Universal Test Existential & Universal Test
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
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
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
Add a loop in your program so that you can enter many equations in a single run, as shown below. Quadratic Equations Quadratic Equations
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
Solve the quadratic equations in your program, if they have real roots. Quadratic Equations Quadratic Equations
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
Sample run. Factorisation Factorisation
Try exercises in preceding slides. Homework Homework