Download presentation
Presentation is loading. Please wait.
Published byJemimah Greer Modified over 8 years ago
2
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc.
3
2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for solving a problem. Properties of an algorithm: - input: zero or more values - output: at least one value - definiteness: each step is precisely defined - finiteness: each step in the algorithm should be solved in specific period of time - effectiveness: each step is capable to solved e.g 0/3 can’t be solved
4
Algorithms(Cont.) We can specify the different between algorithm and program according to computing theory there is a different between them..in algorithm the previous five condition should be applied and it can be describe using natural language (pseudo code) Or(flowchart) Where in the programs all condition should be applied accept the forth one, not all program applied forth condition because the operating system will describe the program in programming language, it design to control the implementation of several jobs, when there is no specific job the operation system does not finish it jobs, it will continue and waiting for new job to be entered. 3
5
Pseudo code examples If student’s grade is greater than or equal to 60 Print “passed” Else Print “faild” Another example: N=0 For each person in the room Set n=n+1
6
C++ Iterative Constructs(LOOP) Three constructs while statement for statement do-while statement
7
While Syntax
8
While Semantics
9
Computing an Average int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average;
10
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; Suppose input contains: 1 5 3 1 6 listSize 4
11
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i Suppose input contains: 1 5 3 1 6 4 listSize 0
12
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum Suppose input contains: 1 5 3 1 6 4 listSize 0 0
13
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum Suppose input contains: 1 5 3 1 6 4 listSize 0 0
14
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 0 0 --
15
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 0 0 1
16
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 0 0 1 1
17
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 0 1 1 1
18
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 1 1 1
19
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 1 1 --
20
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 1 1 5
21
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 1 1 5 6
22
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 1 6 5 2
23
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 2 6 5 6
24
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 2 6 -- 2
25
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 2 6 3 2
26
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 2 6 3 9
27
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 2 9 3 3
28
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 3 9 3 3
29
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 3 9 -- 3
30
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 3 9 1 3
31
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 3 9 1 10
32
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 3 10 1 4
33
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum value Suppose input contains: 1 5 3 1 6 4 listSize 3 10 1 4
34
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum Suppose input contains: 1 5 3 1 6 4 listSize 3 10 average 2.5 4
35
Execution Trace int listSize = 4; int i = 0; double sum = 0; while (i < listSize) { double value; cin >> value; sum = sum + value; i++; } double average = sum / i ; cout << "Average: " << average; i sum average Suppose input contains: 1 5 3 1 6 4 listSize 3 10 2.5 4
36
The For Statement Syntax for (ForInit ; ForExpression; PostExpression) Action Example for (int i = 0; i < 3; i++) { cout << "i is " << i; }
38
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 0
39
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 0
40
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 0
41
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 0
42
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 1
43
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 1
44
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 1
45
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 1
46
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 2
47
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 2
48
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 2
49
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 2
50
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 3
51
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 i 3
52
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i is 2 all done i 3
53
The Do-While Statement Syntax do Action while (Expression) Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true false Expression
54
Example: int i=10; do { cout << "i is " << i; i++; } while (i<20);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.