lbeps session 8

17
Ver. 1.0 Logic Building and Effective Problem Solving Slide 1 of 17 Objectives In this session, you will learn to: Identify repetitive processes Work with arrays Manipulate arrays using loops

Upload: kannadhasan-dass

Post on 09-Dec-2015

267 views

Category:

Documents


13 download

DESCRIPTION

fdgfsd

TRANSCRIPT

Page 1: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 1 of 17

Objectives

In this session, you will learn to:Identify repetitive processes

Work with arrays

Manipulate arrays using loops

Page 2: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 2 of 17

The repeat…until Loop

repeat…until loop: Body executes at least once regardless of the condition

Condition is evaluated after the body is executed once

Syntax::

repeat

begin

//Body of the repeat...until loop

end until (condition)

:

The following embedded Word document contains the flowchart to depict the repeat…until loop syntax.

Repeat Until

Page 3: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 3 of 17

The repeat…until Loop (Contd.)

The following embedded Word document contains the flowchart to calculate the average score of 30 students.

The following embedded Word document contains the pseudocode segment to calculate the average score of 30 students.

Average Score Flowchart

Average Score Pseudocode

Page 4: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 4 of 17

Working with Arrays

Array:Store more than one value at a time

Collection of homogeneous data types

Elements are stored in adjacent memory locations

Elements are accessed using subscript\index

First element has an index 0

Last element has an index one less than the size of the array

Page 5: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 5 of 17

Declaring an Array

Array:Should be declared before use

Syntax:

<data_type> <variable_name>[<size>]

Page 6: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 6 of 17

Declaring an Array (Contd.)

The following pseudocode segment declares a numeric array of size five:begin

numeric arr[5]...............

end

The following figure shows a schematic representation of the array elements in the memory.

arr[0] arr[1] arr[2] arr[3] arr[4]

Page 7: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 7 of 17

Initializing and Assigning Values to an Array

Array initialization:Process of populating an array with values

Can be done in the following ways:During declaration

After declaration

During Declaration After Declaration

beginnumeric arr[5] = {14, 15, 17, 45, 81}..........End

beginnumeric arr[5]display ‘Enter the values for an array’accept arr[0]accept arr[1]accept arr[2]accept arr[3]accept arr[4]end

Page 8: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 8 of 17

Initializing and Assigning Values to an Array (Contd.)

Array elements are displayed using the display keyword.

The following pseudocode initializes and displays the array elements:begin

numeric arr[5]arr[0] = 14arr[1] = 15arr[2] = 17arr[3] = 45arr[4] = 81display arr[1]display arr[2]

end

Page 9: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 9 of 17

Quiz #1

Page 10: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 10 of 17

Quiz

Which of the following constructs executes until the condition is false and breaks if the condition becomes true?1. for2. switch…case3. repeat…until4. if

Solution:repeat…until

Page 11: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 11 of 17

Quiz (Contd.)

Which of the following options will you use to initialize an array with the values entered by the user?1. display2. begin3. end4. accept

Solution:accept

Page 12: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 12 of 17

Quiz (Contd.)

Which of the following options is used to access a particular array element?1. subscript2. accept3. size

Solution:subscript

Page 13: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 13 of 17

Manipulating Arrays Using Loops

Let us see how to manipulate arrays using

loops.

Page 14: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 14 of 17

Manipulating Arrays Using Loops (Contd.)

The following embedded Word document shows the pseudocode to accept and display the marks of five subjects.

Use of array

Page 15: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 15 of 17

Exercises

Let us practice the concepts learned by solving a problem.

Page 16: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 16 of 17

Draw a flowchart and write the pseudocode to accept five numbers and display the total number of odd and even numbers.

(Allocated time to solve the problem is 40 minutes.)

Problem Statement

Use LBEPS Session8 Simulation1

Page 17: LBEPS Session 8

Ver. 1.0

Logic Building and Effective Problem Solving

Slide 17 of 17

In this session, you learned that:In a repeat…until loop, the body of the loop is executed at least once, regardless of the condition.

An array is a collection of elements of a single data type stored in adjacent memory locations.

An array element can be accessed using subscript\index.

Subscript\index specifies the position of an element within the array.

The first element of an array has an index 0 and the last element has an index one less than the size of the array.

An array should be declared before use.

An array can be initialized in two ways:During declaration

After declaration

Summary