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

Slides:



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

ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
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)
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.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Programming is instructing a computer to perform a task for you with the help of a programming language.
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.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
1 Discretization of Fluid Models (Navier Stokes) Dr. Farzad Ismail School of Aerospace and Mechanical Engineering Universiti Sains Malaysia Nibong Tebal.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
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*
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
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.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
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.
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.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Libraries Making Functions Reuseable. Review Last time, we wrote a program to compute the area and circumference of an ellipse. a a bb.
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.
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,
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
C++ Programming: Presentation 1
Review 1.
Engineering Problem Solving With C An Object Based Approach
EMT 101 – Engineering Programming
Dynamic Memory Allocation Reference Variables
Function Basics.
Learning Objectives What else in C++ Bitwise operator
Screen output // Definition and use of variables
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Lab 1 Introduction to C++.
C++ Compilation Model C++ is a compiled language
From C to C++: Summary of weeks 1 - 4
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
Pointers & Functions.
What Is? function predefined, programmer-defined
Class rational part2.
Presentation transcript:

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

2 Abundance of point data? As seen in Homework 2, 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

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

4 Customized Library We have seen that using functions and subroutines defined outside of main to be efficient But having many functions or subroutines within the same main.cpp file can be difficult in terms of managing the size and understanding the main file. What about using subroutines and functions designed by other programmers in your own main file? This is where the use of customized library can be useful

5 How to create a customized library? Can be done by creating another (or many).cpp files Within each.cpp file, you can include the functions and subroutines Since the *.cpp files are outside of main.cpp file, the functions and subroutines are not directly connected to main.cpp How to connect these additional *.cpp files to main.cpp?

6 Using Header file Similar to calling iostream library, you can connect these additional *.cpp files to main.cpp by using a header file and LINK all *.cpp files This header file will include ALL of the initializations of the functions and subroutines in each of the additional *.cpp files. If you are using a GUI-based compiler such as Dev C++, code block or Microsoft C++, you could LINK the *.cpp files by creating a project You need a link.bat to do the same on MS-DOS platform

7 Example Solving a fluid flow over an airfoil In a main function subfunctions or subroutines outside main program BUT still within the same main.cpp file Pseudo-code presentation

8 Functions within main.cpp subroutine geometry and grid modelings subroutine mathematical and physical models Subroutine plot the velocity and pressure fields int main() { preprocessing steps { preprocessing steps …….. …….. loop to solve the problem for pressure and velocities loop to solve the problem for pressure and velocities …….. …….. return 0; return 0; }

9 Problems with all being in main.cpp Main.cpp becomes unnecessarily long and difficult to be understood Not flexible in terms of merging with codes done by other programmers (a standard practice in engineering) Difficult to extend the code for improvements and added capabilities

10 Main function + header file #include #include #include “header_files.h” int main() { preprocessing steps { preprocessing steps loop to solve the problem for pressure &velocities loop to solve the problem for pressure &velocities return 0; return 0; }

11 Header file initializations #include #include #define Pi #define M 60 #define N 60 initializations of geometry and grid modelings subroutines void Cell_Coord(double XCoord[M+2][N+2], double YCoord[M+2][N+2], variables); double YCoord[M+2][N+2], variables); initializations mathematical and physical models subroutines Initializations of plot the velocity and pressure fields functions

12 Geometry.cpp file #include #include # include “header_files.h” //if you need to access other functions outside geometry.cpp void Cell_Coord(double XCoord[M+2][N+2], double YCoord[M+2][N+2], variables) double YCoord[M+2][N+2], variables){ …… ……} void geometry ( ….) {}

13 Kinetic Energy Program #include #include #include “header_files.h” using namespace std; int main() { int n; double KE[n]; double u[n]; double v[n]; double TKE; double TKE; cout << "Enter number of particles: " << endl; cout << "Enter number of particles: " << endl; cin >>n; cin >>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);getch(); return 0; }

14Header_files.h #include #include #define Pi using namespace std; //initialization of a subroutines to be used in Main or other *.cpp files void Determine_KE(int, double u[], double v[], double KE[], double);

15KE.cpp #include #include #include “header_files.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++) 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; }

16 Exercises Repeat Homework #2, but now please include 1. subroutines to compute the second moment of inertia 1. subroutines to compute the second moment of inertia 2. Subroutines to compute the deflection 3. Subroutines to plot the deflection in separate 3 *.cpp files connected within one project using header files.