Title: COMP1105
1COMP1105
- The Structure Theorem
- Modularization
- Top-Down design and
- Stepwise Refinement
- Pseudocode and
- Flowcharts
- Algorithms
- Control structures
- 2. Problem Solving and programming
2Problem solving and software development
- Software Engineering
- Small programs (less than 5000 lines) can be
reliably developed by one person - Most software is developed in much larger
environments - with teams of programmers or even
collections of teams - Managing large projects involves many more
organisational difficulties than the small
single-person projects - Software engineering is the study of processes by
which large projects can be developed
3Problem solving process
- We want to build good problem solving skills for
software development - This means skills which can successfully be used
to tackle larger and more complex problems - The design/documentation techniques may seem like
overkill for small problems, THE PRACTICE IS
IMPORTANT, AND VASTLY WORTHWHILE LATER ON - Planning, organisation, and documentation prevent
a great deal of grief in program development and
maintenance
4A typical Software life cycle
- Analysis - specifying exactly what the problem is
and what are the requirements for an acceptable
solution - Design - developing a detailed solution to the
problem - Coding - turning the design into a working
program - Testing/verification - ensuring the software is
correct - Maintenance - fixing, enhancing, adapting the
software - Obsolescence - abandoning a program which is no
longer effectively maintainable
5Program development
- Analysing/defining the problem we're given
- Developing a solution technique (algorithm)
- Documenting the program/technique
- Translating (implementing) the technique into
code - Compiling and running the program
- Testing the results with appropriate data
6Design and analysis
- Analysis includes
- determining the input data for the program
- determining the output that must be produced
- identifying any information needed to get from
the inputs to the output - Design consists primarily of working out an
algorithm - that is, a step by step procedure for solving the
problem (based on the analysis)
7Structured Programming
- Top-down development
- Modular design
- The Structure Theorem
8Top-down design
- Take a problem, divide it into smaller logical
sub-problems, and remember how they fit together - For each sub-problem, divide it into still
smaller sub-problems - Continue sub-dividing until each little problem
is small enough to be easily solved - Solving a collection of small problems thus
allows us to solve one much larger problem
Modular Design
9The Structure Theorem
- 3 control structures (Bohm and Jacopini)
- Sequence structure statements executed
sequentially by default - Transfer of Control
- Selection if-Then-Else
- Repetition do/while, Repeat/Until
10Algorithms
- Computing problems
- Solved by executing a series of actions in a
specific order - Algorithm is a procedure determining
- Actions to be executed
- Order to be executed
- Example recipe
- Program control
- Specifies the order in which statements are
executed
11Algorithms
- Must be
- Precise and unambiguous
- Give the correct solution in all cases
- Eventually end
- Example an algorithm for making coffee
- Stumble to kitchen
- Get coffee beans
- Get Coffee Grinder
- Grind.
-
- Drink your coffee!
12Pseudocode
- Artificial, informal language used to develop
algorithms that is similar to everyday English - Not executed on computers but is used to think
out program before coding and Easy to convert
into programs
- 6 basic computer operations
- Receive information Read, Get, ..
- Put out information display, Print, ..
- Perform arithmetic
- Assign value to a variable or memory location
- Compare two variable and select one of two
alternative actions If/Else - Repeat a group pf actions Do/While
- 3 basic control structures
- Sequence
- Selection
- Repetition
- 3 steps
- Define problem
- Design solution algorithm
- Check solution
13Pseudocode
- Suppose a program is required to read 2 numbers,
add them and print their total.
Defining Diagram
Algorithm
Algorithm to add 2 numbers and print their
sum Begin Input the first number Input the
second number Add the two numbers Output the
sum End
14Pseudocode
- Suppose you want to determine which is the larger
of the two numbers (if any).
Begin Input Num1 from the keyboard Input the
Num2 from the keyboard If the Num1 is greater
than the Num2 print Num1 Else if the Num2 is
greater than the Num1 print Num2 Else print
a message stating that the numbers are equal
End
15Flowchart
- Graphical representation of an algorithm using
special-purpose symbols connected by arrows
(flowlines) - Parallelogram symbol
- Input/Output operations
- Diamond symbol - Decision
- Rectangle symbol Action/process
- Oval symbol
- Beginning or end of a program, or a section of
code (circles)
Read Num2
Num1 gt Num2
no
yes
Print Num1
Stop
16if Selection structure
- Choose among alternative courses of action
- Pseudocode statement to determine whether a
student has passed the course - If students grade is greater than or equal to 40
- Print Passed
- If the condition is true
- Passed is printed, control continues on to next
statement - If the condition is false
- Print statement ignored, control continues to
statement after the if - Indenting makes algorithm easier to read
17if Selection structure
- Flowchart of pseudocode statement
- If - Performs action if condition true
- if/else Performs Different actions if
conditions true or false - Example
- if students grade is greater than or equal to
40 print Passed - else
- print Failed
18while Repetition Structure
- Action repeated while some condition remains true
- Pseudocode
- while there are more items on my shopping list
- Purchase the next item and cross it off
the list - while loop repeated until condition becomes false
19Example
- A fabric store wants a program to convert
measurements in square metres to square yards - Define problem program data?
- Inputs the incoming information is the number of
square metres of fabric - Outputs the number of square yards which are
equivalent to the input number of square metres - How to get from inputs to outputs the extra
information we need (and can work out) is that 1
square metre 1.196 square yards. Thus the
output number is 1.196 times the input number
20Write an algorithm to Find the tallest person in
the class!
- Read number of people into the variable people
- Input the first height directly into the variable
tallest - Increment counter to 1
- WHILE counter is less than people DO
- Begin
- input a new height into the variable height
- IF height is greater than tallest
- replace tallest with height
- ENDIF
- Increment counter
- End
- Print the value of the largest
- (NB1 BEGIN and END of the algorithm is not
shown! - 2 The while condition becomes false when
counter is equal - to people)
21flowchart
END
22Solve this problem
BEGIN ALGORITHM //Let num1, num2 and num3 store
integer values READ num1, num2 INITIALISE
num3 0 WHILE num2 gt 0 DO BEGIN
num3 num3 num1 num2 num2 - 1
END PRINT num3END ALGORITHM
- The following algorithm reads in 2 numbers and
performs an operation on them. What is the
relationship between the 2 numbers read in and
the result printed? - Draw a flowchart.
23Skills needed in programming
- Attention to detail
- Stupidity
- Good memory
- Ability to abstract, think on different levels
- Further Reading http//www.eskimo.com/scs/cclass
/progintro/sx1.html