• No results found

Problem Solving Skills Spring 2017

N/A
N/A
Protected

Academic year: 2020

Share "Problem Solving Skills Spring 2017"

Copied!
75
0
0

Loading.... (view fulltext now)

Full text

(1)

Problem Solving Skills

Spring 2017

Introduction to Information and Communication Technologies

CSD-102

(2)

Outline

Information Processing Cycles

Problem Solving

Problem

Logic Building

Structure theorem

Pseudo codes

Flowcharts

(3)

Information Processing Cycle

A computer is a machine that, under a program’s direction and control, perfroms

four basic operations:

Input

Processing

Output

Storage

A program is a sequence of instructions that tells

the computer how to perform these four operations

in order to accomplish a task.

(4)

1

st

Basic Operation : Input

When a computer is required to take an input from a particular source,

whether it is a terminal, a disk or any other device, the verbs Read and Get are

used.

Example :

You are writing an essay in MS word

(5)

2

nd

Basic Operation : Output

When a computer is required to provide output to a device, the verbs Print,

Write, Output, or Display are used in pseudo code.

Usually an output Prompt instruction is required before an input Get

instruction.

Example :

You are viewing and printing your essay

Computer displays / writes the added result of 2 values

(6)

3

rd

Basic Operation : Processing

Arithmetic Operations:

Mathematical calculation, Formula, and for these, a programmer uses either actual mathematical symbols or the words for those symbols.

Comparison or Logical Operations:

A computer can compare two variables and select one or two alternate actions

Example:

X = 5, Y = 7 X > Y OR X < Y Z = X + Y

Z = 5 + 7 Z = 12

(7)

4

th

Basic Operation : Storage

An information is stored in some location in memory called Variable.

3 situations when need to store a value to a variable or memory location:

To give data an initial value in pseudo code, the verbs Initialize or Set are used

To assign a value as a result of some processing the symbols '=' or ‘ ' are written

To keep a variable for later use, the verbs Save or Store are used

Example:

You are downloading a song in your laptop

Z = X + Y or Z X + Y

(8)

Problem to Solve

What steps would you propose to solve the following problem?

(9)

Problem to Solve

Problem:

Hair are dirty

Solution:

Wash the hair

How:

Wash the hair Algorithm

Algorithm

Turn on water tab

Wet you hair

Apply shampoo

Leather

Rinse hair

Dry off

(10)

Steps in Problem Solving (in computer

programming)

Computer programming can be divided into two phases:

Problem solving phase

Make an ordered sequence of steps that solves a problem

these sequence of steps is called an algorithm

Implementation phase

(11)

Steps in Problem Solving

First produce a general algorithm (one can use pseudocode)

Refine the algorithm successively to get step by step detailed algorithm that

is very close to a computer language.

Pseudocode is an artificial and informal language that helps programmers

(12)

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.

(13)

Pseudocode

Pseudocode:

Input a set of 4 marks

Calculate their average by summing and dividing by 4

if average is below 50

Print “FAIL”

else

(14)

Algorithm

Detailed Algorithm

Step 1:

Input M1,M2,M3,M4

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

Step 3: if (GRADE < 50) then

Print “FAIL”

else

Print “PASS”

endif

(15)

Characteristics of Algorithm

Definite and having Input and Output

Well-ordered

The steps are in a clear order

Unambiguous

The operations described are understood by a computing agent without further simplification.

Effectively Computable

The computing agent can actually carry out the operation

Algorithms can be executed by a computing agent which is not necessarily a computer

(16)

Rules of Algorithm

For Input:

Use keyword “Input” or “Get” followed by a list of variables separated by a single comma.

Example

Input a Input a , b Get a Get a,b

For Output

:

Use keyword “Output”, “Display” or “Print” followed by a variable name or text. Enclose

“text/message” in inverted commas. Do not enclose variable name in inverted commas.

Example

Good Practice:

Show a message

before to prompt user

for input.

(17)

Rules of Algorithm

Storage/ Assignment

Use the keyword “Set” in combination with “=” or “:=” OR use keyword “=”, “:=” or “<-”

Example

Set X=8

