more on psuedo-code sangeetha parthasarathy 1/23/01

5
More on Psuedo-Code Sangeetha Parthasarathy 1/23/01

Upload: jordan-kearney

Post on 26-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More on Psuedo-Code Sangeetha Parthasarathy 1/23/01

More on Psuedo-Code

Sangeetha Parthasarathy

1/23/01

Page 2: More on Psuedo-Code Sangeetha Parthasarathy 1/23/01

ModulesReusing work:

Sometimes, one finds that an algorithm has to do same thing

in different places. For example: To calculate average of the

grades for students. One does not how many students in

Advance, so we use an module that accepts the grades of

number of students and finding their average. We could

redefine the same set of statements of n of students, but that

would be a repetition. If a change is to made in the statement,

it would led to the problem of changing it certain places and

forget to change in some places. This would led to algorithm

in inconsistent state.

Page 3: More on Psuedo-Code Sangeetha Parthasarathy 1/23/01

To avoid the above problems, one includes modules in psuedo-

code. A module consists of three components:

• Input: Information that must be supplied to the module from outside. If module does need input from outside, then input is specified none.

• Process: A logic (specification) stating what the module does.

• Output: A result achieved due to processing performed in the module. The output is a value can be assigned to a variable. If module does not produce any value, then output is specified as none. If module returns a value, then there must be RETURN statement.

Page 4: More on Psuedo-Code Sangeetha Parthasarathy 1/23/01

An Example of ModuleFor example we might include to accept student number :

Module:

Name: getStudentNumber

Input:None

Output:studentNumber

Process: PRINT(“Please enter a student number”)

READ(studentNumber)

RETURN(studentNumber)

Page 5: More on Psuedo-Code Sangeetha Parthasarathy 1/23/01

RecursionAchieve Repetition in alternate way:

You have seen in earlier sessions that repetition can be achieved

by “WHILE” or “FOR” loops. However there is alternate to

achieve repetition called “recursion”. Recursion is used in

Modules that take at least one input. A recursive module always

directly returns an output when some condition involving

input(s) is true.