Вы находитесь на странице: 1из 110

Programming for Engineers EE057IU

Lecture 1

Instructor: Dr. Nguyen Ngoc Truong Minh


School of Electrical Engineering
Vietnamese German University (VGU)

Binh Duong City, Fall 2016


Programming for Engineers EE057IU

Lecture Content
Introduction to Computers
A Few Uses of Computers
Computer Organization
Data Hierarchy
Programming Languages
Operating Systems
Introduction to C Programming Language
Simple C Program
Memory Concepts
Algorithms
Pseudo-code
Flowchart
2
Programming for Engineers EE057IU

Introduction

Welcome to C world !!!


structured programming in C

object-oriented programming in C++

3
Programming for Engineers EE057IU

A Few Uses of Computers


Electronic health records
Human genome project
World community grid
Medical imaging
Could computing
GPS
Robots
Email, Instant messaging, Video chat
Internet TV
Game programming
4
Programming for Engineers EE057IU

Hardware and Software

More than a billion general-purpose computers, and billions


more embedded computers are used in cell phones, smartphones,
tablet computers, home appliances, automobiles and more.
In 2011, Fujitsu announced K supercomputer which can
perform over 10 quadrillion calculations per second (10
petaflops)!!!
perform in one second more than 1.000.000 calculations for every person
on the planet

5
Programming for Engineers EE057IU

Computer Organization

Input Unit

Output Unit

Memory Unit

Arithmetic and Logic Unit (ALU)

Central Processing Unit (CPU)

Secondary Storage Unit

6
Programming for Engineers EE057IU

Data Hierarchy

Bit

Characters

Fields

Records

Files

Database

7
Programming for Engineers EE057IU

Programming Languages
Various programming languages

Some understandable directly by computers


Others require translation steps 8
Programming for Engineers EE057IU

Programming Languages
Machine language
Natural language of a particular computer
Consists of strings of numbers (1s, 0s)
Instruct computer to perform elementary operations one at a
time
Machine dependant

9
Programming for Engineers EE057IU

Programming Languages
Assembly Language
English like abbreviations
Translators programs called Assemblers to convert assembly
language programs to machine language
E.g. add overtime to base pay and store result in gross pay
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY

10
Programming for Engineers EE057IU

Programming Languages
High-level languages
To speed up programming even further
Single statements for accomplishing substantial tasks
Translator programs called Compilers to convert high-level
programs into machine language
E.g. add overtime to base pay and store result in gross pay
grossPay = basePay + overtimePay

11
Programming for Engineers EE057IU

History of C

In 1972, Dennis Ritchie at Bell Labs (Canada) writes C and in


1978 the publication of The C Programming Language by Kernighan
& Ritchie caused a revolution in the computing world.

In 1983, the American National Standards Institute (ANSI)


established a committee to provide a modern, comprehensive
definition of C. The resulting definition, the ANSI standard, or
ANSI C, was completed late 1988.

12
Programming for Engineers EE057IU

History of C
Evolved from two previous languages
BCPL , B

BCPL (Basic Combined Programming Language) used for


writing OS & compilers, developed in 1967
B used for creating early versions of UNIX OS, developed in
1970
Both were typeless* languages
C language evolved from B (Dennis Ritchie Bell labs)

* Typeless no data types. Every data item occupied 1 word in memory. 13


Programming for Engineers EE057IU

History of C

Hardware independent
Programs portable to most computers
Dialects of C
Common C / Traditional C
ANSI C / New C Standard
ANSI/ ISO 9899: 1990

Called American National Standards Institute ANSI C

Case-sensitive

14
Programming for Engineers EE057IU

Why use C?
Mainly because it produces code that runs nearly as fast as code written in
assembly language.
Some examples of the use of C might be:
Operating Systems (Windows, Mac OS, UNIX)
Language Compilers / Assemblers
Embedded Systems
Text Editors / Language Interpreters
Print Spoolers
Real-Time Systems
Network Drivers
Modern Programs
Databases
Communication Systems
Utilities

Also because of the portability that writing standard C programs can offer 15
Programming for Engineers EE057IU

Why C Still Useful?


C provides:
Efficiency, high performance and high quality services/web services
Flexibility and power
Many high-level and low-level operations middle level
Stability and small size code
Provide functionality through rich set of function libraries
Gateway for other professional languages like C C++ Java
C is used:
System software: Compilers, Editors or Embedded systems
Data compression, graphics and computational geometry, utility programs
Databases, operating systems, device drivers, system level routines
There are zillions of lines of C legacy code
Also used in application programs

