Me168 Chapter1 Complete
Me168 Chapter1 Complete
Me168 Chapter1 Complete
COLLEGE OF ENGINEERING
KNUST
1
Course Outline
Course Objectives
• To describe and explain some basic
terminology used in computer programming
• To write and run computer programs in
MATLAB
• To develop, compile and run code in C/C++
Contact Hours
• 2 hrs theory
• 1 hr practicals
2
Contact Hours
Kumasi Campus
• MECH 1
• Theory (Fridays, 8:00 – 10:00 am)
• Lab (Mondays, 10:30 am – 3:00 pm)
• AERO 1
• Theory (Tuesdays, 8:00 – 10:00 am)
• Lab (Tuesdays, 3:30 pm – 5:30 pm)
Obuasi Campus (MECH 1)
• Theory (Thursdays, 8:00 – 10:00 am)
• Lab (Thursdays, 10:00 – 11:00 am)
3
Course Outline
Course Content
• Computer Programming Fundamentals
• Computer Software
• MATLAB Programming
• C/C++, Object Oriented Programming
Assessment
• Quizzes, Assignments, Mid-Semester, Final Exams
4
Assessment
• Quizzes, Assignments S1 = 30%
• Attendance
• Mid Semester Exams (S2 = 30%)
• End of Semester Exams (S3 = 70%)
• Final Score = (S1 + S2)/2 + S3
5
Class Regulations
6
References
• Chapra, S.C., Applied Numerical Methods with
MATLAB for Engineers and Scientists, 3rd Int.
Ed., 2012
• Nakamura, S., Numerical Analysis and
Graphic Visualization with MATLAB, Prentice
Hall Inc. 1996
• Any C/C++ Programming Book
7
Computer Programming
Relevant Terminology (Review)
Computer Programming:
The process of creating a computer program
Computer Program:
A set of instructions that can be executed by
the computer to perform a specific task
Computer Software
A set of computer programs
8
How Do Computers Store Data?
There are many types of data a
computer needs to store or operate on,
e.g.,
• Text, numbers, images, sound, videos,
programs, instructions, etc.,
12
Storage of Numbers
Numbers are stored as a series of bits
• Example: How will the computer store the
number 42?
Therefore, in general,
• 1 bit yields 2 patterns
• 2 bits yields 4 patterns
• 3 bits yields 8 patterns 14
Storage of Numbers
Mathematically,
• 1 bit – 2 patterns
• 2 bits – 4
• 3 bits – 8
• 4 bits – 16
• 5 bits – 32
• 6 bits – 64
• 7 bits – 128
• 8 bits – 256
• n bits yields 2n different patterns 15
Storage of Numbers
One Byte – 256 Bit Patterns
• E.g., 00000000, 00000001, 00000010, etc.
• Converting the base two numbers to decimal
yields values between 0 and 255
• Thus, a byte can store numbers in the range
0 to 255
17
Binary to Decimal Conversion
𝑑𝑛−1 𝑑𝑛−2 … 𝑑2 𝑑1 𝑑0 2 = 𝑑𝑛−1 × 2𝑛−1 + 𝑑𝑛−2
× 2𝑛−2 + ⋯ + 𝑑2 × 22 + 𝑑1 × 21 + 𝑑0 × 20
19
Bit Numbering (LSB and MSB)
Given the binary number 𝑑𝑛−1 𝑑𝑛−2 … 𝑑2 𝑑1 𝑑0 𝑡𝑤𝑜
with digits di the bits are numbered from right to
as follows: 0, 1, 2, 3, …, n-2, n-1.
Example 1: The bits in the 8-bit number
110010112 are numbered as shown below
MSB LSB
byte 3 byte 2 byte 1 byte 0
22
Least Significant Byte (LSB), Most
Significant Byte (MSB) and Endianness
Computers differ in the way they store numbers
Some computers store the MSB at the smallest
memory address and the LSB at largest
memory address. This is called Big-endian
storage (e.g., Motorola Power PC, Sun
Microsystems, etc)
Other computers store the LSB at the smallest
memory address and the MSB at the largest
memory address. This is called Little-endian
storage (e.g., Windows PC and MAC) 23
Big-endian vs Little-endian Storage
24
Representation of Signed (Positive and
Negative) Numbers
Computer programs calculate both positive and
negative numbers. Thus, it is necessary to
distinguish between them.
26
The 2’s Complement Method
If negative numbers are to be included, the 3 bits can
be used to store the following positive and negative
numbers:
27
The 2’s Complement Method
28
Representation of Signed Numbers
Self-assessment: By means of calculations
determine the set of signed numbers that can be
encoded by exactly
n = 4 bits.
In computer programming, the sets of signed
and unsigned numbers are called Fundamental
Numeric Data Types.
All programming languages such as MATLAB
and C/C++ define and use fundamental data
types
29
Some Fundamental Data Types
30
Character Encoding (ASCII and Unicode)
Character encoding is the conversion text data
into binary numbers.
To store text data in memory each character is
first converted into a unique decimal number
using some encoding system.
• The most widely used encoding system is ASCII
• ASCII stands for American Standard Code for
Information Interchange
‣ Developed in the US, hence encodes only English
characters, numbers and other symbols
31
ASCII Encoding Scheme
The ASCII encoding scheme has a character set
containing a total of 128 characters
• Each character is assigned a unique value
between 0 and 127
• Question: How many bits are required to store a
number between 0 and 127?
‣ Answer = 7 bits: Since only 7 bits are required for
storage, ASCII characters by convention are stored
with 8 bits ( = 1 byte)
‣ In the 8 bits, the most significant bit (MSB) is set to 0
32
ASCII Character Codes
Character ASCII Code Binary Value
Space 32 0010 0000
! 33 0010 0001
“ 34 0010 0010
# 35 0010 0011
$ 36 0010 0100
% 37 0010 0101
& 38 0010 0110
‘ 39 0010 0111
( 40 0010 1000
) 41
* 42
+ 43
, 44
33
ASCII Character Codes
Character ASCII Code Binary Value
- 45
. 46
/ 47
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
34
ASCII Character Codes
Character ASCII Code Binary Value
: 58
; 59
< 60
= 61
> 62
? 63
@ 64
A 65
B 66
C 67
D 68
E 69
F 70
35
ASCII Character Codes
Character ASCII Code Binary Value
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
36
ASCII Character Codes
Character ASCII Code Binary Value
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
[ 91
\ 92
] 93
^ 94
_ 95
` 96
37
ASCII Character Codes
Character ASCII Code Binary Value
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
38
ASCII Character Codes
Character ASCII Code Binary Value
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122
39
ASCII Character Codes
Character ASCII Code Binary Value
{ 123
| 124
} 125
~ 126
Delete 127
43
Hexadecimal Number System
Decimal Binary Hexadecimal
13 1101 D or d
14 1110 E or e
15 1111 F or f
44