largest - UCL Computer Science

Introduction to Computers
and Programming
Essential feature of a computer is the
separation of operations and data.
1
Hardware






Leibnizt 17th Century (adding machine)
Babbage 1835
Turing 1936
Post war: ENIAC 30 tons (needed external wiring)
and EDSAC (first stored program) UK – based on
Von Neumann Design
Now similar design but speed in MIPS and BIBS
rather than 6 – 10!
The physical components




Accept input, Display output.
Store information.
Perform arithmetic and logical operations
Control sequence of system
2
The physical components
Central Processing Unit (CPU)
Arithmetic
and
logic unit (ALU)
INPUT
Memory (RAM)
Control
OUTPUT
Secondary Storage, Hard disk,
Floppy, Zip, CD
3
The Computer’s Core:
CPU and Memory
A collection of switches and relays
Transforms information
User provides input
Computer follows instructions
Returns output to user
Each type understands specific instructions,
Opcodes, (IBM or Mac)
Communicates with the other parts of the
computer.
4
RAM (Random Access Memory)
RAM most common type of primary storage
RAM can store programs and data temporarily
RAM has memory locations with unique addresses
Computers can quickly retrieve from RAM
RAM is volatile memory it is temporary and constantly
changing. Power off — RAM is cleared, whether you saved
it or not!, Save to secondary storage.
5
Memory
Addresses
: :
01010011
01000011
Imagine a stack
of letter boxes.
Each box has a unique
address.
Each box has can
contain some
information (a byte).
Each byte has 8 bits
which can be a 0 or 1.
Memory Address
64000
63999
High
: :
01001111
3002
01001111
3001
3000
01001111
: :
01001111
01100111
0001
0000
Low
6
*
Computer Software
The programs


Systems
Applications
Like English, programs written in a
programming language have their own
syntax.
Unlike English there are no irregular
verbs and exceptions to be learnt
7
Programming Languages
Over 200 languages
Language depends upon task
Assembly, COBOL, Logo, C++, Pascal,
FORTRAN, BASIC.
8
Programming languages (Cont)
 Machine languages

11000000 000000000001 000000000010
Opcode


Add contents of memory location 1 to contents of memory
location 2
Horrid to write programs!
Assembly

ADD 1
2
 Needs an assembler to translate to machine language
9
Programming Languages
(cont)
Levels of Computer Languages:
Low (long way from English)

machine language, assembly
High (Closer to English)

COBOL, Pascal, FORTRAN, Logo, Basic
Mid-level?

C, C++
10
Programming Languages
(cont)
Procedural languages focus on tasks. Breaks
programs down into sub-procedures.

Pascal, Fortran.
Object-Oriented languages (OOP) focus on
data, looks at data and operations that are
performed on the data.

Java, Smalltalk
Special Languages (very high)

Prolog, Lisp
11
However
Computer Can’t Understand English!
OR
C ++, COBOL, FORTRAN, Pascal, BASIC,
Prolog etc.
Computer Understand Machine
language.
12
Translation/Interpretation
Translation to Machine Language



Interpreter
Compiler/linker
Assembler
13
Compilation Creating and executable file
with C++
source code
compiler h-files used
object code
linked to
libraries
.exe file
14
Developing/Constructing Programs
15
Problem Solution and Software
Development
Phase I: Analysis, Design and
Development.
Phase II: Documentation
Phase III: Maintenance
16
A Close Look at Phase I
1. Analyse/Solve the problem
2. Develop/Design a solution produce an
algorithm
3. Code the solution
4. Test/Verify the program
17
A Close Look at Phase I
Problem analysis and Solution finding

Need a process of solving a problem and then
getting the solution onto a computer.
Initially YOU need to know how to solve a
problem in English, before you can get a
computer to tackle the problem.
The problem is that computers do not
understand English.
You need to get the solution into a form the
computer understands.
18
Problem Solving:
generalized problem-solving approach
1. Understand the problem
1. Roughly sketch out
2. Identify inputs/outputs unknowns, assumptions.
3. Notation: sketch out ideas for variable names
and types.
4. Sketch out any equations/relations that you
know or that might be useful.
2. Devise a plan
1. Write the plan down!
2. Check the plan
3. Implement the Plan
1. Check the implementation
4. Examine Solution
19
1. Understand The Problem




Draw a figure.
What is the data, Try to introduce suitable
variables. What sort of variables should they be?
What is the unknown? What is known?
Are there any conditions that need satisfying?
 e.g. A grading program will have different grades
according to exam scores.


Are the conditions sufficient to determine the
unknown? Or is it insufficient? Or redundant? Or
contradictory? If not, what do you need to
determine the unknown?
Can you write the conditions down?
20
Output of Step 1
One or more pages of rough work,
nothing formal
Good idea of problem requirements
Good idea of inputs, outputs, constants,
conditions, assumptions.
21
2. Devise a Plan

Find the connection between the data and the
unknown.





You might need to consider separate problems if you
can’t find an intermediate connection. You should
eventually come up with a plan of the solution.
Have you seen the problem before?
Have you seen the same problem in a slightly
different form?
Do you know a related problem?
Do you know a theorem that could be useful?

Pythagoras?
22
2. Devise a Plan Continued



