Download presentation
Presentation is loading. Please wait.
Published byGloria Hart Modified over 9 years ago
1
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
2
206_C52 Outline Modularity Programmer Defined Functions Parameter Passing Storage Class and Scope
3
206_C53 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)
4
206_C54 Programmer-Defined Functions Function Definition Function Header Parameter Declarations Function Body Return Statement return_type function_name(parameter declarations) { declarations; statements; }
5
206_C55 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; }
6
206_C56 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 using namespace std; //Function Prototype double sinc(double x);
7
206_C57 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; }
8
206_C58 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; }
9
206_C59 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
10
206_C510 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; }
11
206_C511 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; }
12
206_C512 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
13
206_C513 Global/Local Example int count(0);... int main() { int x, y, z;... } int calc(int a, int b) { int x; count += x;... }
14
206_C514 Summary Modularity Programmer Defined Functions Parameter Passing Storage Class and Scope
15
206_C515 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 (%)
16
206_C516 Example /* This program generates and prints ten random */ /* integers between user-specified limits. */ #include using namespace std; // Function prototype. int rand_int(int a, int b); int main() {...
17
206_C517 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; }
18
206_C518 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; }
20
206_C520 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 reliability Analytical reliability Number of trials Random seed Simulation reliability
21
206_C521 Problem Solving Applied Hand Example Analytical Reliability r = 0.8 Series Configuration r_series = r 3 = 0.512 Parallel Configuration r_parallel = 3r - 3r 2 + r 3 = 0.992 Comp
22
206_C522 Problem Solving Applied Hand Example Simulation 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
23
206_C523 Problem Solving Applied Algorithm Development Read component reliability, number of trials, seed Compute analytical reliabilities While 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 reliabilities Print simulation reliabilities
24
/* This program estimates the reliability */ /* of a series and a parallel configuration */ /* using a computer simulation. */ #include 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;
25
// 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);
26
// 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++; }
27
// 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; } /*----------------------------------------------------*/
28
206_C528 Testing
29
206_C529 Testing
30
206_C530 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.