Chapter 11 Case Studies Chapter 11 Case Studies

  • Slides: 23
Download presentation
Chapter 11 Case Studies Chapter 11: Case Studies 1

Chapter 11 Case Studies Chapter 11: Case Studies 1

Finding Maximum in List Init max While not end of list Read value If

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

Finding Maximum in List q Assume data are positive integers. q Set sentinel to

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

Finding Maximum in List q For the moment, to simplify matter, we assume first

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

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

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

Problem q Find the maximum and second maximum of a list of integers. q

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

Existential & Universal Test q Sometimes, we need to test some property in a

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

Existential & Universal Test q Test for existentiality versus test for universality. u. Is

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

Existential & Universal Test q Is there a black egg among all eggs? q

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

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

Existential & Universal Test q What is wrong with this code? Chapter 11 Existential & Universal Test 10

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

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

Existential & Universal Test q Are all eggs white? Chapter 11 Existential & Universal

Existential & Universal Test q Are all eggs white? Chapter 11 Existential & Universal Test 12

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

Existential & Universal Test q What is wrong with this code? Chapter 11 Existential & Universal Test 13

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

Existential & Universal Test q What is wrong with this code? Chapter 11 Existential & Universal Test 14

Problem q Given a list of integers, determine if the list is in strictly

Problem q 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. q Do not assume the range of values for list elements. q 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. ) Chapter 11 Problem 15

Quadratic Equations q A quadratic equation in x is of this form: ax 2

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

Quadratic Equations q Write a program to request for the values a, b, c

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

Quadratic Equations q Add a loop in your program so that you can enter

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

Quadratic Equations q Recall that the roots are: [ -b ± (b 2 -

Quadratic Equations q Recall that the roots are: [ -b ± (b 2 - 4 ac) ] / 2 a q The discriminant is (b 2 - 4 ac). If this is upositive, then there are two real roots uzero, then there is only one real root unegative, then there are imaginary roots Chapter 11 Quadratic Equations 19

Quadratic Equations q Solve the quadratic equations in your program, if they have real

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

Factorisation q Write a program to enter a list of positive integers, using zero

Factorisation q Write a program to enter a list of positive integers, using zero as a sentinel to end the input. q 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) Chapter 11 Factorisation 21

Factorisation q Sample run. Chapter 11 Factorisation 22

Factorisation q Sample run. Chapter 11 Factorisation 22

Homework Try exercises in preceding slides. Chapter 11 Homework 23

Homework Try exercises in preceding slides. Chapter 11 Homework 23