X=8

Set X:=8

X:= 8

X<- 8

01/03/2017 17

Good Practice:

• Number your steps.

• Indicate Start and End

(18)

Addition of Two Numbers

Input: number1 and number2

Output: Sum of number1 and number2

Steps:

1.

Start

2.

Input number1 , number2

3.

Sum = number1 + number2

4.

Display Sum

(19)

Problem Solving

Structure Theorem

Tools and techniques for solving a problem

01/03/2017 19

(20)

What is the Structure theorem

It states that it is possible to write any algorithm by using only three basic control

structures.

Sequence

I have to study classes from grade 1 to grade 8.

I cannot skip any class in order to reach in grade 8.

Repetition

If I am fail in a grade I have to repeat it until pass.

Selection

(21)

Decision/ Selection

Sometimes we need to put certain condition before performing some action, then action will

depend upon the condition if its fulfilled or not Selection statements help us to get this thing

done

This construct represents the decision making abilities of the computer to compare two

pieces of information and select one of two alternative actions.

In pseudocode, selection is represented by the keywords IF, THEN, ELSE and ENDIF

An IF statement always has a condition to check, often a comparison between a variable and a number.

The IF statement also must specify what to do if the condition/comparison is true.

These instructions (for “true”) may come after the word THEN, or they may simply be listed.

(22)

Types of Selection

IF – THEN – ENDIF(Single IF)

Single IF selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false.

IF – ELSE – ENDIF (Double IF)

The IF-ELSE selection statement performs an action if a condition is true and performs a different action if the condition is false

IF – ELSE IF – ELSE – ENDIF (Multiple IF)

The IF – ELSE IF – ELSE selection statement performs one of many different actions, depending on the value of an expression.

Switch (Alternate to Multiple IF)

(23)

Rules for Selection statement

One Option IF (condition) then <<steps>> Endif

Two Options IF(condition) then <<steps>> Else <<steps>> Endif

Multiple Conditions IF (condition) then <<steps>>

Else if(condition) then <<steps>>

Else if (condition) then <<steps>>

Else <<steps>> Endif

(24)

Decision/ Selection

Problem: Input the marks of the students and display pass if the marks are 50 else display fail. Start

Step 1: Input Marks

Step 2: IF Marks > 50 THEN

Step 3:

Display “Pass”

Step 4: ELSE

Step 5: Display “Fail” Step 6: ENDIF

(25)

Repetition

A loop is a repetition of all or part of the commands in a program.

A loop often has a counter (a variable) and continues to repeat a specified number of times.

A loop may also continue till than a condition is true or until a certain condition is met (e.g.,

until the end of a file or until a number reaches a set limit)

Example

WHILE condition p is true

Statement(s) to execute

ENDWHILE

(26)

Types of Repetition

WHILE

while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

DO WHILE

The DO WHILE statement performs the action (or group of actions) in its body at least once.

FOR

The FOR loop allows code to be repeatedly executed.

(27)

While Loop

The while loop is used to repeat a section of code an unknown number of times until a

specific condition is met.

While loops execute blocks of code over and over again.

The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish

its goal.

Algo rules:

while ( condition ) <<steps>> end while

The "something" should eventually result in the condition being false

(28)

Example: While Loop

while (condition)

action

How it works:

• if condition is true then execute action

• repeat this process until condition evaluates to false

action is either a single statement or a group of statements.

Example

while (number<10)

Display “Hello number ” number “player” Number=Number +1

(29)

Do While Loop

The DO WHILE statement performs the action (or group of actions) in its body at

least once.

Do

<<steps>>

While (condition)

Example

number =1

Do

Display “Hello number ” number “player” Number=Number +1

while (number<10)

(30)

For Loop

A for loop is classified as an iteration statement.

Unlike many other kinds of loops, such as the while loop, the for loop is often

distinguished by an explicit loop counter or loop variable.

This allows the body of the for loop to know about the sequencing of each iteration.

For loops are also typically used when the number of iterations is known before

entering the loop.

For loops are the shorthand way to make loops when the number of iterations is

known, as a for loop can be written as a while loop.

(31)