look at the unknown! And try to think of a
familiar problem having the same or a
similar unknown. Is there a problem related
to yours and solved before. Can you use it?
Can you use its method? Should you
introduce some other element in order to
make its use possible?
Can you restate the problem?
If you cannot solve the proposed problem,
try to solve some related problem first. Can
you solve a simplified form with particular
conditions.
23
2. Devise a Plan Continued

Can you imagine a more accessible related
problem? A more general problem? A more
special problem?
 E.g. If you need to count the number of words
in a file. Solve the sub-problem of counting the
number of words on a line first.

Can you solve a part of the problem? Keep
only some of the conditions
24
2. Devise a plan continued
Can you derive something useful from the data?
Can you think of other data appropriate for determining
the unknown?
Can you change the unknown or the data, or both if
necessary, so that the new unknown and the new data
are nearer to each other?

E.g. Change from one coordinate system to another,
Did you use all the data?
Did you use all the conditions?
25
Devise a plan continued
Is there a different way of solving the
problem. Can you use this to check your
solution? is it better?
26
Output of Step 2
Top Down Design
A formal written design.
State inputs, outputs
Write steps to process input unambiguously
using a semi formal notation (PDL, Flowchart
or structure diagram?


Can you translate each step of the plan into C++
easily?
No … Go back and refine
Verification/testing procedures documented
(A test table).
27
3. Coding the Solution

Check code for each step Incrementally.
Can you see clearly that the step is
correct? Can you prove that it is correct?
How would you prove it is correct? i.e.
what are the tests you would use to prove
your plan?
28
4. Examining the solution
Check the result? Use tests identified in
step 2.
Can you use the solution, or the method,
for some other problem?
29
Summary
 So find a solution, write it down in English.
 Write it down more formally in Pseudo-code
(PDL), or as a structure diagram or a flow
chart.
 Start your programming environment and
convert your pseudo-code to C++
 Tell the computer to convert the C++ to
machine language (compile)
 Tell the computer to load the machine
language into memory and execute (Run).
30
Algorithms
An algorithm is simply the technical word for
your solution/Plan.
It is the step by step sequence of instructions
to complete a task it will consist of:




Sequences
Selection
Iteration
Invocation
31
Example of Top Down Design
Making Cups of Coffee
Problem: Make 10 cups of coffee
Initial (Top) Plan
Make 10 cups of coffee
Refinement 1
Repeat 10 times
Make a cup of Coffee
End repeat
32
Example of Top Down Design
Making Cups of Coffee
Refinement 2
Repeat 10 times
Put coffee in Cup
Put boiling water in Cup
End repeat
33
Example of Top Down Design
Making Cups of Coffee
Refinement 3
Repeat these tasks 10 times
1.
Get mug out of cupboard
1. End Repeat
2. Get Coffee out of cupboard
3. Fill Kettle
4. Put Kettle on to boil
5. Repeat 10 times
1.
2.
3.
4.
Put Coffee in cup
If Kettle boiled put water in cup
If wanted Add milk
If wanted Add sugar
6. End repeat
34
Another Example
Linear Search
Problem:
Find the largest number in a list of
numbers.
35
Design Example
Linear Search PDL
call the 1st number largest
call the 2nd number current
1 while we are not finished
if current > largest
4
label current as largest
5
call next number in the list current
6 repeat the while
7 output the largest
2
36
Linear Search Flowchart
Start
largest = first No
Current = Second No
Yes
Largest = Current
Current > Largest?
No
Current = Next No
Yes
Any more numbers?
No
Display largest
Stop
37
Phase II Documentation
Improve readability
You remember
Others understand
Enables easy maintenance
38
What is documentation
Your formal design paperwork.

Should be detailed enough for any
programmer to implement
Comments (annotations) actually
written into the code.
39
Phase III Maintenance
Correction of problems after delivery
Add features
Adapt to changing needs.


E.g. Y2K problem
Needs good documentation!
40
What we are aiming for is
Structured Programming
Advantages
Not one giant step, but breaks in to smaller
and smaller chunks.
Programmer concentrates on details
Easy to do in teams
Easy to keep track of development
Creates more reliable and robust programs.
Makes more possible reuse and extensibility.
41
Structured Programming
Errors isolated
Design written in code
Improves reliability
Minimizes risk of Failure
Faster development
Full documentation

Maintainability
42
Writing Down Solution Designs
Flowcharts
Pseudo-code
Structure Diagrams
43
Pseudo-code/ PDL
•Logical steps
•Written in English
•Language
independent
•Labels
•Indenting for
logical structures
1. begin game
1.1 display instructions
1.2 pick a number between 1 and
100
2. repeat turn until number is guessed or
seven turns are completed
2.1 input guess from user
2.2 respond to guess
3. end repeat
4. end game
4.1 display end message
44
Flowcharts
Logical steps represented
by standard symbols
display instructions
get number
Yes
is number
even?
No
continue
45
Structure Diagrams
Inventory
Control
Program
1 Enter data
2
Do
Calculations
3
Display
report
46
Summary
No Magic Bullet
Adopt good practice (structured
programming)
Apply Three Phases



Phase I – analyse, develop algorithm, code
test
Phase II – Document
Phase III - Maintain
47