Case Studies - PowerPoint PPT Presentation

About This Presentation
Title:

Case Studies

Description:

Chapter 11: Case Studies. 1. Chapter 11. Case Studies. Chapter 11. Finding Maximum in List ... Need to know range of values. Need to assign sentinel, or use end ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 24
Provided by: cen7150
Category:
Tags: case | studies

less

Transcript and Presenter's Notes

Title: Case Studies


1
Chapter 11
  • Case Studies

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

3
Finding Maximum in List
  • Assume data are positive integers.
  • Set sentinel to -999 (or any non-positive
    integer).

Write a do-while loop.
4
Finding Maximum in List
  • For the moment, to simplify matter, we assume
    first data is n, the number of elements in the
    list.

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

6
Problem
  • 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 gt 2.

7
Existential Universal Test
  • 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?

8
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.

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

10
Existential Universal Test
  • What is wrong with this code?

11
Existential Universal Test
  • What is wrong with this code?

12
Existential Universal Test
  • Are all eggs white?

13
Existential Universal Test
  • What is wrong with this code?

14
Existential Universal Test
  • What is wrong with this code?

15
Problem
  • 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 gt 0.
    (An empty list and a list of one element are
    trivially in strictly increasing order.)

16
Quadratic Equations
  • 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.

17
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.

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

19
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

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

21
Factorisation
  • 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)

22
Factorisation
  • Sample run.

23
Homework
  • Try exercises in preceding slides.
Write a Comment
User Comments (0)
About PowerShow.com