SME1013 SME1013 PROGRAMMING PROGRAMMING FOR ENGINEERS FOR - - PowerPoint PPT Presentation

sme1013 sme1013 programming programming for engineers for
SMART_READER_LITE
LIVE PREVIEW

SME1013 SME1013 PROGRAMMING PROGRAMMING FOR ENGINEERS FOR - - PowerPoint PPT Presentation

SME1013 SME1013 PROGRAMMING PROGRAMMING FOR ENGINEERS FOR ENGINEERS Ainullotfi bin Abdul Latif Ainullotfi bin Abdul Latif Faculty of Mechanical Engineering UTM Faculty of Mechanical Engineering UTM Problem Solving Problem Solving


slide-1
SLIDE 1

SME1013 SME1013 PROGRAMMING PROGRAMMING FOR ENGINEERS FOR ENGINEERS

Ainullotfi bin Abdul Latif Ainullotfi bin Abdul Latif

Faculty of Mechanical Engineering UTM Faculty of Mechanical Engineering UTM

slide-2
SLIDE 2

Feb 22, 2012 SME1013 Programming for Engineers 2

Problem Solving Problem Solving

Recognise and understand the problem (what Recognise and understand the problem (what is it that needed to be solved?) is it that needed to be solved?) List the parameters affecting the problem List the parameters affecting the problem (input) (input) Select the appropriate theory Select the appropriate theory Make the necessary assumptions Make the necessary assumptions Solve the problem – may use a computer Solve the problem – may use a computer Verify the results Verify the results

slide-3
SLIDE 3

Feb 22, 2012 SME1013 Programming for Engineers 3

Computer Solution: Computer Solution: Software Development Software Development Process Process

List down the sequence of steps to solve List down the sequence of steps to solve the problem – the problem – algorithm algorithm Write these steps in a computer language Write these steps in a computer language – – program program coding coding Test the program to ensure it error-free Test the program to ensure it error-free and reliable – and reliable – debugging debugging Make the program easy to understand Make the program easy to understand and use – and use – documentation documentation Save the program and improve it as one Save the program and improve it as one gathers experience gathers experience

slide-4
SLIDE 4

Feb 22, 2012 SME1013 Programming for Engineers 4

Computer Coding Computer Coding

A A program program is a sequence of instructions to is a sequence of instructions to the computer for it to solve a particular the computer for it to solve a particular

  • problem. A set of programs is called
  • problem. A set of programs is called code

code. . Programs are written in some Programs are written in some programming programming language language – e.g. Fortran, – e.g. Fortran, Basic, C++, Pascal, Java, Matlab. Basic, C++, Pascal, Java, Matlab. Programs are stored in Programs are stored in files files – which are a – which are a sequence of bytes which is given a name sequence of bytes which is given a name and stored on a disk. and stored on a disk.

slide-5
SLIDE 5

Feb 22, 2012 SME1013 Programming for Engineers 5

Computer Coding Computer Coding

A A bit bit or a binary digit is the smallest unit

  • r a binary digit is the smallest unit
  • f data storage. It has the value of either 0
  • f data storage. It has the value of either 0
  • r 1 only.
  • r 1 only.

A A byte byte is a sequence of 8 bits, is a sequence of 8 bits, representing 256 different values. E.g. of a representing 256 different values. E.g. of a byte is: byte is: 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 A byte could represent a character like a A byte could represent a character like a letter or a number or a symbol. letter or a number or a symbol. A A word word is a larger group of bytes, maybe is a larger group of bytes, maybe two bytes (16 bits). two bytes (16 bits).

slide-6
SLIDE 6

Feb 22, 2012 SME1013 Programming for Engineers 6

Computer Coding Computer Coding

A program is a file containing a A program is a file containing a sequence of “ sequence of “statements statements”, each of ”, each of which tells the computer to do a which tells the computer to do a specific action. specific action. Once a program is Once a program is run run or

  • r executed

executed the the commands are followed and actions commands are followed and actions

  • ccur in a
  • ccur in a sequential

sequential manner. manner. If the program is designed to interact If the program is designed to interact with the outside world, then it must with the outside world, then it must have have input input and and output

  • utput.

.

slide-7
SLIDE 7

Feb 22, 2012 SME1013 Programming for Engineers 7

Computer Coding Computer Coding

A program is said to have a A program is said to have a bug bug if it if it contains a mistake or it does not function contains a mistake or it does not function in the way it is intended to. in the way it is intended to. Bugs can happen both in the Bugs can happen both in the logic logic of the

  • f the

