Algorithms & Flowcharts The Programming Cycle  Five steps PDF

Title Algorithms & Flowcharts The Programming Cycle  Five steps
Author ahmad Hamadeh
Pages 21
File Size 1.7 MB
File Type PDF
Total Downloads 684
Total Views 940

Summary

6/3/2020 Algorithms & Flowcharts By: Mamoun Nawahdah (PhD) 2020 The Programming Cycle  Five steps: 1. Defining the Problem: what has to be done or what problem has to be solved. 2. Planning the Solution: select the best method for solving the defined problem: • Write pseudo code. • Draw Flowcha...


Description

Accelerat ing t he world's research.

Algorithms & Flowcharts The Programming Cycle Five steps ahmad Hamadeh

Related papers

Download a PDF Pack of t he best relat ed papers 

6/3/2020

Algorithms & Flowcharts By: Mamoun Nawahdah (PhD) 2020

The Programming Cycle  Five steps: 1. Defining the Problem: what has to be done or what problem has to be solved. 2. Planning the Solution: select the best method for solving the defined problem: • Write pseudo code. • Draw Flowchart.

3. Coding the Solution: the program is then made. • Program flowchart can serve as a guide in coding.

1

6/3/2020

The Programming Cycle cont. 4. Checking Out the Program: debug and test the program for errors. • Debugging – is the task of finding program errors (bugs) and correcting them so that the program runs correctly.

5. Documentation: • Description of the program logic and copies of the flow charts. • Operating instructions, including program and data structures, and test data used. • Input and output specifications.

Algorithm  A list of simple instructions for carrying out a process step by step.  Example: An algorithm for changing a flat tire: 1. Jack up the car 2. Unscrew the lugs 3. Remove the wheel 4. Put on the spare 5. Screw on the lugs 6. Jack the car down

2

6/3/2020

Pseudocode  Pseudocode is a kind of structured English for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax.  Example: Let's say that you have a friend arriving at the airport, and your friend needs to get from the airport to your house. Here are three different algorithms that you might give your friend for getting to your home:  The taxi algorithm  The bus algorithm  The call-me algorithm

Pseudocode: Example Cont.  The taxi algorithm:  Go to the taxi stand.  Get in a taxi.  Give the driver my address.

The bus algorithm:  Outside baggage claim, catch bus number 70.  Get off on Jerusalem street.  Walk two blocks north to my house.

 The call-me algorithm:  When your plane arrives, call my cell phone.  Meet me outside baggage claim.

3

6/3/2020

Pseudocode cont.  Common Action Keywords:     

Input: READ , OBTAIN, GET Output: PRINT, DISPLAY, SHOW Compute: COMPUTE, CALCULATE Initialize: SET Add one: INCREMENT

 Types of Algorithm operations:  Sequential  Conditional  Iterative

Sequential  Write an algorithm to find and print the sum of two integers ?      

Ask user to enter first integer Read the integer and save as integer_1 Ask user to enter the second integer Read second integer and save as integer_2 Add integer_1 to integer_2 and save result as sum Print sum to screen

4

6/3/2020

Sequential cont.  Write an algorithm to find and print the area of rectangle. Ask user to enter the height of rectangle. Read height and save as rectangle_height. Ask user to enter the width of rectangle. Read width and save as rectangle_width. Multiply rectangle_height by rectangle_width and save the result as area.  Display area.

    

Sequential cont.  Write an algorithm to reverse any two digits number.       

Ask user to enter two digits number. Read number and save as num. Divide num by ten and save result as tens. Divide num by ten and save remainder as rem. Multiply rem by ten and save the result as rev. Add tens to rev. Print rev.

5

6/3/2020

Conditional  Selection logic: Ask questions and choose alternative actions based on the answers.  Example if A is greater than B then print A else print B end if

 Note: ELSE keyword is optional

Conditional Operators  Logical Operators :  AND  OR

 Relational Operators :      

Greater than Greater than or equal Less than Less than or equal Equal Not Equal

6

6/3/2020

Conditional: example  Write an algorithm to print passed or failed based on the student grade.  Ask user to enter student grade.  Read grade and save as student_grade.  If student_grade greater than or equal sixty then • print “passed”  else • print “failed”  end if

Iterative  Perform “looping” behavior; repeating actions until a continuation condition becomes false  WHILE condition  sequence  END WHILE

 REPEAT  sequence  UNTILE condition

 FOR iteration bounds  sequence  END FOR

