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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Modular Programming With Functions
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Chapter 5 Functions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Chapter 6: Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modular Programming with Functions.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
CSE202: Lecture 13The Ohio State University1 Function Scope.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 6 Modularity Using Functions
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Dr. Shady Yehia Elmashad
Functions and an Introduction to Recursion
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Modular Programming with Functions
Dr. Shady Yehia Elmashad
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 6 - Functions Outline 5.1 Introduction
Value returning Functions
User-defined Functions
CS150 Introduction to Computer Science 1
Fundamental Programming
Predefined Functions Revisited
CS 144 Advanced C++ Programming January 31 Class Meeting
Presentation transcript:

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

206_C52 Outline  Modularity  Programmer Defined Functions  Parameter Passing  Storage Class and Scope

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)

206_C54 Programmer-Defined Functions  Function Definition Function Header Parameter Declarations Function Body Return Statement return_type function_name(parameter declarations) { declarations; statements; }

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; }

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);

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; }

206_C58 Program Using a Function /* This function evaluates the sinc function. */ double sinc(double x) { if (abs(x) < ) return 1.0; else return sin(x)/x; }

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

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; }

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; }

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

206_C513 Global/Local Example int count(0);... int main() { int x, y, z;... } int calc(int a, int b) { int x; count += x;... }

206_C514 Summary  Modularity  Programmer Defined Functions  Parameter Passing  Storage Class and Scope

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 (%)

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() {...

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; }

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; }

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

206_C521 Problem Solving Applied Hand Example Analytical Reliability  r = 0.8 Series Configuration  r_series = r 3 = Parallel Configuration  r_parallel = 3r - 3r 2 + r 3 = Comp

206_C522 Problem Solving Applied Hand Example Simulation Reliability (3 random trials)    Series Configuration  All must be less than or equal to 0.8  One success = Parallel Configuration  Any must be less than or equal to 0.8  Three successes = 1.0

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

/* 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;

// 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);

// 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++; }

// 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; } /* */

206_C528 Testing

206_C529 Testing

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