program, as well as in the program, as well as in the commands commands. . In order that the program perform the In order that the program perform the exact actions it is intended to do, before exact actions it is intended to do, before the actual program is written the actual program is written an algorithm an algorithm for solving the problem must first be for solving the problem must first be

  • utlined.
  • utlined.
slide-8
SLIDE 8

Feb 22, 2012 SME1013 Programming for Engineers 8

Computer Coding Computer Coding

An An algorithm algorithm is a general sequence of the is a general sequence of the logical steps in solving a specific problem. logical steps in solving a specific problem. A A flowchart flowchart is a graphical representation of is a graphical representation of the algorithm. the algorithm. A A pseudocode pseudocode gives a more detailed, step- gives a more detailed, step- by-step set of instructions required to solve by-step set of instructions required to solve the problem. It is normally written in the problem. It is normally written in everyday language. everyday language.

slide-9
SLIDE 9

Feb 22, 2012 SME1013 Programming for Engineers 9

Good Algorithms Good Algorithms

Each step must be Each step must be deterministic deterministic – it – it cannot be ambiguous or leaving cannot be ambiguous or leaving things to chance. things to chance. The process must end after a The process must end after a finite finite number of steps – it must not be number of steps – it must not be

  • pen-ended.
  • pen-ended.

The algorithm must be The algorithm must be general general enough to deal with any contingency. enough to deal with any contingency.

slide-10
SLIDE 10

Feb 22, 2012 SME1013 Programming for Engineers 10

Common Flowchart Common Flowchart Symbols Symbols

Start/Stop Process Input/Output Refers to a separate flowchart Decision Connector Off-page Connector Preparation (for loops etc)

slide-11
SLIDE 11

11 11 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Example Problem #1 Example Problem #1

Given temperature in degrees Given temperature in degrees Fahrenheit, the temperature in degrees Fahrenheit, the temperature in degrees Kelvin is to be computed and shown. Kelvin is to be computed and shown. Formula: Formula: T TK

K and T

and TF

F are temperatures in degrees

are temperatures in degrees Kelvin and Fahrenheit respectively Kelvin and Fahrenheit respectively

T K =( T F−32 1.8 )+273.15

slide-12
SLIDE 12

12 12 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Algorithm Algorithm

Start Start Get the temperature in Fahrenheit Get the temperature in Fahrenheit Compute the temperature in Kelvin Compute the temperature in Kelvin using the formula using the formula Show the temperature in Kelvin Show the temperature in Kelvin Stop Stop

T K =( T F−32 1.8 )+273.15

slide-13
SLIDE 13

13 13 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Flowchart Flowchart

Start Get TF Show TK Stop

T K =( T F−32 1.8 )+273.15

slide-14
SLIDE 14

14 14 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Pseudocode Pseudocode

  • 1. Start
  • 1. Start
  • 2. Get T
  • 2. Get TF

F

  • 3. T
  • 3. TK

K = (T

= (TF

F-32)/1.8 + 273.15

  • 32)/1.8 + 273.15
  • 4. Show T
  • 4. Show TK

K

  • 5. Stop
  • 5. Stop
slide-15
SLIDE 15

15 15 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Example Problem #2 Example Problem #2

Given a set of numbers, calculate their Given a set of numbers, calculate their sum and the average value (mean). sum and the average value (mean). Formula: Formula: n is the number of numbers in the set n is the number of numbers in the set

̄ x = 1 n∑

i=1 n

xi

slide-16
SLIDE 16

16 16 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Algorithm Algorithm

  • 1. Start
  • 1. Start
  • 2. Get one number in the set
  • 2. Get one number in the set
  • 3. Count the numbers as it is obtained
  • 3. Count the numbers as it is obtained
  • 4. If there are still numbers to be obtained,
  • 4. If there are still numbers to be obtained,

go back to step 2. go back to step 2.

  • 5. Sum the numbers in the set
  • 5. Sum the numbers in the set
  • 6. Divide the sum by the number of numbers
  • 6. Divide the sum by the number of numbers

in the set to get the average in the set to get the average

  • 7. Show the sum and the average
  • 7. Show the sum and the average
  • 8. Stop
  • 8. Stop
slide-17
SLIDE 17

17 17 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Flowchart Flowchart

Start Get number Calculate mean Show sum and mean Stop Count number Any more number? Calculate sum Yes No

slide-18
SLIDE 18

18 18 SME1013 Programming for Engineers SME1013 Programming for Engineers Feb 22, 2012 Feb 22, 2012

Detailed Flowchart Detailed Flowchart

Start Get xi Mean = sum/n Show sum and mean Stop i ← i + 1 Any more number? Yes No i ← 0 n ← i sum ← 0 j ← 0 j ← j + 1 Is j ≥ n? sum ← sum + xj No Yes