Computer
Programming
(CS101)
Lecture-02
Asst Prof. Rizwan Khan
Department of Computer & Engineering
[email protected]
Introduction to programming
What is a program?
 How to cook?
 The algorithm
 Are you a programmer?
Computer Programming
10/2/2015
3
Pseudocode
 This is the pseudocode for a game of Monopoly
Computer Programming
10/2/2015
4
Flowcharts
Computer Programming
10/2/2015
5
Flowcharts details
Computer Programming
10/2/2015
6
Languages
 Low level (processor dependent)
 Machine code, assembler
 High level: structured, procedural
 Fortran, C, Pascal…
 High level: object oriented
 C++, Java, C#, Perl, Objective-C…
 Virtual machines
 Java, C#…
 Scripting
 Perl, Python, JavaScript…
Computer Programming
10/2/2015
7
Source code -> Object code
 Compiler+linker

• Compiler+linker
Fortran, C, Pascal, C++…
 Interpreter

• Interpreter
Basic, Perl…
 Slow to execute, but fast
to debug (no need to
recompile)
 Intermediate

• Intermediate
Java
Computer Programming
 Fast to execute, but slow
to debug
 Slow…
10/2/2015
8
Source code
Instructions







Data structures
Statement, blocks
Affectation
Operators
Loops
Tests
Subroutines
Comments
Computer Programming






10/2/2015
Variable
List
Array
Hash
Pointers
Objects
9
Source code (2)
Statement, blocks

Variable
One or more
instructions for the
processor


Affectation

Change to a variable
Operator

affect one or more
variable
 + * - / AND OR NOT…
Computer Programming
10/2/2015
A region in memory
that can be modified
Exists in different
types
 Scalar, char, numeric,
boolean
 List, array
 Hash
 Combination->data
structure
10
Source code (3)
Pointers
Loops


Allow the computer to
repeat blocks
Tests

Reference to region in
memory (address)
Objects

Decide what to do
Combination of data
and code
Subroutines

Programs frequently
called (functions)
Comments

The most important
lines of the source
code…
Computer Programming
10/2/2015
11
Number Systems
2-1 INTRODUCTION
A number system defines how a number can be
represented using distinct symbols. A number can be
represented differently in different systems. For
example, the two numbers (2A)16 and (52)8 both
refer to the same quantity, (42)10, but their
representations are different.
Several number systems have been used in
the past and can be categorized into two groups:
positional and non-positional systems. Our main
goal is to discuss the positional number systems, but
we also give examples of non-positional systems.
Computer Programming
10/2/2015
13
Common Number Systems
System
Base Symbols
Used by
humans?
Used in
computers?
Decimal
10
0, 1, … 9
Yes
No
Binary
2
0, 1
No
Yes
Octal
8
0, 1, … 7
No
No
Hexadecimal
16
0, 1, … 9,
A, B, … F
No
No
Computer Programming
10/2/2015
14
Quantities/Counting (1 of 3)
Decimal
0
1
2
3
4
5
6
7
Computer Programming
Binary
0
1
10
11
100
101
110
111
10/2/2015
HexaOctal decimal
0
1
2
0
1
2
3
4
5
6
3
4
5
6
7
7
15
Quantities/Counting (2 of 3)
Decimal
8
9
10
11
12
13
14
15
Computer Programming
Binary
1000
1001
1010
1011
1100
1101
1110
1111
10/2/2015
HexaOctal decimal
10
11
12
8
9
A
13
14
15
16
B
C
D
E
17
F
16
Quantities/Counting (3 of 3)
Decimal
16
17
18
19
20
21
22
23
Computer Programming
Binary
10000
10001
10010
10011
10100
10101
10110
10111
10/2/2015
HexaOctal decimal
20
21
22
10
11
12
23
24
25
26
13
14
15
16
27
17
Etc.
17
Conversion Among Bases
The possibilities:
Computer Programming
Decimal
Octal
Binary
Hexadecimal
10/2/2015
18
Quick Example
2510 = 110012 = 318 =
1916
Base
Computer Programming
10/2/2015
19
Decimal to Decimal (just for fun)
Decimal
Octal
Binary
Hexadecimal
Next slide…
Computer Programming
10/2/2015
20
Weight
12510 =>
5 x 100=
5
2 x 101= 20
1 x 102= 100
125
Base
Computer Programming
10/2/2015
21
Binary to Decimal
Computer Programming
Decimal
Octal
Binary
Hexadecimal
10/2/2015
22
Binary to Decimal
Technique



Multiply each bit by 2n, where n is the “weight” of
the bit
The weight is the position of the bit, starting from
0 on the right
Add the results
Computer Programming
10/2/2015
23
Example
Bit “0”
1010112 =>
1
1
0
1
0
1
x
x
x
x
x
x
20
21
22
23
24
25
=
=
=
=
=
=
1
2
0
8
0
32
4310
Computer Programming
10/2/2015
24
Octal to Decimal
Computer Programming
Decimal
Octal
Binary
Hexadecimal
10/2/2015
25
Octal to Decimal
Technique



Multiply each bit by 8n, where n is the “weight” of
the bit
The weight is the position of the bit, starting from
0 on the right
Add the results
Computer Programming
10/2/2015
26
Example
7248 =>
Computer Programming
4 x 80 =
2 x 81 =
7 x 82 =
10/2/2015
4
16
448
46810
27
Hexadecimal to Decimal
Computer Programming
Decimal
Octal
Binary
Hexadecimal
10/2/2015
28
Hexadecimal to Decimal
Technique



Multiply each bit by 16n, where n is the “weight”
of the bit
The weight is the position of the bit, starting from
0 on the right
Add the results
Computer Programming
10/2/2015
29
Example
ABC16 =>
C x 160 = 12 x
1 =
12
B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560
274810
Computer Programming
10/2/2015
30
Assignment-02
 1.List the differences between Compiler and
interpreter.
 2.List the differences between low level and
high level languages.
 3.
HexaDecimal
Computer Programming
Binary
100010
1110101
100101
101010
10/2/2015
Octal
403
555
703
222
decimal
2CB
1FD
11CE
1AF
31
Descargar

Entertainment Software - Digi-ED