Академический Документы
Профессиональный Документы
Культура Документы
Lecture 1
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
3
Programming for Engineers EE057IU
5
Programming for Engineers EE057IU
Computer Organization
Input Unit
Output Unit
Memory Unit
6
Programming for Engineers EE057IU
Data Hierarchy
Bit
Characters
Fields
Records
Files
Database
7
Programming for Engineers EE057IU
Programming Languages
Various programming languages
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
12
Programming for Engineers EE057IU
History of C
Evolved from two previous languages
BCPL , B
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
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
16
Programming for Engineers EE057IU
18
Programming for Engineers EE057IU
Development with C
Four stages
Editing: Writing the source code by using some IDE or editor
19
Programming for Engineers EE057IU
C Standard Library
Two parts to learning the C world
Learn C itself
20
Programming for Engineers EE057IU
21
Programming for Engineers EE057IU
Basics of C Environment
C systems consist of 3 parts
Environment
Language
C Standard Library
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
Basics of C Environment
Primary memory
Puts program in
Phase 5 Loader memory
Primary memory
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
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
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
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
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
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
43
Programming for Engineers EE057IU
44
Programming for Engineers EE057IU
45
Programming for Engineers EE057IU
Algorithm
46
Programming for Engineers EE057IU
47
Programming for Engineers EE057IU
Flowcharts
Pseudo code
48
Programming for Engineers EE057IU
Flowchart
49
Programming for Engineers EE057IU
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.
52
Programming for Engineers EE057IU
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
Read Hours
Display message
How much do you
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
a rectangle
Multiply Hours
Each symbol represents a Rectangle by Pay Rate.
Store result in
different type of operation. Gross Pay.
END
58
Programming for Engineers EE057IU
Terminal
START
Read Hours
Terminals
Display message
represented by rounded rectangles How much do
you get paid per
indicate a starting or ending point hour?
END
Display Gross
Terminal Pay
END
59
Programming for Engineers EE057IU
START
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
END
60
Programming for Engineers EE057IU
START
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
Read Hours
How many
hours did
you work? Display message
How much do you
get paid per hour?
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
Input Operation
Read Hours
(User types 40)
How many
hours did
you work? Display message
40 How much do you
get paid per hour?
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?
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
Read Hours
How much
do you get
paid per Display message
hour? 20 How much do
you get paid per
hour?
START
Stepping Through the
Display message
Flowchart How many hours
did you work?
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
Read Hours
Your gross
pay is 800
Display message
How much do you
get paid per hour?
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
Sequence
Decision
Repetition
Case
68
Programming for Engineers EE057IU
Sequence Structure
69
Programming for Engineers EE057IU
Decision Structure
70
Programming for Engineers EE057IU
Decision Structure
NO YES
71
Programming for Engineers EE057IU
Decision Structure
NO YES
x < y?
Process A Process B
72
Programming for Engineers EE057IU
Decision Structure
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
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
75
Programming for Engineers EE057IU
Repetition Structure
76
Programming for Engineers EE057IU
Repetition Structure
YES
x < y? Process A
77
Programming for Engineers EE057IU
Repetition Structure
Flowchart
C Code
78
Programming for Engineers EE057IU
79
Programming for Engineers EE057IU
YES
x < y? Display x Add 1 to x
80
Programming for Engineers EE057IU
YES
x < y? Display x Add 1 to x
81
Programming for Engineers EE057IU
YES
x < y? Display x Add 1 to x
82
Programming for Engineers EE057IU
83
Programming for Engineers EE057IU
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
85
Programming for Engineers EE057IU
Case Structure
CASE
years_employed
1 2 3 Other
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
1 2 3 Other
87
Programming for Engineers EE057IU
Connectors
88
Programming for Engineers EE057IU
Connectors
END
A
89
Programming for Engineers EE057IU
Modules
90
Programming for Engineers EE057IU
Modules
START
END
91
Programming for Engineers EE057IU
Combining Structures
YES
x < y? Display x Add 1 to x
92
Programming for Engineers EE057IU
Combining Structures
93
Programming for Engineers EE057IU
Review
94
(Answer on next slide)
Programming for Engineers EE057IU
Answer
Decision
Terminal
Input/Output
Operation Connector
Process Module
95
Programming for Engineers EE057IU
Review
96
(Answer on next slide)
Programming for Engineers EE057IU
Answer
Sequence
Decision
Repetition
Case
97
Programming for Engineers EE057IU
Review
98
(Answer on next slide)
Programming for Engineers EE057IU
Answer
Repetition
99
Programming for Engineers EE057IU
Review
100
(Answer on next slide)
Programming for Engineers EE057IU
Answer
Sequence
101
Programming for Engineers EE057IU
Review
102
(Answer on next slide)
Programming for Engineers EE057IU
Answer
Case
103
Programming for Engineers EE057IU
Review
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 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:
108
Programming for Engineers EE057IU
Example 2:
Average for 10 numbers
What are the outputs of this code ?
109
Programming for Engineers EE057IU
110