16
Programming for Engineers EE057IU

C++ and Other C-Based Languages


Objective-C
object-oriented language based on C, key programming language for the
MAC OS and all iOS-based services (iPods, iPhones and iPads).
Visual C#
Microsofts three primary object-oriented programming languages:
Visual Basic, Visual C++ and C#).
Java
Developed from a funded internal corporate research project at Sun
Microsystems in 1991, a C++-based object-oriented.
PHP
object-oriented, open-source scripting language based on C
JavaScript
developed by Netscape, most widely used scripting language
17
Programming for Engineers EE057IU

Software Development Method


Requirement Specification
Problem Definition
Analysis
Refine, Generalize, Decompose the problem definition
Design
Develop Algorithm
Implementation
Write Code
Verification and Testing
Test and Debug the code

18
Programming for Engineers EE057IU

Development with C

Four stages
Editing: Writing the source code by using some IDE or editor

Preprocessing or libraries: Already available routines

Compiling: translates or converts source to object code for a specific platform


source code object code

Linking: resolves external references and produces the executable module

Portable programs will run on any machine but


Note! Program correctness and robustness are most
important than program efficiency

19
Programming for Engineers EE057IU

C Standard Library
Two parts to learning the C world
Learn C itself

Take advantage of rich collection of existing functions called C


Standard Library

Avoid reinventing the wheel

20
Programming for Engineers EE057IU

Test-Driving C on Operating Systems

Many development environments are available:


GNU C
Dev C++
Microsoft Visual C++
CodeLite
NetBeans
Eclipse
Xcode

21
Programming for Engineers EE057IU

Basics of C Environment
C systems consist of 3 parts
Environment
Language
C Standard Library

Development environment has 6 phases


Edit
Pre-processor
Compile
Link
Load
Execute

22
Programming for Engineers EE057IU

Basics of C Environment
Program edited in
Phase 1 Editor Disk Editor and stored
on disk

Preprocessor
Phase 2 Preprocessor Disk program processes
the code

Creates object code


Phase 3 Compiler Disk and stores on disk

Links object code


Phase 4 Linker Disk with libraries and
stores on disk
23
Programming for Engineers EE057IU

Basics of C Environment
Primary memory

Puts program in
Phase 5 Loader memory

Primary memory

Takes each instruction


Phase 6 CPU and executes it storing
new data values

24
Programming for Engineers EE057IU

Simple C Program

/* A first C Program*/
#include <stdio.h>
void main()
{
printf(Hello World \n);
}

25
Programming for Engineers EE057IU

Simple C Program

Line 1: #include <stdio.h>


As part of compilation, the C compiler runs a program called
the C preprocessor. The preprocessor is able to add and remove
code from your source file.
In this case, the directive #include tells the preprocessor to
include code from the file stdio.h
This file contains declarations for functions that the program
needs to use. A declaration for the printf function is in this file.

26
Programming for Engineers EE057IU

Simple C Program
Line 2: void main()
This statement declares the main function.
A C program can contain many functions but must always
have one main function.
A function is a self-contained module of code that can
accomplish some task.
Functions are examined later.
The void specifies the return type of main. In this case,
nothing is returned to the operating system.

27
Programming for Engineers EE057IU

Simple C Program

Line 3: {
This opening bracket denotes the start of the program.

28
Programming for Engineers EE057IU

Simple C Program

Line 4: printf("Hello World\n");


printf is a function from a standard C library that is used to print
strings to the standard output, normally your screen.
The compiler links code from these standard libraries to the code
you have written to produce the final executable.
The \n is a special format modifier that tells the printf to put a
line feed at the end of the line.
If there were another printf in this program, its string would print
on the next line.

29
Programming for Engineers EE057IU

Simple C Program

Line 5: }
This closing bracket denotes the end of the program.

30
Programming for Engineers EE057IU

Escape Sequence

\n new line
\t horizontal tab ( 6 spaces)
\v vertical tab ( 3 lines)
\r carriage return
\a alert
\\ backslash
\ double quote

31
Programming for Engineers EE057IU

Memory concepts

Every variable has a name, type and value

Variable names correspond to locations in computer


memory

New value overwrites the previous value Destructive


read-in

Value reading called Non-destructive read-out

32
Programming for Engineers EE057IU

Arithmetic in C

C operation Algebraic C
Addition(+) f+7 f+7
Subtraction (-) p-c p-c
Multiplication(*) bm b*m
Division(/) x/y x/y
Modulus(%) r mod s r%s