Example

RULES

For (initialize; condition; change)

<<steps>>

End for

Example

For (number=1; number<10;number++)

Display “Hello number ” number “player”

(32)

For Vs. While

For loop is used when you know the number of iterations you have to make,

mean when you know how many times to execute a loop.

WHILE is used when you are not sure about the iterations but you know what

the condition is and then you can loop that till the condition is met.

(33)

Repetition

Pretest loop: evaluation occurs before the statements within the loop are processed

Posttest loop: evaluation occurs after the statements within the loop are processed

The step at which the loop starts is called its "entrance"

The last step performed before completion is called its "exit".

The conditional test that controls the exit from a loop should be placed either immediately following the entrance to the loop or following all steps in its body, but never in the middle of the body.

Loops with tests at entrance are said to pretest i.e while-do.

Loops with tests after entire body are said to posttest i.e do-while.

It is imperative that at least one statement within the statement block alter the condition and eventually render it false, otherwise the logic may result in an endless loop.

(34)

Repetition

Example

Set student_total to 0

WHILE student_total < 10

Read student record

Print student name and address Add 1 to student_total

ENDWHILE

The variable student total is initialized before the loop condition is executed. The student

total variable is incremented within the body of the loop so it will eventually stop.

(35)

Modules

Modules break an algorithm into logical parts (like your groups)

Helps with Clarity and Understandability

Modules can be reused

Within the same algorithm

In a different algorithm

In Programming Modules can be called:

Sub-routines (in older languages)

Functions (in procedural languages like C/C++)

Methods (in object oriented languages like Java)

(36)

Summary

In this lecture, we have covered:

What is Problem Solving and its Logic Building

What is Structure theorem

What is algorithm and how to represent it in Pseudo codes and flow chart

(37)

Flow Chart

01/03/2017 37

(38)

Quick Recap

Pseudo-code

• High level description of algorithm…

• intended for human reading

• but structured like a programming language

Modules (subroutines/ functions/ methods) • Break down bigger algorithms into chunks

• Improves Clarity and Reuse

Variables

• Are named things with a value (like in algebra)

• Can make algorithms more flexible

(39)

Pseudo code & Flowchart

There are two commonly used tools to help to build logic (algorithm).

Pseudo code is an artificial and informal language that helps programmers develop algorithms.

Pseudo code may be an informal English, combinations of computer languages and spoken language. Whatever works for you.

A Flowchart is another algorithm but graphical that shows logic solution.

Emphasizes individual steps and their interconnections.

A flowchart must have a start and stop.

A step in a flowchart must connect i.e. You can’t leave a step “hanging” with no connection. e.g. control flows from one action to the next

01/03/2017

(40)

What is flow chart?

The flowchart is one of the most basic methods of representing algorithms. It

is useful as a precise method of explanation in some circumstances.

A flowchart is a diagrammatic\pictorial representation of the operations

(41)

Flow chart Symbols

Start/End

Used at the beginning and end of each flowchart.

Input/Output

Shows when information/data comes into a program or is printed out.

Process

Used to show calculations, storing of data in variables, and other “processes” that take

place within a program.

(42)

Flow chart Symbols

Decision

Used to show that the program must decide whether something (usually a comparison

between numbers) is true or false. YES and NO (or T/F) branches are usually shown.

Connector

Used to show that flowchart continues on another page.

Flow Direction

Show you how you have to move

X>7?

Y

(43)

Example

Algorithm

Step 1: Input W,L

Step 2: A ← L x W

Step 3: Print A

START Input W, L A

L x W STOP Output W, L

(44)

Example

Input M1,M2,M3,M4

GRADE ← (M1+M2+M3+M4)/4

if (GRADE <50) then

Print “FAIL”

else

Print “PASS”

endif

START Input M1,M2,M3,M4 GRADE←(M1+M2+M3+M4)/4 IS GRADE<5 0 Y N Print

(45)

Example

Algorithm

Step 1: Input Lft

Step 2: Lcm ← Lft x 30

Step 3: Print Lcm

Flowchart

START Input Lft Lcm

Lft x 30 STOP Output Lcm

