Docsity

Algorithm & Flowchart, Study notes of Logic

Partial preview of the text

Download Algorithm & Flowchart and more Study notes Logic in PDF only on Docsity!

ALGORITHM & FLOWCHART MANUAL for STUDENTS

Part (1)

Algorithm in Programming:

In programming, algorithm is a set of well-defined instructions in sequence to solve the problem.

HOW TO WRITE ALGORITHMS:

Step 1 Define your algorithms input : Many algorithms take in data to be processed, e.g. to calculate the area of rectangle input may be the rectangle height and rectangle width. Step 2 Define the variables : Algorithm's variables allow you to use it for more than one place. We can define two variables for rectangle height and rectangle width as HEIGHT and WIDTH (or H & W). We should use meaningful variable name e.g. instead of using H & W use HEIGHT and WIDTH as variable name. Step 3 Outline the algorithm's operations: Use input variable for computation purpose, e.g. to find area of rectangle multiply the HEIGHT and WIDTH variable and store the value in new variable (say) AREA. An algorithm's operations can take the form of multiple steps and even branch, depending on the value of the input variables. Step 4 Output the results of your algorithm's operations : In case of area of rectangle output will be the value stored in variable AREA. if the input variables described a rectangle with a HEIGHT of 2 and a WIDTH of 3, the algorithm would output the value of 6.

Flowchart:

Flowchart is diagrammatic /Graphical representation of sequence

of steps to solve a problem. To draw a flowchart following

standard symbols are use

Symbol Purpose Description

Flow line

Used to indicate the flow of logic by connecting symbols.

Symbol Purpose Description

Terminal(Stop/Start)

Used to represent start and end of flowchart.

Input/Output Used for input and output operation.

Processing

Used for airthmetic operations and data-manipulations.

Decision

Used to represent the operation in which there are two alternatives, true and false.

On-page Connector Used to join different flowline

Off-page Connector

Used to connect flowchart portion on different page.

Predefined Process/Function

Used to represent a group of statements performing one processing task.

Example (3)

Find Area and Perimeter of Square:

L : Side Length of Square AREA : Area of Square PERIMETER : Perimeter of Square

Algorithm

Step-1 Start

Step-2 Input Side Length of Square say L

Step-3 Area = L x L

Step-4 PERIMETER = 4 x L

Step-5 Display AREA, PERIMETER

Step-6 Stop

Find Area and Perimeter of Rectangle:

L : Length of Rectangle طول المستطيل B : Breadth of Rectangle عرض المستطيل AREA : Area of Rectangle PERIMETER : Perimeter of Rectangle

Algorithm

Step-1 Start

Step-2 Input Side Length & Breadth say L, B

Step-3 Area = L x B

Step-4 PERIMETER = 2 x ( L + B)

Step-5 Display AREA, PERIMETER

Step-6 Stop

Example (4)

Find Area and Perimeter of Circle:

R : Radius of Circle AREA : Area of Circle PERIMETER : Perimeter of Circle

Algorithm Step-1 Start Step-2 Input Radius of Circle say R Step-3 Area = 22.0/7.0 x R x R (or) (3.14 ×R ×R ) Step-4 PERIMETER = 2 x 22.0/7.0 x R (2 × 3.14 ×R ) Step-5 Display AREA, PERIMETER Step-6 Stop

Find Area and Perimeter of Triangle:

A : First Side of Triangle B : Second Side of Triangle C : Third Side of Triangle AREA : Area of Triangle PERIMETER : Perimeter of Triangle

Algorithm

Step-1 Start Step-2 Input Sides of Triangle A,B,C Step-3 S= (A + B + C)/ 2. Step-4 AREA = SQRT(S x (S-A) x (S-B) x(S-C)) Step-5 PERIMETER = A + B + C Step-6 Display AREA, PERIMETER Step-7 Stop

Example (5)

Example (6)

Conditional Statements : if, else, switch

Conditional statements help you to make a decision based on certain conditions. These conditions are specified by a set of conditional statements having Boolean expressions which are evaluated to a Boolean value true or false.

1. if statement

2. If-Else statement

3. Nested If-else statement