33
Programming for Engineers EE057IU

Precedence Order

Highest to lowest
()
*, /, %
+, -

34
Programming for Engineers EE057IU

Examples
Algebra:
z = pr%q+w/x-y

C:
z = p * r % q + w / x y;

Precedence:
1 2 4 3 5

35
Programming for Engineers EE057IU

Examples
Algebra:
a(b+c)+ c(d+e)

C:
a * ( b + c ) + c * ( d + e );

Precedence:
3 1 5 4 2

36
Programming for Engineers EE057IU

Decision Making

Checking falsity or truth of a statement

Equality operators have lower precedence than


relational operators

Relational operators have same precedence

Both associate from left to right

37
Programming for Engineers EE057IU

Decision Making

Equality operators
== equality
!= difference

Relational operators
< less than
> greater than
<= less than or equal to
>= greater than or equal to

38
Programming for Engineers EE057IU

Summary of Precedence Order

Operator Associability
() left to right
/ % left to right
+ - left to right
< <= > >= left to right
== != left to right
= left to right

39
Programming for Engineers EE057IU

Assignment Operators
= assign
+= assign with add
-= assign with subtract
*= assign with multiply
/= assign with divide
%= assign with remainder
>>= assign with right-shift
<<= assign with left-shift
&= assign with bitwise AND
^= assign with bitwise XOR
|= assign with bitwise OR

40
Programming for Engineers EE057IU

Increment/Decrement Operators

++ ++a
++ a++
-- --a
-- a--

41
Programming for Engineers EE057IU

Exercises
1. C/C++ languages are used in _____.
a. Databases, Operating Systems, Device Drivers
b. Data Compression, Utilities Programs
c. Language Compilers/Interpreters
d. All of them
2. C systems consist of three parts. What are they?
a. Environment, Databases, Processor
b. Environment, Databases, C Standard Library
c. Environment, Language, C Standard Library
d. Databases, Language, C Standard Library
3. The development environment of C has _____ phases.
a. 4
b. 5
c. 6
d. 7

42
Programming for Engineers EE057IU

4. A declaration for the printf/scanf functions is in the file _____.


a. stdio.h
b. conio.h
c. stdlib.h
d. math.h
5. The body of a C function is surrounded by _____.
a. parentheses
b. angle brackets
c. curly brackets
d. square brackets
6. A compiler produces an error in compiling a program because a closing
round bracket has been missed out in the source code. What type of error is
this?
a. Syntax Error
b. Logical Error
c. Linker Error
d. There is no error

43
Programming for Engineers EE057IU

44
Programming for Engineers EE057IU

An informal definition of an algorithm is:

Algorithm: a step-by-step method for solving


a problem or doing a task.

Informal definition of an algorithm used in a computer

45
Programming for Engineers EE057IU

Algorithm

A step-by-step problem-solving procedure


An algorithm is a sequence of unambiguous instructions for
solving a problem
The number of steps of an algorithm will be countable and
finite
It is a sequence of instructions (or set of instructions) to make a
program more readable; a process used to answer a question.

46
Programming for Engineers EE057IU

Understand the problem

1. Define the problem

2. Analyze the problem

3. Develop an algorithm/method of solution

4. Write a computer program corresponding to the algorithm

5. Test and debug the program

6. Document the program (how it works and how to use it)

47
Programming for Engineers EE057IU

There are two commonly used tools to help to document


program logic (the algorithm). These are:

Flowcharts

Pseudo code

48
Programming for Engineers EE057IU

Flowchart

A flowchart is a type of diagram that represents an algorithm


or process, showing the steps as boxes of various kinds and
their order by connecting these with arrows. This
diagrammatic representation can give a step-by-step solution
to a given problem.

49
Programming for Engineers EE057IU

Process operations are represented in these boxes, and


arrows connecting them represent flow of control. Flowcharts
are used in analyzing, designing, documenting or managing a
process or program in various fields.

50
Programming for Engineers EE057IU

Symbols
Start and end symbols, represented as lozenges, ovals or rounded rectangles,
usually containing the word Start or End, or another phrase signaling the start
or end of a process, such as submit enquiry or receive product.

Arrows, showing what's called flow of control in computer science. An


arrow coming from one symbol and ending at another symbol signifies flow
passes to the symbol the arrow points to.

Processing steps, represented as rectangles. Examples: Add 1 to X; replace


identified part; save changes or similar.

Input/Output, represented as a parallelogram. Examples: Get X from the user;


