lecture_11_algorithms Algorithms Ch8.1
lecture_11_algorithms Algorithms Ch8.1
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Informal Definition
CONCEPT
©Brooks/Cole, ©Brooks/Cole,
2003 2003
©Brooks/Cole, ©Brooks/Cole,
2003 2003
1
Figure 8-3 Figure 8-4
Defining actions in FindLargest algorithm FindLargest refined
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Figure 8-5
Generalization of FindLargest
8.2
THREE CONSTRUCTS
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Figure 8-6
Three constructs
8.3
ALGORITHM
REPRESENTATION
©Brooks/Cole, ©Brooks/Cole,
2003 2003
2
Figure 8-7 Figure 8-8
Flowcharts for three constructs Pseudocode for three constructs
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Example 1
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Example 2
Algorithm 8.2: Pass/no pass Grade
Write an algorithm to change a numeric Pass/NoPassGrade
grade to a pass/no pass grade. Input: One number
1. if (the number is greater than or equal to 70)
then
Solution 1.1 Set the grade to “pass”
else
See Algorithm 8.2 on the next slide. 1.2 Set the grade to “nopass”
End if
2. Return the grade
End
©Brooks/Cole, ©Brooks/Cole,
2003 2003
3
Example 3
Algorithm 8.3: Letter grade
Write an algorithm to change a numeric LetterGrade
Input: One number
grade to a letter grade. 1. if (the number is between 90 and 100, inclusive)
then
Solution 1.1 Set the grade to “A”
End if
See Algorithm 8.3 on the next slide. 2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Continues on the next slide
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Algorithm 8.3: Letter grade (continued) Algorithm 8.3: Letter grade (continued)
3. if (the number is between 70 and 79, inclusive) 5. If (the number is less than 60)
then then
3.1 Set the grade to “C” 5.1 Set the grade to “F”
End if End if
4. if (the number is between 60 and 69, inclusive) 6. Return the grade
then End
4.1 Set the grade to “D”
End if
Continues on the next slide
©Brooks/Cole, ©Brooks/Cole,
2003 2003
©Brooks/Cole, ©Brooks/Cole,
2003 2003
4
Example 5
Algorithm 8.5: Find largest of 1000 numbers
FindLargest
Write an algorithm to find the largest of Input: 1000 positive integers
1000 numbers. 1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
Solution then
3.1.1 Set Largest to the value of the integer
See Algorithm 8.5 on the next slide. End if
3.2 Increment Counter
End while
4. Return Largest
End
©Brooks/Cole, ©Brooks/Cole,
2003 2003
Summary
©Brooks/Cole,
2003