Algorithms

CONCEPT

Figure 8-1

Informal definition of an algorithm used in a computer

Figure 8-2 Finding the largest integer among five integers .

Figure 8-3 Defining actions in FindLargest algorithm .

Figure 8-4 FindLargest refined .

Figure 8-5 Generalization of FindLargest .

2 THREE CONSTRUCTS .8.

Figure 8-6 Three constructs .

3 ALGORITHM REPRESENTATION .8.

Figure 8-7 Flowcharts for three constructs .

Figure 8-8 Pseudocode for three constructs .

Example 1 Write an algorithm in pseudocode that finds the average of two numbers Solution See Algorithm 8.1 on the next slide. .

Add the two numbers 2. Divide the result by 2 3.Algorithm 8. Return the result by step 2 End .1: Average of two AverageOfTwo Input: Two numbers 1.

.4 on the next slide. You do not know the number of numbers. Solution See Algorithm 8.Example 4 Write an algorithm to find the largest of a set of numbers.

Set Largest to 0 2. Return Largest End .4: Find largest FindLargest Input: A list of positive integers 1.Algorithm 8.1.1 if (the integer is greater than Largest) then 2. while (more integers) 2.1 Set largest to the value of the integer End if End while 3.

Example 5

Write an algorithm to find the largest of 1000 numbers.
Solution

See Algorithm 8.5 on the next slide.

Algorithm 8.5: Find largest of 1000 numbers
1. 2. 3. FindLargest Input: 1000 positive integers Set Largest to 0 Set Counter to 0 while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while Return Largest End

4.

8.5 SUBALGORITHMS

Figure 8-9 Concept of a subalgorithm .

Algorithm 8. while (more integers) 2.6: Find largest FindLargest Input: A list of positive integers 1. Set Largest to 0 2.1 FindLarger End while 3. Return Largest End .

1 Set Largest to the value of the integer End if End . if (the integer is greater than Largest) then 1.Subalgorithm: Find larger FindLarger Input: Largest and current integer 1.

8.6 BASIC ALGORITHMS .

Figure 8-10 Summation .

Figure 8-11 Product .

Figure 8-12 Selection sort .

Figure 8-13: part I Example of selection sort .

Figure 8-13: part II Example of selection sort .

Figure 8-14 Selection sort algorithm .

Flowcharts are generally drawn in the early stages of formulating computer solutions. showing. .Flowchart: What is a Flowchart?    The flowchart is a means of visually presenting the flow of control through an information processing systems. the sequence of steps. at a minimum. the operations performed within the system and the sequence in which they are performed. It is a graphic representation of how a process works.

Some standard symbols. which are frequently required for flowcharting many computer programs are shown below .Flowchart (Contd«): Guideline for drawing a flowchart: Flowcharts are usually drawn using some standard symbols.

Disk storage I/O symbol .shows hardcopy printer output.indicates input from or output to disk storage.shows an instruction other than input.indicates the beginning and end points of an algorithm. Process symbol . Printer output symbol . Principles of Programming . Input-output symbol .shows an input or an output operation.NI July 2005 32 .Flowchart Symbols Terminal symbol . output or selection.

Off-page connector .provides continuation of a logical path on another page. On-page connector . Flow lines .shows a selection process for two-way selection.NI July 2005 33 .Flowchart Symbols cont« Selection symbol .provides continuation of logical path at another point in the same page. Principles of Programming .indicate the logical sequence of execution steps in the algorithm.

Rectangle or box use it to represent an event which is controlled within the process. Diamond use it to represent a decision point in the process. . Typically this will be a step or action which is taken.Flowchart (Contd«): A set of useful standard Flowchart symbols:     Rounded box use it to represent an event which occurs automatically. Circle use it to represent a point at which the flowchart connects with another process.

. Proper documentation: Flowcharts serve as a good program documentation Efficient Coding: Flowcharts act as a guide or blueprint during the systems analysis and program development phase.ADVANTAGES OF USING FLOWCHARTS:     Communication: Flowcharts are better way of communicating the logic of a system Effective analysis: roblem can be analyzed in more effective way.

Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart.  .ADVANTAGES OF USING FLOWCHARTS (Contd«):  Proper Debugging: Flowchart helps in debugging process.

Flow chart of the while loop : .

Flow chart of the for loop: .

The flow chart of the if statement: .

The flow chart of the if«else statement: .

The flow chart of the switch statement: .

