1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 6.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 3.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Computer Science 1620 Function Scope & Global Variables.
Computer Science 1620 Programming & Problem Solving.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
CS 117 Section 2 + KNET Computer accounts – ed to KNET students –Change password Homework 1 Lab Tutors –In lab for next 2 weeks –Will help you with.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Software Engineering 1 (Chap. 1) Object-Centered Design.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 1.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 9.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Discretization of Fluid Models (Navier Stokes) Dr. Farzad Ismail School of Aerospace and Mechanical Engineering Universiti Sains Malaysia Nibong Tebal.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 2.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 Discretization of Fluid Models (Navier Stokes) Dr. Farzad Ismail School of Aerospace and Mechanical Engineering Universiti Sains Malaysia Nibong Tebal.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 Discretization of Fluid Models (Navier Stokes) Dr. Farzad Ismail School of Aerospace and Mechanical Engineering Universiti Sains Malaysia Nibong Tebal.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 9.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 11.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
#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.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Fundamental Programming Fundamental Programming Introduction to Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Problem Solving and Program Design. Problem Solving Process Define and analyze the problem. Develop a solution. Write down the solution steps in detail.
Review 1.
Engineering Problem Solving With C An Object Based Approach
EMT 101 – Engineering Programming
solve the following problem...
Screen output // Definition and use of variables
Lab 1 Introduction to C++.
Chapter 6: User-Defined Functions I
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Presentation transcript:

1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 6

2 Functions and Subroutines Writing a code with only a main function is efficient or less efficient? Efficiency can be measured more than one way. Efficiency in terms of program management and hence debugging Can also be measured in terms of how much data is being passed from one part of the program to another.

3 Functions versus subroutines It will be messy if you include all your algorithm in the main Use subroutines or functions This add additional cost (and perhaps errors) due to information passing of variables and identifiers Need to declare functions and subroutines in header files

4 Example Solving a fluid flow over an airfoil In one main function In a main function with many subfunctions or subroutines Pseudo-code presentation

