Download - Introduction to Computers and Programming
1
Introduction to Computers and Programming
Essential feature of a computer is the separation of operations and data.
2
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
3
The physical components
INPUTINPUT OUTPUTOUTPUT
Memory (RAM)Memory (RAM)Secondary Storage, Hard disk, Secondary Storage, Hard disk,
Floppy, Zip, CDFloppy, Zip, CD
Arithmetic and
logic unit (ALU)
ControlControl
Central Processing Unit (CPU)Central Processing Unit (CPU)
4
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.
5
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.
6
Addresses
*
01010011
01000011
01001111
01001111
01001111
01001111
01100111
Imagine a stackof 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.
0000
0001
3000
3001
3002
64000
: :
: :
63999
Low
High
Memory Memory Address
: :
7
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
8
Programming Languages
Over 200 languages Language depends upon task Assembly, COBOL, Logo, C++, Pascal, FORTRAN, BASIC.
9
Programming languages (Cont)
Machine languages 11000000 000000000001 000000000010
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
Opcode
10
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++
11
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
12
However
C ++, COBOL, FORTRAN, Pascal, BASIC, Prolog etc.
Computer Can’t Understand English! OR
Computer Understand Machine language.
13
Translation/Interpretation
Translation to Machine Language Interpreter Compiler/linker Assembler
14
Compilation Creating and executable file with C++
source source codecode
compiler -h-files used
object codeobject code linked to libraries
.exe file.exe file
15
Developing/Constructing Programs
16
Problem Solution and Software Development
Phase I: Analysis, Design and Development.Phase II: DocumentationPhase III: Maintenance
17
A Close Look at Phase I
1. Analyse/Solve the problem2. Develop/Design a solution
produce an algorithm3. Code the solution4. Test/Verify the program
18
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.
19
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 Plan1. Check the implementation
4. Examine Solution
20
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?
21
Output of Step 1
One or more pages of rough work, nothing formalGood idea of problem requirementsGood idea of inputs, outputs, constants, conditions, assumptions.
22
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?
23
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.
24
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
25
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?
26
Devise a plan continued
Is there a different way of solving the problem. Can you use this to check your solution? is it better?
27
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).
28
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?
29
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?
30
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).
Summary
31
AlgorithmsAn 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
32
Example of Top Down DesignMaking Cups of Coffee
Problem: Make 10 cups of coffee
Initial (Top) PlanMake 10 cups of coffee
Refinement 1Repeat 10 times
Make a cup of CoffeeEnd repeat
33
Example of Top Down DesignMaking Cups of Coffee
Refinement 2Repeat 10 times
Put coffee in CupPut boiling water in Cup
End repeat
34
Example of Top Down DesignMaking Cups of Coffee
Refinement 3Repeat these tasks 10 times
1. Get mug out of cupboard
1.End Repeat2.Get Coffee out of cupboard3.Fill Kettle4.Put Kettle on to boil5.Repeat 10 times
1. Put Coffee in cup2. If Kettle boiled put water in cup3. If wanted Add milk4. If wanted Add sugar
6.End repeat
35
Another Example
Linear SearchProblem:
Find the largest number in a list of numbers.
36
Design Example Linear Search PDLcall the 1st number largestcall the 2nd number current
1 while we are not finished2 if current > largest4 label current as largest5 call next number in the list current6 repeat the while7 output the largest
37
Linear Search Flowchart
largest = first NoCurrent = Second No
Largest = CurrentCurrent > Largest?
Current = Next NoAny more numbers?
Display largest
No
No
Yes
Yes
Start
Stop
38
Phase II Documentation
Improve readabilityYou rememberOthers understandEnables easy maintenance
39
What is documentation
Your formal design paperwork. Should be detailed enough for any
programmer to implement
Comments (annotations) actually written into the code.
40
Phase III Maintenance
Correction of problems after deliveryAdd featuresAdapt to changing needs. E.g. Y2K problem Needs good documentation!
41
What we are aiming for is Structured Programming
AdvantagesNot one giant step, but breaks in to smaller and smaller chunks.Programmer concentrates on details Easy to do in teamsEasy to keep track of developmentCreates more reliable and robust programs.Makes more possible reuse and extensibility.
42
Structured Programming
Errors isolatedDesign written in code Improves reliabilityMinimizes risk of FailureFaster developmentFull documentation Maintainability
43
Writing Down Solution
Designs
FlowchartsPseudo-codeStructure Diagrams
44
•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 1002. 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
Pseudo-code/ PDL
45
Logical steps representedby standard symbols
display instructionsdisplay instructions
get numberget number
is numberis numbereven?even?
YesYes NoNo
continucontinuee
Flowcharts
46
Structure Diagrams
Inventory Control Program
Enter data Do Calculations
Display report
1 2 3
47
Summary
No Magic BulletAdopt good practice (structured programming)Apply Three Phases Phase I – analyse, develop algorithm,
code test Phase II – Document Phase III - Maintain