7

6/3/2020

Iterative: While

Iterative: Repeat

8

6/3/2020

Flowchart  A diagram representing the logical sequence in which the combination of steps or operations is to be performed.  Widely used graphic method for describing computer operations.  Major uses are in documentation and programming.

Flowcharting Symbols

9

6/3/2020

Notations Commonly used in Flowcharting Notation

Meaning

Notation

Meaning

+

Addition

= or EQ

Equal to

-

Subtraction

> or GT

Greater than

*

Multiplication

< or LT

Less than

/

Division

!= or NE

Not equal

()

Grouping

Y

Yes

|

Logical OR

N

No

&

Logical AND

EOF

End of File

Flowchart Structures  Sequence

10

6/3/2020

Example 1:

 Problem: Make a program flowchart that takes in the radius of a circle as input, compute the corresponding area, and print out the value of the radius and the area.  Solution: 1. 2. 3. 4.

Define the value of pi (equal to 3.1416) Read in the value of radius (r). Calculate the area of the circle using the formula: area = pi * r * r Print out the radius of the circle and its area.

Example 1:

11

6/3/2020

Flowcharting Problems:  Make a flowchart that allows entry of 3 numbers and print their sum and average.  Make a flowchart to compute the area of the triangle using the formula area = 1/2bh,  Where b = base, and h = height of the triangle.

 Make an algorithm and a flowchart that takes in the height in feet and inches and convert to meters, given 1 inch equals 0.0254 meters. Print out height in meters.

Flowcharting Problems cont.  Make a flowchart that takes in a temperature in degrees Fahrenheit and convert it into degrees Celsius using the following formula: Celsius = (5.0/9.0) * (Fahrenheit – 32)

12

6/3/2020

Decisions (Selection) Structures

Other Decision Structures

13

6/3/2020

Other Decision Structures

Example 2:  Problem: The ABC Manufacturing Company plans to give a year-end bonus to each of employees. Draw a program flowchart which will compute the bonus of an employee. Consider the following criteria: if the employee’s monthly salary is less that $1,000, the bonus is 50% of the salary; for employees with salaries greater than or equal to $1,000, the bonus is $1,000. Print out the name corresponding the bonus of the employee.

14

6/3/2020

Solution: 1. Read in the name and salary of the employee. 2. Test if the salary of the employee is less than $1,000. 3. If the salary is less than $1,000, the bonus is computed as 0.50*salary. Otherwise, the bonus is equal to $1,000. 4. Print out the employee’s name and bonus

Flowchart

15

6/3/2020

Programming Problems  Make an algorithm and a program flowchart that takes in the amount of purchase for 3 items. If the total amount purchase is less than $5000 the customer will receive 10% discount, if it is greater than or equal to $5000 the customer will receive 15% discount. Print out the name of the customer and the total amount due.  Make a flowchart that takes in 3 numbers and prints out the smallest and largest among them.

Programming Problems cont.  Make an algorithm and draw a program flowchart that checks if an integer is even or odd number. If the integer is even display the message “The number is even”. If the integer is odd display the message “The number is odd”. If the number is zero (0) stop processing.

16

6/3/2020

Looping Structures

Example 3:  Problem: The initial value of the radius of a circle is equal to one unit and each succeeding radius is one unit greater than the value before it. Draw a flowchart to compute the area of the circle starting with r = 1.0 up to 5.0, then print out each radius and the corresponding area of the circle.

17

6/3/2020

Solution: 1. Initialize the value of r to 1.0 and the value of pi to 3.1416. 2. Compute area using the formula: area=pi*r*r 3. Print out the value of r and the computed area 4. Increment the value of r by 1.0. 5. If r is less than or equal to 5.0, loop back and repeat step 2 through 5. However, if r is greater than 5.0, stop processing.

Flowchart

18

6/3/2020

Looping Structures

Example 4:  Problem: Draw a flowchart to compute and print the sum of the squares of positive integers from 1 to 100. Refer to the mathematical function defined below:

19

6/3/2020

Solution: 1. Initialize the sum to 0 and number, n to 1. 2. Test the value of n if its greater than 100. 3. If n is less than or equal to 100, perform the computation n*n, add to sum, add 1 to n (increment) and repeat step 2, otherwise, proceed to step 4. 4. Display the accumulated sum and end processing.

Flowchart

20...


Similar Free PDFs