display X.

Conditional (or decision), represented as a diamond (rhombus). These


typically contain a Yes/No question or True/False test .
51
Programming for Engineers EE057IU

Start or end of the program

Computational steps or processing function of a


program

Input or output operation

Decision making and branching

Connector or joining of two parts of program

52
Programming for Engineers EE057IU

1 - Simple sequential Flowchart

Example 1:

Read X, Y, Z
Compute Sum (S) as X + Y +Z
Compute Average (A) as S/3
Compute Product (P) as
XxYxZ
Write (Display) the Sum, Average
and Product

53
Programming for Engineers EE057IU

Example 2:
Find the sum of two numbers.
Variables:
A: First Number
B: Second Number
C: Sum (A+B)
Algorithm:
Step 1 Start
Step 2 Input A
Step 3 Input B
Step 4 Calculate C = A + B
Step 5 Output C
Step 6 Stop

54
Programming for Engineers EE057IU

Example 3:
Find the difference and the division of two numbers and
display the result.

55
Programming for Engineers EE057IU

Example 4:

Find the circle area and circumcenter of a circle where R (radius) is given
(exercise)
Variables:
R: Radius
A: Area
C: Circumcenter

Algorithm:
Step 1 Start
Step 2 Input R
Step 3 Calculate Pi
Step 4 Calculate A = Pi(R)2
Step 5 Calculate C = 2Pi(R)
Step 6 Print R, A, C
Step 7 Stop
56
Programming for Engineers EE057IU

START

What is a Flowchart? Display message


How many hours
did you work?

Read Hours

Display message
How much do you

A flowchart is a diagram that get paid per hour?

depicts the flow of a program. Read Pay Rate

The figure shown here is a


Multiply Hours
flowchart for the pay-calculating by Pay Rate.
Store result in

program. Gross Pay.

Display Gross
Pay

END
57
Programming for Engineers EE057IU

Rounded
START Rectangle
Basic Flowchart Symbols Display message
How many hours
did you work?

Read Hours

Notice there are three types of


Display message
symbols in this flowchart: How much do you Parallelogram
get paid per hour?
rounded rectangles
parallelograms Read Pay Rate

a rectangle
Multiply Hours
Each symbol represents a Rectangle by Pay Rate.
Store result in
different type of operation. Gross Pay.

Rounded Display Gross


Pay
Rectangle

END
58
Programming for Engineers EE057IU

Terminal
START

Basic Flowchart Symbols Display message


How many hours
did you work?

Read Hours

Terminals
Display message
represented by rounded rectangles How much do
you get paid per
indicate a starting or ending point hour?

Read Pay Rate

START Multiply Hours


by Pay Rate.
Store result in
Gross Pay.

END
Display Gross
Terminal Pay

END
59
Programming for Engineers EE057IU

START

Basic Flowchart Symbols Display message


How many hours
did you work?

Read Hours

Input/Output Operations
Display message
represented by parallelograms How much do
you get paid per Input/Output
indicate an input or output operation hour? Operation

Read Pay Rate

Display message Multiply Hours


by Pay Rate.
How many hours Read Hours Store result in
Gross Pay.
did you work?
Display Gross
Pay

END
60
Programming for Engineers EE057IU

START

Basic Flowchart Symbols Display message


How many hours
did you work?

Processes Read Hours

represented by rectangles Display message


How much do
you get paid per
indicates a process such as a hour?

mathematical computation or variable


assignment Read Pay Rate

Multiply Hours
Process by Pay Rate.
Multiply Hours Store result in
Gross Pay.
by Pay Rate.
Store result in
Display Gross
Gross Pay. Pay

END
61
Programming for Engineers EE057IU

START
Stepping Through the Output
Operation
Display message

Flowchart How many hours


did you work?

Read Hours
How many
hours did
you work? Display message
How much do you
get paid per hour?

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Hours: ? Display Gross
Pay
Pay Rate: ?
Gross Pay: ? END
62
Programming for Engineers EE057IU

START
Stepping Through the
Display message

Flowchart How many hours


did you work?

Input Operation
Read Hours
(User types 40)
How many
hours did
you work? Display message
40 How much do you
get paid per hour?

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Hours: 40 Display Gross
Pay
Pay Rate: ?
Gross Pay: ? END
63
Programming for Engineers EE057IU

START
Stepping Through the Display message
How many
Flowchart hours did you
work?

