قراءة
عرض

Program design

*

Program Design Process has 2 phases: Problem Solving Phase Creates an algorithm that solves the problem Implementation (Coding) Phase Translates the algorithm into a programming language
Algorithm
Problem
Program

Algorithms

* An algorithm is a finite set of steps defining the solution of a particular problem. Need not to belong one particular language Sequence of English statements can also be algorithm It is not a computer program An algorithm can be expressed in English like language,called pseudocode, in a programming language or in the form of flowchart.

* Example 1

Write an algorithm that finds the average of two numbers Solution:
AverageOfTwo Input: Two numbers Add the two numbers Divide the result by 2 Return the result by step 2 2 End

* Example 2

Write an algorithm to change a numeric grade to a pass/fail grade. Solution:
Pass/FailGradeInput: One numberif (the number is greater than or equal to 40)then 1.1 Set the grade to “pass”else 1.2 Set the grade to “fail”End ifReturn the gradeEnd


* Example 5
Write an algorithm to find the largest of 1000 numbers.
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
Solution

Flowchart

* A graphical representation of an algorithm, often used in the design phase of programming to work out the logical flow of a program. Visual way to represent the information flow Make our logic more clear Help during writing of program Make testing and debugging easy

*

Flowchart or program constructs

*

Flowchart Constructs

*

Flowchart Constructs (cont..)

*


Flowchart Constructs (cont..)
*

Example-1

Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Algorithm Step 1: Take Width and Length as input Step 2: Calculate Area by Width* Length Step 3: Print Area.

Example 3

Write an algorithm and draw a flowchart to convert the length in feet to centimeter. Algorithm: Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM)

Example 3

Pseudocode Step 1: Input LftStep 2: Lcm  Lft x 30 Step 3: Print Lcm START
Input Lft
Lcm  Lft x 30 Print Lcm
STOP
Flowchart

Example 4

Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

Example 4

Algorithm: Input the coefficients (a, b, c) of the quadratic equation Calculate d Calculate x1 Calculate x2 Print x1 and x2

Example 4

Pseudocode: Step 1: Input a, b, cStep 2: d  sqrt ( )Step 3: x1  (–b + d) / (2 x a)Step 4: x2  (–b – d) / (2 x a)Step 5: Print x1, x2 START
Input a, b, c
d  sqrt(b x b – 4 x a x c) Print x1 ,x2
STOP
x1 (–b + d) / (2 x a) X2  (–b – d) / (2 x a)

Example 5

Write an algorithm that reads three numbers and prints the value of the largest number.

Flow Charts Limitation

For very large program, flow chart goes for many pages Costly to draw flow charts for large program Difficult to modify
*




رفعت المحاضرة من قبل: Bilal AL Qazzaz
المشاهدات: لقد قام 7 أعضاء و 168 زائراً بقراءة هذه المحاضرة








تسجيل دخول

أو
عبر الحساب الاعتيادي
الرجاء كتابة البريد الالكتروني بشكل صحيح
الرجاء كتابة كلمة المرور
لست عضواً في موقع محاضراتي؟
اضغط هنا للتسجيل