Final Flowchart

Preview:

Citation preview

How Computers Solve Problems

• Computers also use Algorithms to solve problems, and change data into information

• Computers can only perform one simple step at a time

• Complex “Human” Algorithms must be broken down into simple step-by-step instructions BEFORE they can be translated into computer code

Algorithm

• It is the set of instructions to solve any problem. These are the steps followed after one another.

• These steps for solving a problem, to process some input for obtaining the desired result or output is called ALGORITHM

Characteristics

• It should have input• Steps that are included in the algorithm should

be executable by the computer.• Each step must be finite and small and

unambiguous• Every step of algorithm should yield some

result.• Should be written in a very simple language• Algorithm validation

Algorithm :Temperature Conversion

1. Read the numerical value of C

2. Calculate F using formula

F=9/5C+32

3. Write numerical value of C

4. Terminate the process

Algorithm: Average of two numbers

1. Input: Two numbers

2. Add the two numbers

3. Divide the result by 2

4. Return the result by step 2

5. End

Algorithm: Change a numeric grade to a letter grade.

Input: One number

1. if (the number is between 90 and 100, inclusive)then– 1.1 Set the grade to “A”

– End if

2. if (the number is between 80 and 89, inclusive)then– 2.1 Set the grade to “B”

– End if

3.if (the number is between 70 and 79, inclusive)then– 3.1 Set the grade to “C”– End if

4. if (the number is between 60 and 69, inclusive)then– 4.1 Set the grade to “D”– End if

5. If (the number is less than 60)then– 5.1 Set the grade to “F”– End if

6. Return the grade– End

FindLargestInput: 1000 positive integers

1. Set Largest to 02. Set Counter to 03. 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 CounterEnd while

4. Return LargestEnd

Algorithm:Algorithm: Find largest of 1000 numbersFind largest of 1000 numbers

FactorialInput: A positive integer num

1. Set FactN to 12. Set i to 13. while (i is less than or equal to num)

3.1 Set FactN to FactN x I 3.2 Increment iEnd while

4. Return FactNEnd

Algorithm :Algorithm : Iterative factorialIterative factorial

1. Start 2. Read the roll number and marks of 1 student. This is the

highest marks found so far as marks of only one student has been read

3. Is there any more student in the class4. If yes, then go to step 5 .If not ,then goto step 85. Read next roll number and its marks .Compare the marks of

this next roll number with the highest marks find so far6. If marks read in step 5 is greater than the highest marks found

so far , then erase the previously noted highest marks , and replace it by the marks read in step 5 .Also replace the roll number, if marks of step 5 are less than the highest marks, then do nothing.

7. Repeat the step 3 through 5 till all the roll numbers of class have been entered.

8. Print the roll number of the student with the highest marks and percentage of the student.

9. Stop

7. Repeat the step 3 through 5 till all the roll numbers of class have been entered.

8. Print the roll number of the student with the highest marks and percentage of the student.

9. Stop

Problem Solving

• Problem Solving is the ability to understand what you have, what you want, and creating a set of instructions to change what you have into what you want

• Good Problem Solving Skills are based on knowledge, experience and logic

• Good Programmers NEVER make assumptions

Flowcharts and Pseudo Code

• A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily

• There are two standards in programming:– FLOWCHARTS– PSEUDOCODE– ACTUAL CODE

Flowcharts

• A Flowchart is a Visual Representation of an algorithm

• A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data

• Flowcharts aid in breaking down a problem into simple steps

Advantages

• Each symbol denotes the instruction• Pictorial representation it becomes easy to understand

and explain the problem.• It gives clear picture of flow of data in any program• It helps the programmer to determine the type of

logic control to be used in the program• It is easy to detect the error with the help of flow

chart• It serves as a guiding document for program writing

or coding

Limitations

• Time consuming and laborious too especially for complex problems

• Redrawing is even more difficult and time consuming.

• Difficult to include new step in the existing flowchart

• If an algorithm has complex branches and loops , flowchart become very difficult to draw.

Pseudo Code

• “Pseudo” means “pretend” or “false”

• Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code

• Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does

Tips on Writing Good Pseudo Code• Use indention for improved clarity

• Do not put “code” in pseudo code – make your pseudo code language independent

• Don’t write pseudo code for yourself – write it in an unambiguous fashion so that anyone with a reasonable knowledge can understand and implement it

• Be consistent

• Prefer formulas over English language descriptions

The Three Computer Programming Constructs

• Any problem, regardless of how complex, can be broken down into three basic CONSTRUCTS

– SEQUENCE

– SELECTION

– ITERATION

THREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTS

Three constructs

Flowcharts for three constructs

Pseudocode for three constructs

Start or stop

Process

Input or output

Connector

Decision

Flow line

Off-page connector

Flowchart Elements

Flowchart and the selection control structure

Simple IF statement

Account_balance <

$300?

Service_charge = $5 Service_charge = $2

T F

Null ELSE statement

x<0

X= -x

T

F

Combined IF statement

Student = P/T ANDGender =

F ?

Increment Female_part_time_count

T

F

Nested IF statement

IncrementCounter_A

Record

Code =`A‘ ?

IncrementCounter_B

IncrementCounter_C

IncrementError_counter

Record

Code =`A‘ ?

Record

Code =`A‘ ?

T

T

T

F

F

F

Case Structure

CaseOf

variable

Statement_a Statement_b Statement_c Statement_d

Value 1 Value 2 Value 3 Value 4

Summation

More numbers

Product

Recommended