Algorithms JPC and JWD © 2002 McGraw-Hill, Inc.
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
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
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
C++ Iterative Constructs(LOOP) Three constructs while statement for statement do-while statement
While Syntax
While Semantics
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;
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: listSize 4
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: listSize 0
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: listSize 0 0
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: listSize 0 0
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: listSize
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: listSize 0 0 1
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: listSize
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: listSize
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: listSize 1 1 1
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: listSize
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: listSize 1 1 5
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize
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: listSize 3 10 average 2.5 4
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: listSize
The For Statement Syntax for (ForInit ; ForExpression; PostExpression) Action Example for (int i = 0; i < 3; i++) { cout << "i is " << i; }
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 0
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 0
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 0
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 0
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i 1
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i 1
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 1
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 1
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 2
Execution Trace for (int i = 0; i < 3; i++) { cout << "i is " << i; } cout << "all done"; i is 0 i is 1 i 2
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
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
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
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
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
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
Example: int i=10; do { cout << "i is " << i; i++; } while (i<20);