(46)
(47)

Trace Tables & Dry Run

Algorithm

A sequence of steps designed to perform a particular task

Dry run

Working through a section of a program manually

Trace table

A table constructed with a column to identify the instruction executed and columns for the contents of each variable

Variable

The identifier associated with a particular memory location used to store data

Constant

A data item with a fixed value

(48)

Example 1

Algorithm

1.

Start

2.

Set x := 0

3.

input y

4.

x := y * 2

5.

Output x

6.

End

– Trace table

– Let user Input for y s 5

Step Algorithm Lines

X

Y

Output

1

Start

-

-

-2

Set x := 0

0

-

-3

input y

0

5

-4

x := y * 2

10

5

-5

Output x

10

5

10

6

End

-

-

-A - indicates Nil

(49)

Example 2

01/03/2017 49

– Algorithm : Take five inputs from user, calculate and display their sum and average

(Without using loop)

1.

Start

2.

input : num1, num2, num3, num4 and num5

3.

Set sum := 0 , average := 0 , totalNumbers :=5

4.

input in num1, num2, num3, num4 and num5

5.

sum := num1 + num2 + num3 + num4 + num5

6.

average := sum / totalNumbers

7.

Print “Sum is ” sum

8.

Print “Average is ” average

9.

End

(50)

Example 2

Trace table: Take five inputs from user, calculate and display their sum and

average (Without using loop)

– Assume input: 25, 17, 34, 9, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum average totalNu mbers

Output

1 Start - - -

-2 input : num1, num2, num3, num4 and

num5

- - -

-3 Set sum := 0 , average := 0 , totalNumbers :=5

- - - 0 0 5

-4 input num1, num2, num3, num4 and

num5

25 17 34 9 75 0 0 5

-5 sum := num1 + num2 + num3 + num4 +

num5

25 17 34 9 75 0 0 5

-6 average := sum / totalNumbers 25 17 34 9 75 160 32 5

-7 Print “Sum is ” sum 25 17 34 9 75 160 32 5 Sum is 160

8 Print “Average is ” average 25 17 34 9 75 160 32 5 Sum is 160

(51)

Example 3

01/03/2017 51

– Algorithm : Take five inputs from user, calculate

and display their sum and average (Without

using loop)

1.

Start

2.

Num=0

3.

Set sum := 0 , average := 0 , totalNumbers :=5

4.

Input num

5.

sum := sum + num

6.

Input num

7.

sum := sum + num

8.

Input num

9.

sum := sum + num

10.

Input num

11.

sum := sum + num

12.

Input num

13.

sum := sum + num

14.

average := sum / totalNumbers

15.

Print “Sum is ” sum

16.

Print “Average is ” average

(52)

Example 3

Take five inputs from user using only 1 variable for input, calculate and display

their sum and average (Without using loop)

– Assume input: 25, 17, 34, 9, 75

# Algorithm Lines num sum average totalNumbers Output

1 Start - - - -

-2 num=0 0 - - -

-3 Set sum := 0 , average := 0 , totalNumbers :=5 - 0 0 5

-4 Input num 25 0 0 5

-5 sum := sum + num 25 25 0 5

-6 input num 17 25 0 5

-7 sum := sum + num 17 42 0 5

-8 Input num 34 42 0 5

-9 sum := sum + num 34 76 0 5

(53)

-Example 3

01/03/2017 53

Take five inputs from user using only 1 variable for input, calculate and display

their sum and average (Without using loop)

– Assume input: 25, 17, 34, 9, 75

# Algorithm Lines num sum average totalNumbers Output

12 Input num 75 85 0 5

-13 sum := sum + num 75 160 0 5

-14 average := sum / totalNumbers 75 160 32 5

-15 Print “Sum is ” sum 75 160 32 5 Sum is 160

16 Print “Average is ” average 75 160 32 5 Sum is 160

Average is 32

(54)

-Example 4

1. Start

2. Set num1=0, num2=0, num3=0, num4=0, num5=0

3. Set totalSum := 0 , sumOfEven := 0, average := 0 ,

totalNumbers := 5

4. input num1, num2, num3, num4 and num5

