Example: Solution of Quadratic Equations - PowerPoint PPT Presentation

About This Presentation
Title:

Example: Solution of Quadratic Equations

Description:

1. Example: Solution of Quadratic Equations. We want to solve for real values of x, ... The value of x can be determined from the 'well-known' formula: ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 12
Provided by: alanwi8
Category:

less

Transcript and Presenter's Notes

Title: Example: Solution of Quadratic Equations


1
Example Solution of Quadratic Equations
  • We want to solve for
    real values of x, for given values of a, b, and
    c.
  • The value of x can be determined from the
    well-known formula
  • Depending on the value of the discriminantwe
    have 3 cases
  • two real values for x
  • one real value for x
  • no real values for x

2
Flow diagram
b2 4ac gt 0
True
False
b2 4ac lt 0
False
True
x1 b/2a
No solutions
3
Flow diagram, version 2
d b2 4ac
False
True
d gt 0
d lt 0
False
True
x1 b/2a
No solutions
4
Structure of nested if statements
  • discriminant b b - 4.0 a c
  • if ( discriminant gt 0 )
  • // We have two real solutions
  • else
  • if ( discriminant lt 0 )
  • // We have no real solutions.
  • else
  • // We have one real solution.

5
Adding the Complex case
  • Suppose we now want to solve
    for real or complex values of x, for given
    values of a, b, and c.
  • In this case, we can still use the formula
  • but when we have we will need to be careful to
    avoid taking a square root of a negative number.
  • If we take the square root of the absolute value
    of the discriminant, we have the complex
    coefficient

6
Flow diagram
d b2 4ac
False
d gt 0
True
d lt 0
True
False
x1 b/2a
7
Testing for Equality withFloating Point Values
  • Because of the possibility of round-off error,
    testing for equality (or inequality) of float or
    double values is NOT recommended.
  • Instead, test that the value is close enough to
    the desired value by using a tolerance value, and
    checking that the absolute value of the
    difference is less than the tolerance
  • double EPSILON 1.0e-10
  • double x
  • ...
  • if ( x 37.0 ) // not recommended
  • ...
  • if ( Math.abs( x 37.0 ) lt EPSILON ) // better

8
The switch statement
  • Allows for multi-way branches
  • You can only use case statements when the test is
    on an integer, Boolean, or character variable.
  • Idea
  • List various cases for specific values of the
    tested variable, plus one default case for when
    there is no match.
  • Provide a statement block for each case.
  • You MUST end each statement block with break or
    the program will go to the next following case.

9
The switch statement
true
case 1
statement block 1
break
false
true
case 2
statement block 2
break
false
true
case n
statement block n
break
false
defaultstatement block
10
Example
  • System.out.println(Enter an integer from 1 to 3
    )
  • int a Keyboard.readInt()
  • switch( a )
  • case 1
  • System.out.println(You entered a 1.)
  • break
  • case 2
  • System.out.println(You entered a 2.)
  • break
  • case 3
  • System.out.println(You entered a 3.)
  • break

11
Missing break statement
true
case 1
statement block 1
false
true
case 2
statement block 2
break
false
true
case n
statement block n
break
false
defaultstatement block
Write a Comment
User Comments (0)
About PowerShow.com