CMPT 120 - PowerPoint PPT Presentation

About This Presentation
Title:

CMPT 120

Description:

Completes in a finite amount of time given proper input ... Proper output. What are the normal cases (possibilities) based on the input? ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 19
Provided by: chr15
Category:
Tags: cmpt | proper

less

Transcript and Presenter's Notes

Title: CMPT 120


1
CMPT 120
  • Introduction to Computer Science and Programming
    I
  • Chris Schmidt

2
Algorithms
  • What is an algorithm?
  • An algorithm is a set of instructions on how to
    accomplish something
  • The basic idea can be applied to many typical
    activities
  • Recipes, assembly instructions, directions on how
    to get somewhere could all be viewed as types of
    algorithms

3
Algorithms
  • Recipe Example from Study Guide
  • Combine the room-temperature butter and the
    sugar. Mix until light and fluffy.
  • Add the eggs to the creamed butter and mix to
    combine.
  • In another bowl, combine the liquid ingredients
    and mix to combine.
  • Sift together the flour and other dry
    ingredients.
  • Alternately add the dry and liquid ingredients to
    the butter-egg mixture. Mix just enough to
    combine.

4
Algorithms
  • Aspects of a proper algorithm
  • Solves a problem
  • How to make muffins
  • Unambiguous instructions
  • Step 5 isnt entirely clear
  • Completes in a finite amount of time given proper
    input
  • This clearly does

5
Algorithms
  • What sort of problems are algorithms created for?
  • Sorting and searching
  • Well be looking at some well known algorithms
    for this later in the semester
  • Encryption and Decryption
  • Mathematical factoring, finding prime numbers
  • Idea can be applied to any problem you want to
    write code to solve

6
Algorithms
  • Check if a user entered number is prime
  • Ask the user for a number to check the primeness
    of.
  • Start with divisor equal to 2.
  • If the users number is divided evenly by the
    divisor, it is not prime. You are done.
  • If the divisor squared is less than the users
    number, increase it by one and go to step 3.
  • If youve reached this step, the users number is
    prime.

7
Algorithms
  • Aspects of a proper algorithm
  • Solves a problem
  • Tests if a user entered number is prime
  • Unambiguous instructions
  • Completes in a finite amount of time given proper
    input
  • What if step 4 didnt compare the divisor with
    the square root of the users number?

8
Pseudocode
  • So far weve used natural language to describe
    algorithms
  • It is helpful to describe algorithms in
    pseudocode (almost code)
  • An algorithm written in pseudocode is then easily
    coded in any give programming language

9
Pseudocode
  • Digital Clock Example from Study Guide
  • set hour to 0
  • set minute to 0
  • set second to 0
  • repeat forever
  • set second to second 1
  • if second is more than 59, then
  • set second to 0
  • set minute to minute 1
  • if minute is more than 59, then
  • set minute to 0
  • set hour to hour 1
  • if hour is more than 23, then
  • set hour to 0
  • write hour minutesecond
  • wait for 1 second

10
Pseudocode
  • Pseudocode for prime number example
  • set divisor to 2
  • write Enter an integer greater than 2
  • read userNumber
  • repeat while divisor2 lt userNumber
  • if userNumber divisor is equal to 0
  • write The number is not prime, it is divisible
    by
  • write divisor
  • exit program
  • set divisor to divisor 1
  • write The number is prime.

11
Writing a Program
  • Define the problem
  • Create a plan (algorithm) to solve the problem
  • Translate your algorithm into code
  • Test and make adjustments to your code and
    algorithm as neede

12
Creating an Algorithm
  • What to keep in mind when creating your algorithm
  • Read in all needed input
  • Proper output
  • What are the normal cases (possibilities) based
    on the input?
  • Are there any special cases?

13
Algorithm Example
  • Problem Find the roots (real) of a quadratic
    equation.
  • Quadratic Equation ax2 bx c
  • The roots are where this formula is equal to 0,
    ax2 bx c 0
  • Ex. a1,b0,c-1 x2 1 (x-1) (x1) 0
    roots at x1,x-1

14
Algorithm Roots of Quadratic Formula
  • How do we find the roots?
  • Quadratic equation
  • Does this formula always work?

15
Algorithm Roots of Quadratic Formula
  • Input the coefficients a,b,c
  • Output the roots of ax2 bx c
  • What cases do we need to handle?

16
Algorithm Roots of Quadratic Formula
  • What cases do we need to handle?
  • Normal
  • Two distinct roots
  • One root
  • But what special cases do we need to be careful
    of?
  • No roots 4ac gt b2
  • a0 (straight line bxc0 root at c/b)
  • a0,b0 (horizontal line, no roots unless c 0)
  • a0,b0,c0 (infinite roots)

17
Algorithm Roots of Quadratic Formula Pseudocode
  • write Enter the coefficients of the formula
  • read a,b,c
  • if a,b,and c equal 0
  • write infinite roots
  • exit
  • if a and b equal 0
  • write no roots
  • exit
  • if a equals 0
  • root -c/b
  • write One root at x, root
  • exit
  • .
  • .
  • .

18
Testing
  • Remember once youve translated your pseudocode
    into actual code that you need to test every
    possible case
Write a Comment
User Comments (0)
About PowerShow.com