5. sum := num1 + num2 + num3 + num4 + num5

6. average := sum / totalNumbers

7. If (num1 mod 2 = 0 )

8. Then sumOfEven := sumOfEven +num1

9. End if

10. If (num2 mod 2 = 0 )

11. Then sumOfEven := sumOfEven +num2

12. End if

13. If (num3 mod 2 = 0 )

14. Then sumOfEven := sumOfEven +num3

15. End if

16. If (num4 mod 2 = 0 )

17. Then sumOfEven := sumOfEven +num4

18. End if

19. If (num5 mod 2 = 0 )

20. Then sumOfEven := sumOfEven +num5

21. End if

22. Print “Total Sum is ” sum

23. Print “Average is ” average

24. Print “Sum of evens is ” sumOfEven

– Algorithm : Take five inputs from user using only 1 variable

for input, calculate and display their totalSum, sumOfEven

and average (Without using loop)

(55)

Example 4

01/03/2017 55

– Algorithm : Take five inputs from user using only 1 variable for input, calculate and

display their totalSum, sumOfEven and average (Without using loop)

– Assume input: 24, 19, 34, 18, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve n

average totalNumb ers

1 Start - - -

-2 Set num1=0, num2=0, num3=0, num4=0, num5=0

0 0 0 0 0 - - -

-3 Set sum := 0 , sumOfEven := 0, average := 0 , totalNumbers := 5

- - - 0 0 0 5

4 input in num1, num2, num3, num4 and

num5

24 19 34 18 75 0 0 0 5

5 sum := num1 + num2 + num3 + num4 +

num5

24 19 34 18 75 0 0 0 5

6 average := sum / totalNumbers 24 19 34 18 75 170 0 34 5

7 If (num1 mod 2 = 0 ) 24 19 34 18 75 170 0 34 5

8 Then sumOfEven := sumOfEven +num1 24 19 34 18 75 170 24 34 5

(56)

Example 4

01/03/2017 56

– Algorithm : Take five inputs from user using only 1 variable for input, calculate and

display their totalSum, sumOfEven and average (Without using loop)

– Assume input: 24, 19, 34, 18, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve n

average totalNumb ers

10 If (num2 mod 2 = 0 ) 24 19 34 18 75 170 24 34 5

11 Then sumOfEven := sumOfEven

+num2

24 19 34 18 75 170 24 34 5

12 End if 24 19 34 18 75 170 24 34 5

13 If (num3 mod 2 = 0 ) 24 19 34 18 75 170 24 34 5

14 Then sumOfEven := sumOfEven

+num3

24 19 34 18 75 170 58 34 5

15 End if 24 19 34 18 75 170 58 34 5

16 If (num4 mod 2 = 0 ) 24 19 34 18 75 170 58 34 5

17 Then sumOfEven := sumOfEven

+num4

24 19 34 18 75 170 76 34 5

18 End if 24 19 34 18 75 170 76 34 5

19 If (num5 mod 2 = 0 ) 24 19 34 18 75 170 76 34 5

(57)

Example 4

01/03/2017 57

– Algorithm : Take five inputs from user using only 1 variable for input, calculate and

display their totalSum, sumOfEven and average (Without using loop)

– Assume input: 24, 19, 34, 18, 75

# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve n

average totalNumb ers

22 Print “ Total Sum is ” sum 24 19 34 18 75 170 76 34 5

23 Print “Average is ” average 24 19 34 18 75 170 76 34 5

24 Print “ Sum of evens is ” sum 24 19 34 18 75 170 76 34 5

25 End - - -

-Output

22 Total Sum is 170 23 Total Sum is 170 Average is 34 24 Total Sum is 170 Average is 34 Sum of evens is 76

(58)

Example 5

Algorithm:

1. Start

2. Num=0

3. Set sum := 0 , average := 0 , totalNumbers := 5, loopCounter := 1

4. Repeat while (loopCounter <= totalNumbers)

5. Begin

6. input num

7. display “Enter input ” loop counter “:” 8. sum := sum + num

9. loopCounter = loopCounter + 1

10. End while

