1 emt 101 – engineering programming dr. farzad ismail school of aerospace engineering universiti...

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

Upload: jeffrey-holland

Post on 13-Dec-2015

227 views

Category:

Documents


4 download

TRANSCRIPT

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

11

EMT 101 – Engineering EMT 101 – Engineering ProgrammingProgramming

Dr. Farzad IsmailDr. Farzad Ismail

School of Aerospace EngineeringUniversiti Sains Malaysia

Nibong Tebal 14300 Pulau Pinang

Week 1

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

22

What Can You Do with Programming?What Can You Do with Programming?

As a database controlAs a database control

Web based applicationWeb based application

Machine or Electronics controlMachine or Electronics control

Systems control (UAV)Systems control (UAV)

Software developmentSoftware development

Solve Engineering Problems (Simulation)Solve Engineering Problems (Simulation)

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

33

What is Programming?What is Programming?

A computer needs ‘something’ to tell it what to do to A computer needs ‘something’ to tell it what to do to perform certain tasksperform certain tasks

The tasks may require precise details and multiple stepsThe tasks may require precise details and multiple steps

That ‘something’ is called a That ‘something’ is called a computer program.computer program.

The process of developing and executing the computer The process of developing and executing the computer program is called program is called computer programmingcomputer programming..

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

44

Computer User versus Computer ProgrammerComputer User versus Computer Programmer

You can use a computer without doing any programmingYou can use a computer without doing any programming

A Facebook user does not need to know how the A Facebook user does not need to know how the software has been programmed to be able to use software has been programmed to be able to use FacebookFacebook

Analogy: You can ride a motorcyle without being a Analogy: You can ride a motorcyle without being a mechanic, operate a TV without being an electricianmechanic, operate a TV without being an electrician

Most computer users may never have to perform Most computer users may never have to perform programming!programming!

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

55

Computer User versus Computer Programmer Computer User versus Computer Programmer

A programmer develops a software(s) for computer users A programmer develops a software(s) for computer users to useto use

Programmers not only come from computer engineers or Programmers not only come from computer engineers or scientists but also other engineering or scientific disciplinesscientists but also other engineering or scientific disciplines

Programming is a very interesting and rewarding activity: Programming is a very interesting and rewarding activity: write a computer game with sound and motionwrite a computer game with sound and motion

Programmers Programmers fullyfully direct the computer as their slaves to direct the computer as their slaves to extend their brain capabilitiesextend their brain capabilities

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

66

What is a ProgramWhat is a Program

Program = Data + AlgorithmProgram = Data + Algorithm

All in source codeAll in source code

Written in various languagesWritten in various languages

High level languages requires:High level languages requires:

1- Compile1- Compile

2- Link or Build2- Link or Build

3- Execute3- Execute

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

77

Choices of Programming LanguageChoices of Programming Language

Machine languageMachine language

FortranFortran

CC

C++C++

JavaJava

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

88

Machine LanguageMachine Language

The most primitive yet the most efficient way of The most primitive yet the most efficient way of communicating with the computercommunicating with the computer

Task: Task:

1) Move contents of memory location 10000 into location ET1) Move contents of memory location 10000 into location ET

2) Add the value 3000 from memory location ET2) Add the value 3000 from memory location ET

3) If the outcome is negative, proceed with instruction located 3) If the outcome is negative, proceed with instruction located in memory location 5432in memory location 5432

In machine language the instruction reads:In machine language the instruction reads:

161 10000 45 3000 127 5432161 10000 45 3000 127 5432

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

99

Fortran LanguageFortran Language

The first high level language developed in 1957, mainly for solving The first high level language developed in 1957, mainly for solving mathematical problemsmathematical problems

In FOTRAN 77 the task now reads:In FOTRAN 77 the task now reads:

REAL*8 X10000REAL*8 X10000

REAL*8 ETREAL*8 ET

X10000= 10.d0X10000= 10.d0

ET = X10000 + 3000.d0ET = X10000 + 3000.d0

IF (ET.LT. 0.d0)IF (ET.LT. 0.d0)

