Introduction to Algorithmic Processes CMPSC 201C Fall 2000

22
Introduction to Algorithmic Processes CMPSC 201C Fall 2000

description

Introduction to Algorithmic Processes CMPSC 201C Fall 2000. Administrative Issues. Exam 2 - Monday, November 6 at 8:15 to 9:30 in 102 and 105 Forum Sections 3, 4, 5, 6, 9,and 10 (John and Pyush) will be in 102 Forum and sections 1, 2, 7, 8, 11,and 12 (Anand and Nidhi) will be in 105 Forum. - PowerPoint PPT Presentation

Transcript of Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Page 1: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Introduction to Algorithmic Processes

CMPSC 201CFall 2000

Page 2: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Administrative Issues

Exam 2 - Monday, November 6 at 8:15 to 9:30 in 102 and 105 Forum

Sections 3, 4, 5, 6, 9,and 10 (John and Pyush) will be in 102 Forum and sections 1, 2, 7, 8, 11,and 12 (Anand and Nidhi) will be in 105 Forum.

Conflict form must be returned to me by noon on October 25!

Two practice exams will be on web and I-drive by October 25.

Page 3: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Types of parameters

Formal parameters(arguments) - parameters listed in the function heading

Actual parameters(arguments) - parameters listed in the function call

Page 4: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Example Cont.//Function to calculate miles per gallon for a tank of

gas…….

//

int mpg(double oldOdom, double newOdom,

double gallons) // function heading

{

int ans = (int) ((newOdom - oldOdom)/gallons));

return ans;

}

Page 5: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Scope Scope - portion of code in which the identifier name

is accessible.Local - limited to a specific block of statements in

which the identifier was declared (or defined for a function name)

Global - may be accessed from any portion of code once the identifier has been declared (usually at beginning of code).

Usually variables are local and constants are global. If you do use global variables, be VERY careful as

they can be affected in ways you did not expect.

Page 6: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Block

Statements that are enclosed braces { }.

May be body of function, loop, if-then, etc.

Page 7: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Scope Rules

A name that is declared within a block is local to that block and is accessible only within that block.

All other names are “inherited” from the immediate surrounding region.

Page 8: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Lifetime

Lifetime of a variable or constant is the time when memory has been allocated to it.

If the constant or variable is global, memory is always allocated to that constant or variable and the lifetime would be static.

If the constant or variable is local, then memory is only allocated when the block in which it was declared is executing and the memory is deallocated when the block is finished executing. In this case the lifetime would be automatic.

Page 9: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Program Example

Page 10: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Questions????

Page 11: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Multiple Results

Value- returning function returns one value to the function that called it.

However, sometimes more that one result is calculated and need to be passed back to the originating function.

Then use reference parameters (arguments) in the formal parameter list.

Page 12: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Example (pg. 149)// Add two vectors that are at right angles…..

// Pi is a global CONSTANT that was declared before

// main

void addVect (double a, double b //vectors at rt <‘s

double& rMagnitude // output

int& rDirection ) // output - direction

{ double rDirRadians; //local variable

rMagnitude = sgrt(a * a + b * b);

rDirRadians = atan (b / a);

rDirection = (int) (180 / Pi * rDirRadians + 0.5); }

Page 13: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Function Heading Pass by value

int mpg(double oldOdom, double newOdom,

double gallons)

Pass by reference

void addVect (double a, double b //vectors at rt <‘s

double& rMagnitude // output

int& rDirection ) // output - direction

Page 14: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Function Prototype

Pass by value

int mpg (double, double, double);

Pass by reference

void addVect (double, double, double&, int&);

Page 15: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Function call

Pass by value

lastTankMPG = mpg(10502.5, 10754.6, 10.0);

Pass by reference

addVect (aVect, bVect, rMag, rDir);

Page 16: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Pass by reference

When using pass by reference, both identifiers (the one in the function call and the one in the function heading) refer to the same memory location.

Therefore, when formal parameter (the one in the function) changes what is stored in the memory location, the same change occurs for the actual parameter

Page 17: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Formal and Actual Parameters

Formal Parameter Actual Parameter

Pass by value value, variable, or an

expression

Pass by reference variable

Page 18: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Value vs Nonvalue

Use value-returning functions (int, float, double, etc.) when one value needs to be passed back to the calling function.

Use nonvalue-returning functions (void) for printouts, pass multiple results, modify actual parameters.

See table 5.2 on page 153

Page 19: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Questions????

Page 20: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Recursive Function

A function that calls itself or is part of a cycle of calls.

f1

f1

f2f3

Page 21: Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Non-programming Example

Recursive definition of a cow.A cow is a four-legged animal whose mother was

a cow.

Page 22: Introduction to Algorithmic Processes CMPSC 201C Fall 2000