11. average := sum / totalNumbers

12. Print “Total Sum is ” sum

Take five inputs from user, calculate and display their

sum and average (With using loop)

(59)

Example 5

01/03/2017 59

Take five inputs from user, calculate and display their sum

and average (With using loop)

– Assume input: 25, 17, 34, 9, 75

# Algorithm Step

Loop Pass / Iteration

num sum average totalNumbers loopCounte r loopCounter <= totalNumbers 1 1 - - - -2 2 - 0 - - - - -3 3 - - 0 0 5 1 -4 4 - - 0 0 5 1 True 5 5 1 - 0 0 5 1 True 6 6 1 25 0 0 5 1 True 7 7 1 25 25 0 5 1 True 8 8 1 25 25 0 5 2 True 9 Repeat --- go to step 4 9 4 - 25 25 0 5 2 True 10 5 2 25 25 0 5 2 True

(60)

# Algorithm Step

Loop Pass / Iteration

num sum average totalNumbers loopCounte r loopCounter <= totalNumbers 11 6 2 17 25 0 5 2 True 12 7 2 17 42 0 5 2 True 13 8 2 17 42 0 5 3 True 9 Repeat --- go to step 4 14 4 - 17 42 0 5 3 True 15 5 3 17 42 0 5 3 True 16 6 3 34 42 0 5 3 True 17 7 3 34 76 0 5 3 True 18 8 2 34 76 0 5 4 True 9 Repeat --- go to step 4 19 4 - 34 76 0 5 4 True 20 5 4 34 76 0 5 4 True 21 6 4 9 76 0 5 4 True 22 7 4 9 85 0 5 4 True

(61)

01/03/2017 61

# Algorithm Step

Loop Pass / Iteration

num sum average totalNumbers loopCounte r loopCounter <= totalNumbers 9 Repeat --- go to step 4 24 4 - 9 85 0 5 5 True 25 5 5 9 85 0 5 5 True 26 6 5 75 85 0 5 5 True 27 7 5 75 160 0 5 5 True 28 8 5 75 160 0 5 6 True 9 Repeat --- go to step 4 29 4 - 75 160 0 5 6 False

Loop ended --- go to step 10

30 10 - 75 160 32 5 6

-31 11 - 75 160 32 5 6

-32 12 - 75 160 32 5 6

(62)

-Step

Output for total algorithm

6 Enter input 1: 25 6 Enter input 2: 17 6 Enter input 3: 34 6 Enter input 4: 9 6 Enter input 5: 75 11 Total Sum is 160 12 Total Sum is 160 Average is 32

(63)

Example 5.1

01/03/2017 63

– Algorithm: (slight modification in condition and loopCounter value)

1. Start 2. num=0

3. Set sum := 0 , average := 0 , totalNumbers := 5, loopCounter := 0 4. Repeat while (loopCounter < totalNumbers)

5. Begin

6. loopCounter = loopCounter + 1 7. input num

8. Display “Enter input ” loop counter “:” 9. sum := sum + num

10. End while

11. average := sum / totalNumbers 12. Print “Total Sum is ” sum

13. Print “Average is ” average 13. End

Take five inputs from user, calculate and display their

sum and average (With using loop)

(64)

Example 5

Take five inputs from user, calculate and display their sum

and average (With using loop)

– Assume input: 25, 17, 34, 9, 75

# Algorithm Step

Loop Pass / Iteration

num sum average totalNumbers loopCounte r loopCounter <= totalNumbers 1 1 - - - -2 2 - 0 - - - - -3 3 - - 0 0 5 0 -4 4 - - 0 0 5 0 True 5 5 1 - 0 0 5 0 True 6 6 1 - 0 0 5 1 True 7 7 1 25 0 0 5 1 True 8 8 1 25 25 0 5 1 True 9 Repeat --- go to step 4 9 4 - 25 25 0 5 1 True

(65)

01/03/2017 65

# Algorithm Step

Loop Pass / Iteration

