Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

30
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

Transcript of Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

Page 1: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

Modular Programming

ELEC 206

Computer Applications for Electrical Engineers

Dr. Ron Hayne

Page 2: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 2

Outline

Modularity Programmer Defined Functions Parameter Passing Storage Class and Scope

Page 3: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 3

Modularity

Modules Can be written and tested separately Testing is easier (smaller) Doesn't need to be retested Reduces length of program Hides details (abstraction)

Structure Charts Module structure of a program (not sequence)

Page 4: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 4

Programmer-Defined Functions

Function Definition Function Header Parameter Declarations Function Body Return Statement

return_type function_name(parameter declarations)

{

declarations;

statements;

}

Page 5: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 5

Example Function

/* This function converts degrees Celsius */

/* to degrees Fahrenheit. */

double Celsius_to_Fahr(double Celsius)

{

// Declare objects.

double temp;

// Convert.

temp = (9.0/5.0)*Celsius + 32;

return temp;

}

Page 6: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 6

Program Using a Function

/* Program chapter5_2 */

/* */

/* This program prints 21 values of the sinc */

/* function in the interval [a,b] using a */

/* programmer-defined function. */

#include <iostream>

#include <cmath>

using namespace std;

//Function Prototype

double sinc(double x);

Page 7: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 7

Program Using a Function

int main()

{

...

// Compute and print table of sinc(x) values.

cout << "x and sinc(x) \n";

for (int k=0; k<=20; k++)

{

new_x = a + k*x_incr;

cout << new_x << " " << sinc(new_x) << endl;

}

// Exit program.

return 0;

}

Page 8: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 8

Program Using a Function

/* This function evaluates the sinc function. */

double sinc(double x)

{

if (abs(x) < 0.0001)

return 1.0;

else

return sin(x)/x;

}

Page 9: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 9

Parameter Passing

Call by Value Value of the function argument passed to the

formal parameter of the function

Call by Reference Address of the function argument passed to the

formal parameter of the function

Page 10: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 10

Call by Value Example

/* Incorrect function to switch two values */

void switch(int a, int b)

{

int hold;

hold = a;

a = b;

b = hold;

return;

}

Page 11: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 11

Call by Reference Example

/* Correct function to switch two values */

void switch2(int& a, int& b)

{

int hold;

hold = a;

a = b;

b = hold;

return;

}

Page 12: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 12

Storage Class and Scope

Local Objects Defined within a function Can be accessed only within the function Automatic storage class (default) Static storage class (retained entire execution)

Global Objects Defined outside all functions Can be accessed by any function External storage class

Page 13: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 13

Global/Local Example

int count(0);

...

int main()

{

int x, y, z;

...

}

int calc(int a, int b)

{

int x;

count += x;

...

}

Page 14: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 14

Summary

Modularity Programmer Defined Functions Parameter Passing Storage Class and Scope

Page 15: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 15

Random Numbers

cstdlib int rand();

Generates integer between 0 and RAND_MAX void srand(unsigned int);

Specifies the seed for the random number generator

Generating random integers over a specified range (a, b) Modulus operator (%)

Page 16: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 16

Example

/* This program generates and prints ten random */

/* integers between user-specified limits. */

#include <cstdlib>

#include <iostream>

using namespace std;

// Function prototype.

int rand_int(int a, int b);

int main()

{

...

Page 17: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 17

Example

srand(seed);

...

// Generate and print ten random numbers.

cout << "Random Numbers: \n";

for (int k=1; k<=10; k++)

{

cout << rand_int(a,b) << ' ';

}

cout << endl;

// Exit program.

return 0;

}

Page 18: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 18

Example

/* This function generates a random integer */

/* between specified limits a and b (a<b). */

int rand_int(int a, int b)

{

return rand()%(b-a+1) + a;

}

Page 19: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Page 20: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 20

Problem Solving Applied

Instrumentation Reliability Problem Statement

Compare the analytical and simulation reliabilities for a series configuration with three components and for a parallel configuration with three components.

Input/Output Description

Component reliabilityAnalytical reliability

Number of trials

Random seedSimulation reliability

Page 21: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 21

Problem Solving Applied

Hand ExampleAnalytical Reliability

r = 0.8

Series Configuration r_series = r3 = 0.512

Parallel Configuration r_parallel = 3r - 3r2 + r3 = 0.992

Comp Comp Comp

Comp

Comp

Comp

Page 22: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 22

Problem Solving Applied

Hand ExampleSimulation Reliability (3 random trials)

0.939775 0.0422243 0.929037 0.817733 0.211689 0.9909 0.0377037 0.103508 0.407272

Series Configuration All must be less than or equal to 0.8 One success = 0.333333

Parallel Configuration Any must be less than or equal to 0.8 Three successes = 1.0

Page 23: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 23

Problem Solving Applied

Algorithm DevelopmentRead component reliability, number of trials, seedCompute analytical reliabilitiesWhile k <= number of trials

Generate three random numbers between 0 and 1 If each number <= component reliability

Increment series_success If any number <= component reliability

Increment parallel_success Increment k

Print analytical reliabilitiesPrint simulation reliabilities

Page 24: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

/* This program estimates the reliability *//* of a series and a parallel configuration *//* using a computer simulation. */

#include <iostream>#include <cstdlib>#include <cmath>using namespace std;

// Function prototypes double rand_float(double a, double b);

int main(){ // Declare objects. unsigned int seed; int n; double component_reliability, a_series, a_parallel, series_success=0, parallel_success=0, num1, num2, num3;

Page 25: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

// Get information for the simulation. cout << "Enter individual component reliability: \n"; cin >> component_reliability; cout << "Enter number of trials: \n"; cin >> n; cout << "Enter unsigned integer seed: \n"; cin >> seed; srand(seed); cout << endl;

// Compute analytical reliabilities. a_series = pow(component_reliability,3); a_parallel = 3*component_reliability - 3*pow(component_reliability,2) + pow(component_reliability,3);

Page 26: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

// Determine simulation reliability estimates. for (int k=1; k<=n; k++) { num1 = rand_float(0,1); num2 = rand_float(0,1); num3 = rand_float(0,1); if (((num1<=component_reliability) && (num2<=component_reliability)) && (num3<=component_reliability)) { series_success++; } if (((num1<=component_reliability) || (num2<=component_reliability)) || (num3<=component_reliability)) { parallel_success++; } }

Page 27: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

// Print results. cout << "Analytical Reliability \n"; cout << "Series: " << a_series << " " << "Parallel: " << a_parallel << endl; cout << "Simulation Reliability " << n << " trials\n"; cout << "Series: " << (double)series_success/n << " Parallel: " << (double)parallel_success/n << endl;

// Exit program. return 0;}/*----------------------------------------------------*//* This function generates a random *//* double value between a and b. */

double rand_float(double a, double b){ return ((double)rand()/RAND_MAX)*(b-a) + a;}/*----------------------------------------------------*/

Page 28: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 28

Testing

Page 29: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 29

Testing

Page 30: Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C5 30

Summary

Modularity Programmer Defined Functions Parameter Passing Storage Class and Scope Random Numbers Problem Solving Applied End of Chapter Summary

C++ Statements Style Notes Debugging Notes