Read Hours
How much
do you get
paid per
hour? Display message
Output How much do you
Operation get paid per hour?

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Hours: 40 Display Gross
Pay
Pay Rate: ?
Gross Pay: ? END
64
Programming for Engineers EE057IU

START
Stepping Through the
Display message

Flowchart How many hours


did you work?

Read Hours
How much
do you get
paid per Display message
hour? 20 How much do
you get paid per
hour?

Input Operation Read Pay Rate

(User types 20)


Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Hours: 40 Display Gross
Pay
Pay Rate: 20
Gross Pay: ? END
65
Programming for Engineers EE057IU

START
Stepping Through the
Display message
Flowchart How many hours
did you work?

How much Read Hours


do you get
paid per
hour?
Display message
How much do you
get paid per hour?

Read Pay Rate

Multiply Hours
Process: The product
by Pay Rate.
of 40 times 20 is Store result in
stored in Gross Pay Gross Pay.
Variable Contents:
Hours: 40
Display Gross
Pay Rate: 20 Pay
Gross Pay: 800
END
66
Programming for Engineers EE057IU

START
Stepping Through the
Display message

Flowchart How many hours


did you work?

Read Hours
Your gross
pay is 800
Display message
How much do you
get paid per hour?

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Variable Contents:
Output
Hours: 40 Operation Display Gross
Pay
Pay Rate: 20
Gross Pay: 800 END
67
Programming for Engineers EE057IU

Four Flowchart Structures

Sequence
Decision
Repetition
Case

68
Programming for Engineers EE057IU

Sequence Structure

A series of actions are performed in sequence

The pay-calculating example was a sequence flowchart.

69
Programming for Engineers EE057IU

Decision Structure

One of two possible actions is taken, depending on a


condition.

70
Programming for Engineers EE057IU

Decision Structure

A new symbol, the diamond, indicates a yes/no question. If


the answer to the question is yes, the flow follows one path.
If the answer is no, the flow follows another path

NO YES

71
Programming for Engineers EE057IU

Decision Structure

In the flowchart segment below, the question is x < y? is asked.


If the answer is no, then process A is performed. If the answer is
yes, then process B is performed.

NO YES
x < y?

Process A Process B

72
Programming for Engineers EE057IU

Decision Structure

The flowchart segment below shows how a decision structure


is expressed in C as an if/else statement.

Flowchart
C Code
NO YES
if (x < y)
x < y?
a = x * 2;
else
Calculate a Calculate a as
as x plus y. x times 2. a = x + y;

73
Programming for Engineers EE057IU

Decision Structure

The flowchart segment below shows a decision structure


with only one action to perform. It is expressed as an if
statement in C code.

Flowchart
C Code
NO YES
x < y? if (x < y)
a = x * 2;
Calculate a as
x times 2.

74
Programming for Engineers EE057IU

Repetition Structure

A repetition structure represents part of the program that


repeats. This type of structure is commonly known as a
loop.

75
Programming for Engineers EE057IU

Repetition Structure

Notice the use of the diamond symbol. A loop tests a condition,


and if the condition exists, it performs an action. Then it tests the
condition again. If the condition still exists, the action is
repeated. This continues until the condition no longer exists.

76
Programming for Engineers EE057IU

Repetition Structure

In the flowchart segment, the question is x < y? is asked.


If the answer is yes, then Process A is performed. The
question is x < y? is asked again. Process A is repeated as
long as x is less than y. When x is no longer less than y, the
repetition stops and the structure is exited.

YES
x < y? Process A

77
Programming for Engineers EE057IU

Repetition Structure

The flowchart segment below shows a repetition structure


expressed in C as a while loop.

Flowchart
C Code

YES while (x < y)


x < y? Add 1 to x
x++;

78
Programming for Engineers EE057IU

Controlling a Repetition Structure

The action performed by a repetition structure must


eventually cause the loop to terminate. Otherwise, an
infinite loop is created.
In this flowchart segment, x is never changed. Once the
loop starts, it will never end.
QUESTION: How can this
flowchart be modified so
it is no longer an infinite YES
x < y? Display x
loop?

79
Programming for Engineers EE057IU

Controlling a Repetition Structure

ANSWER: By adding an action within the repetition that


changes the value of x.

YES
x < y? Display x Add 1 to x

80
Programming for Engineers EE057IU

A Pre-Test Repetition Structure

This type of structure is known as a pre-test repetition structure.


The condition is tested BEFORE any actions are performed.

YES
x < y? Display x Add 1 to x

81
Programming for Engineers EE057IU

A Pre-Test Repetition Structure

In a pre-test repetition structure, if the condition does not


