EMT 101 – Engineering Programming

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

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.
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.
Writing and Testing Programs Drivers and Stubs Supplement to text.
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.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
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.
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.
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.
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.
1 Discretization of Fluid Models (Navier Stokes) Dr. Farzad Ismail School of Aerospace and Mechanical Engineering Universiti Sains Malaysia Nibong Tebal.
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
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.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 6.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
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.
Review 1.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to C++ October 2, 2017.
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
solve the following problem...
User-Defined Functions
User-defined Functions
Dr. Shady Yehia Elmashad
Learning Objectives What else in C++ Bitwise operator
Lab 1 Introduction to C++.
Announcements General rules about homeworks
User-defined Functions
Chapter 5 Function Basics
Announcements General rules about homeworks
Screen output // Definition and use of variables
User-defined Functions
Starting Out with C++: From Control Structures through Objects
Lab 1 Introduction to C++.
Announcements Homework 1 will be assigned this week,
Chapter 6: User-Defined Functions I
Fundamental Programming
Announcements General rules about homeworks
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Programming Fundamental
Presentation transcript:

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

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.

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

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

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

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

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

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

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….. ) Each subroutine requires information as input to perform

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…

Kinetic Energy Program Using Function #include <iostream> #include <string> #include <cmath> #include <conio.h> 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; cout << "Enter number of particles: " << endl; cin >>n; double KE[n]; double u[n]; double v[n]; for (int i=0; i<n; i++) cout << "Enter the u velocity for particles: " << endl; cin >> u[i]; cout << "Enter the v velocity for particles: " << endl; cin >> v[i]; KE[i]= Compute_KE(u[i],v[i]); cout << "KE of particle " << i+1 << " is " << KE[i] << endl; getch(); return 0; } // end of program body

Kinetic Energy Program Using Subroutine #include <iostream> #include <string> #include <cmath> #include <conio.h> 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++) KE[i]= u[i]*u[i] + v[i]*v[i]; TKE += KE[i]; } cout << "TKE is " << TKE << endl; int main() { int n; double TKE; cout << "Enter number of particles: " << endl; cin >>n; double KE[n]; double u[n]; double v[n]; cout << "Enter the u velocity for particles: " << endl; cin >>u[i]; cout << "Enter the v velocity for particles: " << endl; cin >> v[i]; Determine_KE(n,u,v,KE, TKE); return 0;

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 (i) rectangular rule (ii) the Simpson’s rule (take home) Divide the domain into N subsections, where N=5,10,20,40. Compare your results.

Assignment Please refer to Homework 2 in website