num sum average totalNumbers loopCounte r loopCounter <= totalNumbers 11 6 2 25 25 0 5 2 True 12 7 2 17 25 0 5 2 True 13 8 2 17 42 0 5 2 True 9 Repeat --- go to step 4 14 4 - 17 42 0 5 2 True 15 5 3 17 42 0 5 2 True 16 6 3 17 42 0 5 3 True 17 7 3 34 42 0 5 3 True 18 8 2 34 76 0 5 3 True 9 Repeat --- go to step 4 19 4 - 34 76 0 5 3 True 20 5 4 34 76 0 5 3 True 21 6 4 34 76 0 5 4 True 22 7 4 9 76 0 5 4 True 23 8 4 9 85 0 5 4 True

(66)

# Algorithm Step

Loop Pass / Iteration

num sum average totalNumbers loopCounte r loopCounter <= totalNumbers 9 Repeat --- go to step 4 24 4 - 9 85 0 5 4 True 25 5 5 9 85 0 5 4 True 26 6 5 9 85 0 5 5 True 27 7 5 75 85 0 5 5 True 28 8 5 75 160 0 5 5 True 9 Repeat --- go to step 4 29 4 - 75 160 0 5 5 False

Loop ended --- go to step 10

30 10 - 75 160 32 5 5

-31 11 - 75 160 32 5 5

-32 12 - 75 160 32 5

(67)

-01/03/2017 67

Step

Output for total algorithm

6 Enter input 1: 25 6 Enter input 2: 17 6 Enter input 3: 34 6 Enter input 4: 9 6 Enter input 5: 75 11 Total Sum is 160 12 Total Sum is 160 Average is 32

(68)

Same problem can be done using for loop. Increment

statement will work as last line in the loop.

(69)

Example 6

01/03/2017 69

– Algorithm:

1.

Start

2.

input num=0

3.

Set sum := 0 , average := 0 , totalNumbers :=

0, loopCounter := 0

4.

Repeat while (true)

5.

Begin

6. loopCounter = loopCounter + 1

7. input num

8. display “Enter input ” loop counter “:”

9. if (num == -1)

10. then break

11. end if

Take as many inputs as user wants using -1 as sentinel

value, calculate and display their sum and average

10.

end if

11.

totalNumbers := totalNumbers +1

12.

sum := sum + num

13.

End while

14.

average := sum / totalNumbers

15.

Print “Total Sum is ” sum

16.

Print “Average is ” average

(70)

Example 6: Trace table

Step

Output for total algorithm

Enter input 1: 25 Enter input 2: 17 Enter input 3: 34 Enter input 4: 9 Enter input 5: 75 Enter input 6: -1 Total Sum is 160 Total Sum is 160 Average is 32

(71)

Example 7

01/03/2017 71

– Algorithm:

1.

Start

2.

num=0

3.

Set sum := 0 , average := 0 , totalNumbers :=

0, loopCounter := 0

4.

Repeat while (true)

5.

Begin

6. loopCounter = loopCounter + 1

7. input num

8. display “Enter input ” loop counter “:”

9. if (num == -1)

10. then break

11. end if

Take as many inputs as user wants using -1 as sentinel value,

calculate and display sumOfEvens and sumOfOdds

10.

end if

11.

if (num mod 2 = 0)

12.

then sumOfEvens := sumOfEvens +num

13.

else sumOfOdds := sumOfOdds +num

14.

end if

15.

End while

16.

Print “Sum of Evens : ” sumOfEvens

17.

Print “Sum of Odds : ” sumOfOdds

(72)

Example 7: Trace table

Ste

p

Output for total algorithm

Enter input 1: 25 Enter input 2: 18 Enter input 3: 34 Enter input 4: 9 Enter input 5: 75 Enter input 6: -1 Sum of Evens : 52 Sum of Evens : 52 Sum of Oddss : 109

(73)

Practice Questions

Temperature converter

Currency converter

Leap Year calculator

Grade calculator

Number of days in a given month (if and switch)

Alphabet character is vowel or consonant (if and switch)

Palindrome tester

(74)

Practice Questions

Factorial of given number

Prime number

Fibonacci Series

(75)

Summary

In this lecture, we have covered:

Algorithm creation

Trace table and dry run

Use of loops and decisions

References

Related documents