Example: Applying GCDs - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Example: Applying GCDs

Description:

... diverse as encryption for online-banking to controlling the ... Calculator ... Solution for Expensive Calculator #include iostream using ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 11
Provided by: need9
Category:

less

Transcript and Presenter's Notes

Title: Example: Applying GCDs


1
Example Applying GCDs
  • The greatest common divisor (GCD) of two positive
    integers a and b is the largest integer that
    evenly divides both a and b.
  • Mathematicians say that an answer like 3/6 is
    wrong, because it can be reduced to1/2 by
    dividing out by the GCD of the numerator and
    denominator - 3 in this case.
  • An algorithm that computes the GCD of two numbers
    is useful in computer science for things as
    diverse as encryption for online-banking to
    controlling the motions of robots.

2
Euclideans GCD A Loopy Algorithm
  • Today's best known algorithm for solving this
    problem is the Euclidean Algorithm, based on
    the mathematics known to the ancient Greek world.
  • Euclids basic process (algorithm) for computing
    the GCD of two numbers a and b is this
  • Assuming a is the larger of the two,
    compute the remainder when a is
    divided by b, and
    replace a with b
    and b by the remainder.
  • Keep doing this until you get a
    remainder of zero, at which point
    b is the GCD.
  • For example, if a is 42 and b is 30, then 6 is
    the GCD.

3
Coding Up Euclids Algorithm
  • We can do this with a loop, and hopefully it's
    clear that the loop will look something like
    this
  • while(not exactly sure what goes here! )
  • r a b // remember "" is remainder
  • a b
  • b r
  • Two questions still need to be answered
  • When do we stop?
  • Where is the answer when we stop?

4
When to Stop Looping
  • Our clue to stop is that r is zero (or, you could
    say that b being zero is our clue).
  • Note that the answer resides in the variable a.
    So, our completed loop looks like this
  • while (b ! 0)
  • r a b // remember "" is remainder
  • a b
  • b r
  • cout ltlt a ltlt " is the GCD!" ltlt endl

5
Example An Expensive Calculator
  • Let's consider writing a program that reads from
    the user a simple arithmetic expression using
    and/or -, like
  • 3 4 - 7 2 - 4
  • Program prints out the result of the expression.
    Essentially your program will be a big while
    loop, and initially you might decide to write
    something like this

6
Example An Expensive Calculator
  • We can start filling in the parts of the code
    that we know how to do

7
Example An Expensive Calculator
  • Next well deal with reading in the /-'s, and
    using them to make up test conditions for our
    loop and if statements.
  • Suppose we read the /- operation into a variable
    of type char which we'll call op.
    Three problems to
    face
  • when to read in op
  • how to do the loop
    test condition
  • how to do the if test
    condition (lets tackle

    this first)

8
Where to read in the value for op?
  • The value for op will have to be read via cin gtgt
    op somewhere in the loop, but where?
  • 3 choices

9
Example Solution for Expensive Calculator
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int total0, k
  • char op ''
  • coutltlt "Enter expression "
  • while (op ! '') // Loop
  • // read next value
  • cin gtgt k
  • // add or subtract val from total
  • if (op '')
  • total total k
  • else
  • total total - k
  • cin gtgt op // read next op
  • // end while loop
  • // Write result
  • cout ltlt total ltlt endl
  • return 0
  • // end main

10
ICE Finding the Largest File
  • Write a program that reads the below, and prints
    out the largest file size.
  • File size (in bytes) appears in the column after
    "faculty".
  • The berettagt at the end of the input indicates
    that there are no more file entries to read.
Write a Comment
User Comments (0)
About PowerShow.com