exist, the loop will never begin.

YES
x < y? Display x Add 1 to x

82
Programming for Engineers EE057IU

A Post-Test Repetition Structure

This flowchart segment shows a post-test Display x


repetition structure.
Add 1 to x
The condition is tested AFTER the actions
are performed.
YES
A post-test repetition structure always x < y?
performs its actions at least once.

83
Programming for Engineers EE057IU

A Post-Test Repetition Structure

The flowchart segment below shows a post-test repetition


structure expressed in C as a do-while loop.

Display x

C Code
Add 1 to x do
Flowchart {
printf(%d,x);
x++;
YES
} while (x < y);
x < y?

84
Programming for Engineers EE057IU

Case Structure

One of several possible actions is taken, depending


on the contents of a variable.

85
Programming for Engineers EE057IU

Case Structure

The structure below indicates actions to perform depending


on the value in years_employed.

CASE
years_employed

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800

86
Programming for Engineers EE057IU

Case Structure

If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400

If years_employed = 1, If years_employed is any


CASE other value, bonus is set to
bonus is set to 100
years_employed 800

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800

87
Programming for Engineers EE057IU

Connectors

Sometimes a flowchart will not fit on one page.

A connector (represented by a small circle) allows you


to connect two flowchart segments.

88
Programming for Engineers EE057IU

Connectors

The A connector indicates that


the second flowchart segment
begins where the first segment
ends. A
START

END
A

89
Programming for Engineers EE057IU

Modules

A program module (such as a function in C/C++)


is represented by a special symbol.

90
Programming for Engineers EE057IU

Modules

START

The position of the module Read Input

symbol indicates the point the


module is executed. Call calc_pay
function

A separate flowchart can be


constructed for the module. Display results

END

91
Programming for Engineers EE057IU

Combining Structures

Structures are commonly combined to create more complex


algorithms.
The flowchart segment below combines a decision structure
with a sequence structure.

YES
x < y? Display x Add 1 to x

92
Programming for Engineers EE057IU

Combining Structures

This flowchart segment


shows two decision
structures combined.
NO YES
x > min?

Display x is outside NO YES


the limits.
x < max?

Display x is outside Display x is


the limits. within limits.

93
Programming for Engineers EE057IU

Review

What do each of the following symbols represent?

94
(Answer on next slide)
Programming for Engineers EE057IU

Answer

What do each of the following symbols represent?

Decision
Terminal

Input/Output
Operation Connector

Process Module

95
Programming for Engineers EE057IU

Review

Name the four flowchart structures.

96
(Answer on next slide)
Programming for Engineers EE057IU

Answer

Sequence
Decision
Repetition
Case

97
Programming for Engineers EE057IU

Review

What type of structure is this?

98
(Answer on next slide)
Programming for Engineers EE057IU

Answer

Repetition

99
Programming for Engineers EE057IU

Review

What type of structure is this?

100
(Answer on next slide)
Programming for Engineers EE057IU

Answer

Sequence

101
Programming for Engineers EE057IU

Review

What type of structure is this?

102
(Answer on next slide)
Programming for Engineers EE057IU

Answer

Case

103
Programming for Engineers EE057IU

Review

What type of structure is this?

104
(Answer on next slide)
Programming for Engineers EE057IU

Answer

Decision

105
Programming for Engineers EE057IU

Example 5:
Perry Brown needs a program that will allow him to enter the length of four sides of a
polygon. The program should display the perimeter of the polygon. Complete the
flowchart for this problem.

Example 6: 2nd order polynomials


Work out the algorithm that output the solutions of a 2nd order polynomial ax2+bx+c=0,
given the parameters a, b and c. Only real solutions will be treated. The cases with 1 or 2
solutions will be separated.

Example 7:
Define an algorithm that returns the number of years until a father will have an age double
of its sons age.

106
Programming for Engineers EE057IU

2- Branched Flowcharts

Example 1:
Draw a flowchart that shows the traffic light processing (exercise)
Algorithm:
Step 1 Start
Step 2 make a Decision (what is the color)
Step 3 if the color is Red then STOP
Step 4 if the color is Yellow then WAIT
Step 5 if the color is Green then PASS
Step 6 Stop

107
Programming for Engineers EE057IU

3- Loop Flowcharts

Example 1:

What are the outputs of this code ?

108
Programming for Engineers EE057IU

Example 2:
Average for 10 numbers
What are the outputs of this code ?

109
Programming for Engineers EE057IU

110

Вам также может понравиться