3.5): .Flowchart for finding the sum of first five natural numbers ( i.4. 1.2.e.

.Flowchart (Example): Flowchart to find the sum of first 50 natural numbers.

B Yes Print A Is A > B No Print B End .Flow Chart to find largest of two numbers: Start Read A.

and C: NO .Flowchart to find the largest of three numbers A.B.

  .LIMITATIONS OF USING FLOWCHARTS:  Complex logic: Sometimes. In that case. the program logic is quite complicated. reproduction of flowchart becomes a problem. Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely. Reproduction: As the flowchart symbols cannot be typed. flowchart becomes complex and clumsy.

An additional benefit is that because pseudocode does not need to use a specific syntax. . it can be translated into different programming languages and is therefore somewhat universal. Pseudocode is very similar to everyday English.Pseudocode    Pseudocode is a shorthand notation for programming which uses a combination of informal programming structures and verbal descriptions of code. This helps in the initial planning of a program. Pseudocode is an artificial and informal language that helps programmers develop algorithms. In general. It captures the logic and flow of a solution without the bulk of strict syntax rules. by creating the logical framework and sequence of the code. pseudocode is used to outline a program before translating it into proper syntax.

Pseudocode & Algorithm  Example 1: Write an algorithm to determine a student¶s final grade and indicate whether it is passing or failing. . The final grade is calculated as the average of four marks.

Pseudocode & Algorithm Pseudocode:  Input a set of 4 marks  Calculate their average by summing and dividing by 4  if average is below 50 Print ³FAIL´ else Print ³PASS´ .

Pseudocode & Algorithm   Detailed Algorithm Step 1: Input M1.M3.M2.M4 Step 2: GRADE n (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print ³FAIL´ else Print ³PASS´ endif .

example .

Design with Structure Charts .

. It is very difficult to see the design and address design issues when coding Design needs to take place in a mode which minimizes code influence . Code significantly clouds the design process.Design Process    Problem solving and design should be done independent of programming.

4 3 Write code to implement the top-down design. Improve readabililty of the code by incorporating functions. Top-down design.4 Steps 1 2 Create a structure chart based on the main item. As you mature this will be skipped by going to step 4. . NOT programming requirements Modify structure chart based on the programming requirements.

00 charge if the volume of the package exceeds 18 cubic feet.08 per lb.8 6.15 per lb. ----------------------------There is an additional $1. an appropriate message should be printed. The charge for shipping a parcel is $0.2 0.75 plus an amount based on package weight as follows: Weight (lb.0 . Write a program that will read the dimensions of a parcel (in feet) and its weight (in pounds) and then compute and print the postage due. If the package is rejected. NPS will not accept any packages whose largest dimension is greater than 3 feet or whose weight exceeds 50 pounds.National Parcel Example Example 2 : Parcel Service Company.10 per lb.5 1. Emphasizes IF notation for top down design. Over 40 $0.) Rate ----------------------------20 or less $0. 40 or less $0. National Parcel Service (NPS) specializes in nationwide delivery of small packages. Example Test Data Length Width Depth Weight 1.

75 Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18 .Parcel Delivery National Parcel Service Parcel * Size Weight Shipping Status Length Width Depth Acceptable Not Acceptable Charge Weight Charge Volume Charge 0.

but do provide insight into the structure we expect and perceive. In this problem we need to adjust for a single parcel. There are multiple views of anything. Other views are not wrong.Change in Perspective     Our natural view of the problem domain may not match the requirements. .

Parcel Delivery (adjust to one package) Parcel Size Weight Shipping Status Length Width Depth Acceptable Not Acceptable Charge Weight Charge Volume Charge 0.75 Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18 .

Step 2 Adapt the structure chart to accommodate the program specifications: DO A TOP-DOWN DESIGN .

0 Charge= Wt_Chrg+ Vol_Chrg+ 0.15 Vol<=18 Vol_chrg= 0.1 Wt>40 Wt_chrg= Wt*0.75 .08 20<Wt<=40 Wt_chrg= Wt*0.Adjusting Structure Chart for Program (Top-Down) Design Compute Postage Input Package Info Determine Acceptability Parcel Delivery Size Weight Acceptable Not Acceptable Length Width Depth Compute Charge Output Charge Output Rejection Message Compute Weight Charge Compute Volume Charge Compute Total Charge Wt<=20 Wt_chrg= Wt*0.0 Vol>18 Vol_chrg= 1.