Lecture Data Structures and Practise

Download Report

Transcript Lecture Data Structures and Practise

Lecture 2
Introduction to C
Programming
Arne Kutzner
Hanyang University / Seoul Korea
This week …
• You will learn:
– From the first Computers to the C
Programming Languages
• Von Neumann Architecture
• The Idea of “binary computing”
• Origins of Programming Languages
– About Flowcharts and Algorithms
Introduction to C Programming
L2.2
Von Neumann’s Computer
Architecture
Foundations of modern
Computers and their
Programming
Computers until the second world
war and before
• The earliest computing machines had fixed
programs.
– The earliest computers were not so much
"programmed" as they were "designed".
– "Reprogramming", when it was possible at all, was
a laborious process, starting with flow charts and
paper notes, followed by detailed engineering
designs, and then the often arduous process of
physically rewiring and rebuilding the machine.
Introduction to C Programming
L2.4
Examples of “fixed program
computers”
• Colossus Mark I (Dec. 1943) / Colossus
Mark II (June 1944)
Purpose:
Message Decoding during
World War 2
(was used to help decipher
teleprinter messages which
had been encrypted using the
Lorenz SZ40/42 machine
(Enigma))
INFO in the Web: http://en.wikipedia.org/wiki/Colossus_computer
Introduction to C Programming
L2.5
Von Neumann Architecture
(proposed 1945)
Memory
Control Unit
Arithmetic Logic
Unit
Accumulator
Input
Introduction to C Programming
Output
L2.6
Von Neumann Architecture (2)
• The Control Unit “understands” instructions like
e.g. copy data from the memory to the accumulator
=> Instruction Set Architecture
• The Arithmetic Logic Unit performs arithmetic
operations and Boolean operations.
• The Memory stores data and Program (Series of
control unit instructions
• The Input takes data in form of an data stream from
some medium
• The Output receives data in form of data stream and
delivers them to some output medium
Introduction to C Programming
L2.7
Von Neumann Architecture (3)
• Von Neumann’s approach was revolutionary
because:
He created an instruction set architecture
and detailed the computation as the
execution of a series of instructions (the
program) stored in the memory.
– Program and data share the memory !!
• Highly flexible computer design
Introduction to C Programming
L2.8
Structure of Memory
Addresses
• Ordered sequence of
storage locations called
memory cells.
• Each memory cell is
identified by a unique
address
Memory Cells
0000
0001
0002
0003
43
87
06
00
0004
0005
12
00
Content of a
memory cell
Introduction to C Programming
L2.9
Instruction representation
• Instruction are encoded using
sequences of bits
• Example PDP-11(single operand
instructions): INFO in the Web: http://en.wikipedia.org/wiki/PDP-11
16 bit = 2 byte
10 bit
1
5
3 bit
6 5
OP-Code
3
Mode
3 bit
2
0
Register
Introduction to C Programming
L2.10
Computer nowadays?
• Almost all modern computer have still a
Von Neumann Architecture
Main Memory
CPU
Input
Introduction to C Programming
Output
L2.11
How to program “von
Neumann” computer ?
Assembler Code
• Sequence of processor instructions in human
readable text-form – this is called Assembler
Code
• Example for PDP-11 Assembler Code:
.TITLE
This PDP-11
Assembler Code
prints “Hello World”
HELLO WORLD
.MCALL .TTYOUT,.EXIT
HELLO:: MOV
1$:
MOVB
BEQ
.TTYOUT
BR
DONE:
.EXIT
MSG:
#MSG,R1 ;STARTING ADDRESS OF STRING
(R1)+,R0 ;FETCH NEXT CHARACTER
DONE
;IF ZERO, EXIT LOOP
;OTHERWISE PRINT IT
1$
;REPEAT LOOP
.ASCIZ /Hello, world!/
.END
HELLO
Introduction to C Programming
L2.13
Assembler and Programming
Languages
• Assembler was the first form of some
primitive (low-level) programming
language
• Definition:
A programming language is a set of
rules that provides a way of telling a
computer what operations to perform.
Introduction to C Programming
L2.14
Disadvantages of Assembler Code
• Low level of abstraction
– Code is processor (vendor)-specific
Therefore: Non portable
• The code is unstructured
– Difficult to read
– Risk of “spaghetti-structure”
• Time intensive to learn and to program
Introduction to C Programming
L2.15
High-level Programming
Languages
• The wish for some programming medium that
is more abstract than assembler lead to the
development of High-level Programming
Languages
• Definition:
A high-level programming language is a
programming language that, in comparison to
low-level programming languages, may be
more abstract, easier to use, or more portable
across platforms.
Introduction to C Programming
L2.16
Early high-level programming
languages
• Fortran:
For programming in the scientific area
(number crunching)
• Cobol:
For programming of business applications
(e.g. banking applications)
• Lisp:
Development originally motivated be research
in the Artificial Intelligence area
Introduction to C Programming
L2.17
Fortran Code Example
c23456789012345678901234567890123456789012345678901234567890123456789012
PROGRAM sphere
c This program determines the surface area and volume of a sphere, given
c its radius.
c Variable declarations
REAL rad, area, volume, pi
c Definition of variables
c
rad = radius, area = surface area, volume = volume of the sphere
c Assign a value to the variable pi.
pi = 3.141593
c Input the value of the radius and echo the inputted value.
PRINT *, 'Enter the radius of the sphere. '
READ *, rad
PRINT *, rad, ' is the value of the radius. '
c Compute the surface area and volume of the sphere.
area = 4.0 * pi * rad**2
volume = (4.0/3.0) * pi * rad**3
c Print the values of the radius (given in cm), the surface area (sq cm),
c and the volume (cubic cm).
PRINT *,'In a sphere of radius', rad, ' , the surface area is',
+
area, ' and its volume is', volume, '. '
STOP
END
Introduction to C Programming
L2.18
COBOL Code Example
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. Multiplier.
AUTHOR. Michael Coughlan.
* Example program using ACCEPT, DISPLAY and MULTIPLY to
* get two single digit numbers from the user and multiply them together
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1
01 Num2
01 Result
PIC 9 VALUE ZEROS.
PIC 9 VALUE ZEROS.
PIC 99 VALUE ZEROS.
PROCEDURE DIVISION.
DISPLAY "Enter first number (1 digit) : " WITH NO ADVANCING.
ACCEPT Num1.
DISPLAY "Enter second number (1 digit) : " WITH NO ADVANCING.
ACCEPT Num2.
MULTIPLY Num1 BY Num2 GIVING Result.
DISPLAY "Result is = ", Result.
STOP RUN.
Introduction to C Programming
L2.19
Programming Languages
Introduction to C Programming
L2.20
Why was C created ?
• The origin of C is closely tied to the
development of the Unix operating system
– Originally Unix was implemented in Assembler for
the PDP-7 (1970)
– 1973 Unix was “rewritten” for the PDP-11
architecture using the newly developed C
Programming Language
• An older language “B” was unsuitable for this purpose
due to some language design lacks
Introduction to C Programming
L2.21
Characteristics of C
• Primitive but fast
• Quite “low-level” compared to e.g. Java
– Some people say: “More or less like
assembler but independent of any specific
instruction set”
• Strange and quite cryptic syntax
• Difficult to grasp
Introduction to C Programming
L2.22
The Idea of Binary Computing
Binary Number, Hexadecimal
Number and the encoding of
characters
Binary Numbers
• A sequence of 1s and 0s can be
interpreted as a decimal number
2
1
0
e.g. 101 = 1*2 +0*2 +1*2 = 5
• These type of number (0 and 1
represented) is called Binary Number
Introduction to C Programming
L2.24
8 bits = 1 Byte
• Todays computer architectures are
based on a 8 bits (= 1 Byte) approach.
– E.g. one memory cell has nowadays 1 Byte
7
6
5
4
3
2
1
0
1 0 1 0 1 1 0 0
Introduction to C Programming
= 172
L2.25
Hexadecimal Numbers (1)
• Counting on the
foundation of 16
digits (0 – F)
• Reasonable in the
context of the
decomposition of
binary number into
blocks of 4 bits
Introduction to C Programming
decimal
binary
hex
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
10
1010
A
11
1011
B
12
1100
C
13
1101
D
14
1110
E
15
1111
F
L2.26
Hexadecimal Numbers (2)
16 bit
• Example:
8 bit (1 Byte)
binary
hex
decimal
8 bit (1 Byte)
0 1 1 0 1 1 1 0 1 1 1 0 0 1 1 1
6
E
E
7
28391
Introduction to C Programming
L2.27
Exercise
•
•
What is the number 01000111 in
decimal notation?
What is the number 156 in
hexadecimal notation?
Introduction to C Programming
L2.28
Characters and Numbers
• Using a Code Table numbers can be
mapped to characters of some
alphabet.
Example (part of the ASCII table):
Character
Code (decimal)
‘A’
65
‘B’
66
‘C’
67
‘D’
68
Introduction to C Programming
L2.29
Sequences of Characters
(Strings)
• Text is represented as a sequence of
characters (numbers)
• Such sequences are called Strings
• Example:
H
E
L
L
O
72
69
76
76
79
Introduction to C Programming
The Text "Hello"
as sequence of
characters
(numbers)
L2.30
Algorithms and Flowcharts
Binary Number, Hexadecimal
Number and the encoding of
characters
Algorithms - Introduction
•
In order to solve some problem, we need some
description how to solve it.
E.g. Cooking some eggs:
How to cook eggs?
1.
2.
3.
4.
5.
•
Put some water into a pot
Heat the water until it cooks
Put the eggs into the water
Wait 3 Minutes
Take the eggs out of the pot and cool them under floating
water
Such a description of a problem solution is called an
Algorithm
Introduction to C Programming
L2.32
Algorithms described as Flowcharts
START
Water in pot ?
Yes
No
Our “Cook-eggs”-Algorithm
as Flowchart:
put water into pot
Wait 3 Minutes
Heat water
Water cooking ?
Take eggs out of pot
No
Cool the eggs
Yes
END
Introduction to C Programming
L2.33
Flowchart Atoms
Statement
• Statement – Some operation as e.g.
“put the eggs into water”
put water into pot
Introduction to C Programming
L2.34
Flowchart Atoms (cont.)
Condition
No
Yes
• Condition: Some yes/no question
(condition). Depending on the answer
Yes (True) or No (False) we go different
ways.
Introduction to C Programming
L2.35
Combined Flowchart Constructs
if side
Yes
Condition
No
else side
Statement
Statement
Statement
Statement
• If/else – alternative: Construct where
select on of two alternative sequences
of statements, depending on the result
of the condition
Introduction to C Programming
L2.36
Combined Flowchart Constructs
(cont.)
• If/else example: (Here we do nothing on
one side, but this is ok)
Water in pot ?
Yes
No
put water into pot
Introduction to C Programming
L2.37
Combined Flowchart Constructs
(cont.)
Statement 1
Statement n
Question
• Loop: Special construct with a
Condition where one alternative of the
Condition creates a circular flow
Introduction to C Programming
L2.38
Combined Flowchart Constructs
(cont.)
• Loop Example:
Heat water
Water not cooking ?
Introduction to C Programming
YES
L2.39
Algorithmic Description
• Solutions for all realistic Problems can
be algorithmically described by
combing the introduced elements:
–Statements
–If/else alternatives
–Loops
Introduction to C Programming
L2.40
Variables
• Variables are named containers that
store some type of data, e.g. a number.
Example:
Variable x Value of x
x
read x from user input
User input is typically
something typed on the
keyboard
0
user input is 93
x
Introduction to C Programming
93
L2.41
Variables in Flowcharts
• Example: Calculation of the maximum
of two numbers:
read x from user input
read y from user input
No
x>y
set max to y
Yes
set max to x
print max on output
Introduction to C Programming
L2.42
Exercise
•
Take a piece of paper and draw a
flowchart that:
1. reads the value of three variables x and y
and z
2. calculates the minimum of the three
variables read before
Introduction to C Programming
L2.43