+ All Categories
Home > Documents > C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be...

C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be...

Date post: 01-Jun-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
22
1 Outline Program development cycle. Algorithms development and representation. Examples.
Transcript
Page 1: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

1

Outline

Program development cycle.

Algorithms development and representation.

Examples.

Page 2: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

2

Program Development Cycle Program development cycle steps:

Problem definition.

Problem analysis (understanding).

Algorithm development: Ways for algorithm representation:

Human language

Pseudocode.

Flowcharts (also called UML activity diagram).

Coding.

Execution and testing.

Maintenance.

Recall that such cycle and all the techniques presented in this lecture are the same for any programming language you want to use not only for C++.

Page 3: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

3

Problem Definition

To understand the problem is half the solution.

Describe it by precise, up to the point statements that will make both analyzing and solving the problem easier and clearer.

Page 4: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

4

Problem Analysis

Determine the inputs, outputs, and the required operations.

Explore all possible solutions.

Pick the easiest, in terms of implementation cost (space, time) one.

Page 5: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

5

Algorithm Development

Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is

called program control and in industry it is called work flow).

So, it is a plan for solving the given problem. You must validate the developed algorithm, i.e. make

sure that it solves the correct problem. You must verify the developed algorithm, i.e. make

sure that it produces correct results. You must check the feasibility (in terms of the needed

resources, ease of implementation, ease of understanding and debugging, its expected execution time, etc.) of the developed algorithm.

Page 6: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

6

Algorithm Representation – Human Language

Use your own language to represent the steps of the developed algorithm.

Example: adding two integers:

1. Prompt the user to enter two numbers.

2. Add them and store the result.

3. Display the sum to the user on the screen.

Page 7: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

7

Algorithm Representation – Pseudocode Artificial, informal language used to develop

algorithms. Kind of structured English for describing algorithms. Middle approach between human language and C++

code. It is convenient and user friendly. Not actually executed on computers Allows us to “think out” a program before writing the

code for it Usually easy to convert into a corresponding C++

program Consists only of executable statements, i.e. no

definitions or declarations.

Page 8: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

The Hashemite University 8

Pseudocode Notations I Input:

Input from keyboard: Get. Input from file or memory location: Read.

Output: Output to printer: Print. Output to file: Write. Output to screen: Display, Prompt (usually followed

by Get).

Values assignment: Initial values: Initialize, Set. Results of computation: =, .

Keeping variables for later use: Save, Store.

Page 9: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

9

Pseudocode Notations II Arithmetic computation:

Either use exact operators (+, *, /, -) or equivalent words of them (add, multiply, divide, subtract).

Computations: either use Compute or represent the actual operation mathematically. E.g. Compute average or avg = sum/count.

Control structures: Use the actual words as it is: If, If – then – Else,

While, do – While, For – to --, ...

Relational operators: Write them as words: greater then, less than or

equal, etc. Or you can use their symbols: >, <=, etc.

Page 10: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

10

Pseudocode Examples Adding two numbers:

1. Prompt user for number1 2. Get number1 3. Prompt user for number2 4. Get number2 5. Add number1 and number2 6. Set sum to the result 7. Display sum

Other examples (on board):

Deciding the grade (A-F) of a student.

Page 11: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

11

Algorithm Representation – Flowcharts

Represent the algorithms or computer programs execution steps graphically.

The American National Standards Institute (ANSI) published a standard for flowcharts that includes a definition of their symbols (see next slide).

Pseudocode is preferred over flowcharts since: More readable. Can be converted into C++ code easier.

Flow charts are very similar to the UML (Unified Modeling Language) activity diagram with some differences in the used symbols.

UML is an industry standard for modeling software systems.

We will study flowcharts not activity diagrams in this course.

Page 12: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

The Hashemite University 12

Flowcharts Symbols

Page 13: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

13

Flowchart Examples

Numbers addition example.

Start

End

Get number1

Get number2

sum = number1 + number2

Display sum

Page 14: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

14

Decision flow chart

Page 15: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

15

Looping flow chart

Page 16: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

16

Example III

Write a program that reads three numbers from a file. If the multiplication of these numbers is greater than or equal their sum then print “Winner” on the screen”, otherwise print “Loser” on the screen.

Solution:

On board.

Page 17: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

17

Class Average Algorithm

Problem: Calculate and report the grade-point average for a class

Discussion: The average grade equals the sum of all grades divided by the number of students

Output: Average grade

Input: Student grades

Processing: Find the sum of the grades; count the number of students; calculate average

Page 18: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

18

Flowchart

Page 19: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

19

Pseudocode

Program: Determine the average grade of a class

Initialize Counter and Sum to 0

Do While there are more grades

Get the next Grade

Add the Grade to the Sum

Add 1 to the Counter

Loop

Compute Average = Sum/Counter

Display Average

Page 20: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

20

Coding

Writing the source code of your solution that is to convert the developed algorithm into code statements of the used language, i.e. C++.

Some useful tips: Make sure of using correct syntax.

Use meaningful identifiers to make your code more readable.

Add suitable documentation and comments.

Make your code modular or structured as possible.

Page 21: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

21

Execution and Testing

Compilation and debugging. Types of errors:

Syntax errors (Compile time errors): Errors caught by compiler

Logical errors (Runtime errors): Errors which have their effect at execution time

Non-fatal logic errors program runs, but has incorrect output

Fatal logic errors program exits prematurely

Tracing to verify your program with different sets of inputs.

Page 22: C++: Ch 4 · 5 Algorithm Development Algorithm is a procedure that determines the: Actions to be executed. Order in which these actions are to be executed (which is called program

22

Maintenance

Not always applicable in education, i.e. highly required in real world jobs.

Update your code based on:

Discovered and reported bugs.

Customer feedback to make your application more efficient and flexible.

Upgrade the code.


Recommended