5 1 Main function int main() { initializations { initializations ….. ….. geometry and grid modelings geometry and grid modelings …. …. mathematical and physical models mathematical and physical models …… …… loop to solve the problem for pressure and velocities loop to solve the problem for pressure and velocities …….. …….. plot the velocity and pressure fields plot the velocity and pressure fields …….. …….. return 0; return 0; }

6 Problems with 1 Main functions Main program becomes messy Difficult to debug The program is very ‘rigid’ since any changes to part of the program requires going through the whole main program Difficult to extend the code to the purposes

7 How to overcome? Use ‘divide and conquer’ approach in managing the main programs into smaller subprograms Use either functions or subroutines

8 Main function + subroutines Global declarations int main() { subroutines for initializations { subroutines for initializations subroutines for geometry and grid modelings subroutines for geometry and grid modelings subroutines for mathematical and physical models subroutines for mathematical and physical models subroutines to solve the problem for pressure &velocities subroutines to solve the problem for pressure &velocities subroutines for plot the velocity and pressure fields subroutines for plot the velocity and pressure fields return 0; return 0; }

9 Discussions on how the program and subprograms work The algorithm for each subroutine lies outside main Requires the declaration of the subroutines either in the same main file or another file which can be read and compiled together with the main file (similar to using a C++ library) Example subroutine for geometric model: geometric_model (input parameters, grid size, coordinates, boundary conditions….. ) geometric_model (input parameters, grid size, coordinates, boundary conditions….. ) Each subroutine requires information as input to perform

10 The advantages with using subroutines The whole program is more manageable in terms of program developments and debugging The program is now flexible to changes, i.e. for example if another geometry is used, no need to change main function, just change the subroutine function The subroutines can be used with other main functions (compatibility with other code) However, there is a disadvantage of using subroutines…

11 Kinetic Energy Program Using Function #include #include using namespace std; double Compute_KE(double u, double v) { double KE=0.0; KE= u*u + v*v; return KE; } int main() { int n; int n; cout << "Enter number of particles: " << endl; cout << "Enter number of particles: " << endl; cin >>n; cin >>n; double KE[n]; double u[n]; double v[n]; double KE[n]; double u[n]; double v[n]; for (int i=0; i<n; i++) for (int i=0; i<n; i++) { cout << "Enter the u velocity for particles: " << endl; cout << "Enter the u velocity for particles: " << endl; cin >> u[i]; cin >> u[i]; cout << "Enter the v velocity for particles: " << endl; cout << "Enter the v velocity for particles: " << endl; cin >> v[i]; cin >> v[i]; KE[i]= Compute_KE(u[i],v[i]); KE[i]= Compute_KE(u[i],v[i]); cout << "KE of particle " << i+1 << " is " << KE[i] << endl; cout << "KE of particle " << i+1 << " is " << KE[i] << endl; } getch(); getch(); return 0; } // end of program body

12 Kinetic Energy Program Using Subroutine #include #include using namespace std; //Declaration of a subroutine to be used in Main void Determine_KE(int n, double u[], double v[], double KE[], double TKE) { for (int i=0; i<n; i++) for (int i=0; i<n; i++) { KE[i]= u[i]*u[i] + v[i]*v[i]; KE[i]= u[i]*u[i] + v[i]*v[i]; TKE += KE[i]; TKE += KE[i]; } cout << "TKE is " << TKE << endl; cout << "TKE is " << TKE << endl; } int main() { int n; double TKE; double TKE; cout << "Enter number of particles: " << endl; cout << "Enter number of particles: " << endl; cin >>n; cin >>n; double KE[n]; double u[n]; double v[n]; double KE[n]; double u[n]; double v[n]; for (int i=0; i<n; i++) for (int i=0; i<n; i++) { cout << "Enter the u velocity for particles: " << endl; cout << "Enter the u velocity for particles: " << endl; cin >>u[i]; cin >>u[i]; cout << "Enter the v velocity for particles: " << endl; cout << "Enter the v velocity for particles: " << endl; cin >> v[i]; cin >> v[i]; } Determine_KE(n,u,v,KE, TKE); Determine_KE(n,u,v,KE, TKE); return 0; }

13 Tutorial 1 Using subroutines write a program to perform a numerical integration of f(x)=x^2*sin(x)*exp(x^2) over x=[0,Pi] having a choice of f(x)=x^2*sin(x)*exp(x^2) over x=[0,Pi] having a choice of (i) rectangular rule (i) rectangular rule (ii) the Simpson’s rule (take home) (ii) the Simpson’s rule (take home) Divide the domain into N subsections, where N=5,10,20,40. Compare your results. Divide the domain into N subsections, where N=5,10,20,40. Compare your results.

14 Plot of point data? The computer produces point data which are difficult to analyze Need to plot these point data to clearly view the results You can create your own plotting functions but it is easier to just ‘dump’ the data into a professional plotting software like Excel, Tecplot and even Matlab. The key to ‘dump’ out data is using the fstream library

15 #include #include int main() int main() { fstream data_name fstream data_name data_name.open(“C:/Data/test.dat",ios::out); data_name.open(“C:/Data/test.dat",ios::out); data_name << "VARIABLES = X, Y” << endl; data_name << "VARIABLES = X, Y” << endl; data_name << "ZONE T=T" << No <<", I=" << (M) << ", J=" << N << ", F=POINT" << endl; data_name << "ZONE T=T" << No <<", I=" << (M) << ", J=" << N << ", F=POINT" << endl; data_name.precision(12); data_name.precision(12); for (int j=1; j<N+1; j++) for (int j=1; j<N+1; j++) { for (int i=1; i<M+1; i++) { for (int i=1; i<M+1; i++) { data_name.width(20); data_name << X[i][j];} { data_name.width(20); data_name << X[i][j];} } return 0; return 0; }

16 Tutorial 2 Please refer to Homework 3 in website