CALL F(X5432)CALL F(X5432)

ENDIFENDIF

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

1010

C, C++ LanguageC, C++ Language

C developed in 1970 (Dennis Ritchie) followed by C++ in 1980 C developed in 1970 (Dennis Ritchie) followed by C++ in 1980 (Bjaern Stroustrup)(Bjaern Stroustrup)

In C++ the task now reads In C++ the task now reads

X10000 = 10.0;X10000 = 10.0;

float ET;float ET;

ET = X10000 + 3000;ET = X10000 + 3000;

If (ET < 0)If (ET < 0)

{{

F(X5432);F(X5432);

}}

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

1111

Differences between these languagesDifferences between these languages

The machine language is the most ‘direct’ form of communication The machine language is the most ‘direct’ form of communication between human and computer, but it is very difficult to usebetween human and computer, but it is very difficult to use

FORTRAN and C/C++ are easier to be understood and used by FORTRAN and C/C++ are easier to be understood and used by humans compared to machine language but the computer does not humans compared to machine language but the computer does not understand it -> needs a compiler to convert them to object file understand it -> needs a compiler to convert them to object file (machine language form) (machine language form)

Based on current problem (set-up), the difference between C++ and Based on current problem (set-up), the difference between C++ and FORTRAN is just the syntax.FORTRAN is just the syntax.

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

1212

Procedural vs. Object Oriented ProgrammingProcedural vs. Object Oriented Programming

Procedural: need to know the details of each part of Procedural: need to know the details of each part of

program to use the codeprogram to use the code

Object Oriented (OOP): just need to know at higher level to Object Oriented (OOP): just need to know at higher level to

be able to use the codebe able to use the code

OOP is slightly less efficient in terms of running time versusOOP is slightly less efficient in terms of running time versus

procedural but easier to manageprocedural but easier to manage

Most research codes and libraries are written in procedural Most research codes and libraries are written in procedural programmingprogramming

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

1313

What Scientific Programming can Do?What Scientific Programming can Do?

Movie 23 - P/H: Below - P/H: Below

Movie 5, - Turning Circle, - Turning Circle

Movie 57 - Propeller - Propeller

Movie 68 - Rough Sea - Rough Sea

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

1414

First, need to understandFirst, need to understand

The Generic approach of Programming to solve scientific The Generic approach of Programming to solve scientific or engineering problems -> Flow chartor engineering problems -> Flow chart

Concept of variables, identifiers, data type, C++ libraryConcept of variables, identifiers, data type, C++ library

Initialization, arrays and memory allocationInitialization, arrays and memory allocation

Control structure and loopsControl structure and loops

Function, subroutines, structures and classesFunction, subroutines, structures and classes

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

1515

A Sample C++ CodeA Sample C++ Code

#include <iostream> //to include C++ library for input and output of values#include <iostream> //to include C++ library for input and output of values

using namespace std; //to be able to use all the standard cusing namespace std; //to be able to use all the standard classes, objects and functions in C++

int main () // start of program body, main is a function with no parameters that int main () // start of program body, main is a function with no parameters that returns an int. valuereturns an int. value

{{

int numberOfLanguages; // define an identifier numberOfLanguages as an integer data typeint numberOfLanguages; // define an identifier numberOfLanguages as an integer data type

cout << “Hello student.\n "; // program greets the student and go to next linecout << “Hello student.\n "; // program greets the student and go to next line

<< “Welcome to EMT 101.\n”; << “Welcome to EMT 101.\n”;

cout << “How many programming languages have you used? "; // program asking user to input no of cout << “How many programming languages have you used? "; // program asking user to input no of languageslanguages

cin >> numberOfLanguages; // input from user is now saved into cin >> numberOfLanguages; // input from user is now saved into numOfLanguagesnumOfLanguages

return 0; // to return 0 as the integer value of main, most C++ compilers need this return 0; // to return 0 as the integer value of main, most C++ compilers need this

} // end of program body } // end of program body

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

1616

ExercisesExercises

Write a C++ program to enter your name and your Write a C++ program to enter your name and your student metric number.student metric number.