4. If-Else If

5. Switch statement

If statement

The single if statement in is used to execute the code if a condition is true. It is also called one-way selection statement.

Syntax

If(condition)Then 'statement(s) 'will execute if the condition is true End If

How "if" statement works

 (^) If the expression is evaluated to nonzero (true) then

if block statement(s) are executed.  (^) If the expression is evaluated to zero (false) then

Control passes to the next statement following it.

If-else statement

The if-else statement is used to execute the code if condition is true or false. It is also called two-way selection statement.

Syntax

If(condition)Then 'statement(s) will execute if the condition is true Else 'statement(s) will execute if the condition is false End If

How "if..else" statement works..

 (^) If the expression is evaluated to nonzero (true) then if block

statement(s) are executed.  (^) If the expression is evaluated to zero (false) then else block

statement(s) are executed.

If..else If

The if-else-if statement is used to execute one code from multiple conditions. It is also called multipath decision statement. It is a chain of if..else statements in which each if statement is associated with else if statement and last would be an else statement.

Syntax if(condition_1) {

// this block will execute // when condition_1 is true

} else if(condition_2) {

// this block will execute // when condition2 is true

}

………

. else {

// this block will execute when none // of the condition is true

}

}

Example (8)

Algorithm & Flowchart to find the smallest of two numbers :

Algorithm & Flowchart to find the largest of two numbers:

Tasks

1. Find Even numbers between 1 to 50

2. write algorithm and drow flowchart to find the result

of equation:

Step1: Start

Step2: Read/input x

Step3: if X greater than or equal zero then

F=X

Else

F=-X

End if

Step4: Print F

Step5: End

Example 10

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.

Algorithm Step 1: Input M1, M2, M3, M

Step 2: GRADE = (M1+M2+M3+M4)/

Step 3: if (GRADE < 60) then

Print “FAIL”

else

Print “PASS”

Endif

Step 4: Stop

Example 11

Write an algorithm that accept an integer number from the user, in case of the number is Positive, check and print out whether it is Even or Odd number.

Algorithm

Step 1: Read number from user say N Step 2: If ( N > 0) then If (N % 2 == 0) then Print “Number is Even” else Print “Number is Odd” End if End if Step 4: Stop

Example 12

Write An Algorithm That Read Student's Mark As Integer Then Print The Equivalent Grade Depends On The Following Table:

Algorithm Step 1: Start Step 2: Read mark Step 3: If ( mark < 60 ) then Print “Fail“ Else If ( mark < 65 ) then Print “Accept“ Else If ( mark < 75) then Print “Good“ Else If ( mark < 85) then Print “ Very Good“ Else If ( mark <= 100) then Print “ Excellent“ Else Print "Invalid Mark! Try again!" Step 4: stop

Example 13

Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation:

ax^2 +bx+c= Where:

d = √b^2 − 4ac , and the roots are:

x1 =

(–b + d)

2a

and x2 =

(–b− d)

2a

Algorithm:

sqrt (b*b – 4 * a * c)

0≤Mark<60 60≤Mark<65^ 65≤Mark<75^ 75≤Mark<85^ 85≤Mark^ ≤^100 Fail OK Good Very Good Excellent

Switch Statement

Switch statement acts as a substitute for a long if-else-if that is used to test a list of cases. A switch statement contains one or more case labels which are tested against the switch expression. When the expression match to a case then the associated statements with that case would be executed.

Syntax

Switch (expression)

{

case value1:

//Statements

break;

case value 2:

//Statements

break;

case value 3:

//Statements

case value n:

//Statements

break;

Default:

//Statements

}

Example 14

Write algorithm which read the numbers from 1 to 7 and display their correspondence day of week.

Step 1: Start Step2: read integer number say (day) Step3: Switch (day) { Case 1 : Print “Saturday“ break; Case 2 : Print “Sunday“ break; Case 3 : Print “Monday“ break; Case 4 : Print “Tuesday“ break; Case 5 : Print “Wednesday“ break; Case 6 : Print “Thursday“ break; Case 7 : Print “Friday“ break; Default : Print “Invalid day“ break; }

Step4: stop