Sie sind auf Seite 1von 11

3.

0 Programme Design Approach and Design Techniques

Introduction

Programmes are written to solve specific problems, as for example to process


the payroll, to provide management support, to handle data, to play game, to
show a movie, etc. Before writing a programme, one should follow a series of
steps that will help to understand the problem, explore different ways of solving
them, selecting the most appropriate solution and describe the solution in terms
of suitable structures to represent the data and algorithms to process the data.
In this lesson we will learn how to solve problems using computers.

Learning outcomes
• On completing this lesson, you would be able to :
o Explain the use of different programme design approaches
o Apply design techniques.

3.1 Programme Design Approaches

Usually, solutions to small problems can be easily described, but many problems
we need to solve with the use of a computer will appear to be very large and
complex. In fact, many of the problems are large and complex. It is up to us to
find solutions to these large, complex problems.
We can use many different tools and methodologies to achieve this. They are
centered on breaking down the problem into more manageable parts.

3.1.1 Top Down Design

In this approach, we try to break down the problem into components. Each of
these components is then broken down into smaller components, and this
process is continued until components that can be easily solved are reached
through algorithms and implemented in some programming language. However,

Fundamentals of Computer Programming 1/11


the process is not dependent on any particular programming language. To
understand the Top-down problem solving process let us take organizing a batch
party
The problem statement is:

To organize a batch
party
Now let us identify different sub problems of this problem:
To organize a batch
party

Organize Venue Organize Food


Organize Food Organize Music Invite People
Invite People

Now we further break down the sub-problems in to smaller problem statements.


To organize a
batch party

Organize Venue Organize Organize Invite


Food Music People

Find Find pricing Evaluate Prepare Find Print and


suitable and other and Select Invitee List Addresses Mail letters
locations data

This process will go on until we have things that we can easily do, such as
preparing the invitee list. On the other hand, you may have to break some of
these sub-problems into much smaller tasks. For example, print and mail letters
may be broken down into tasks such as drafting the letter, typing the letter,
proof reading the letter, preparing the invitee database, mail merging and
posting.

Fundamentals of Computer Programming 2/11


3.1.2 Step-wise refinement

While Top down design provides us with a hierarchy chart that shows the inter-
relationship between different parts of the solution, we need some mechanism
to describe the solution. A process closely associated with top-down design is
known as step-wise refinement. In step-wise refinement, we keep on refining
the solution in each step until we reach a solution that can be easily
implemented in a programming language.

3.2 Programme Design Techniques

You might be tempted to use a programming language to describe your solution,


however we will discourage you to give into this temptation just now. You can
use one of two methods to describe the solution process. Both methods do not
depend on any particular programming language. In this way, we will be able to
describe the solution and then use any programming language to implement it.
The two methods are flowcharts and Pseudo code. Flowcharts are a visual
language where fundamental structures are described in terms of different
geometric shapes while Pseudo-code is essentially an English-like structured
language.

Both methods rely on a few fundamental programming structures to describe


algorithms. These are shown in the following table (Table 3.1) with a brief
description.

Sequence Defines sequential statements that are executed one


after another. ASSIGNMENT, INPUT, OUTPUT
Selection Defines conditional statements that may be executed
depending on a certain condition being satisfied.
IF … THEN, IF… THEN … ELSE, SWITCH (CASE)
Iteration Defines statements that can be used to execute some
statements repeatedly.
FOR, WHILE, DO … WHILE, REPEAT … UNTIL

Fundamentals of Computer Programming 3/11


Sub-programs Defines statements that can be used to decompose a
problem into sub-problems. SUB PROGRAMME
(FUNCTION, PROCEDURE, SUB PROCEDURE, METHOD)

Table 3.1 – Fundamental programming structures

3.2.1 Flowcharts

In flowcharts, we use a graphical language to describe an algorithm. It contains


graphical symbols to represent input, output, processing, conditional logic, etc.
Flowcharts also allow us to apply successive refinement as we see in the
examples. Different authors may use different symbols to draw flowcharts, but
we will use the following notation (figure 3.1) in our programming tasks.

Data input and For I = 1 to 10 Iteration (For


output loop)

Routine
1 continued later
Decision
(in another
diagram)

Process Name Subroutine

Beginning of a START Name Beginning of a

START Programme Sub-Programme

End of a Sub
END End of a routine EXIT
program

Figure 3.1 - Some commonly used symbols to draw flow charts

Fundamentals of Computer Programming 4/11


Now let us take a look at some examples of algorithms.
Example 1: Add two numbers and print the result. Figure 3.2 depicts the
corresponding flowchart.

START

Read
number1,
number2

Result = number1 + number2;

Print Result

END
Figure 3.2 - Flowchart of adding two numbers and displaying the value

Activity 3.1
1. Draw a flow chart to calculate the interest earned by investing a certain sum at a bank at
a given interest rate.
2. Draw a flow chart to calculate the sum and the average of three numbers.
3. Draw a flow chart to read student’s name and marks for five subjects and display the
average mark.

Check your answers

Fundamentals of Computer Programming 5/11


3.2.2 Pseudo-Code

Pseudo-code is a semi-structured English-like language used to describe


algorithms and can be categorized into four main groups described previously,
according to their functionality. Like flow charting symbols, different authors
use different keywords in writing pseudo code. However, it is important that we
establish a set of guidelines so that all of us speak the same pseudo code
dialect. Some keywords in pseudo code are listed in the following (table 3.2).

Concept Keyword or symbol Example of use


ASSIGNMENT = Name = "Moratuwa"
INPUT Read Read name, age
OUTPUT Print Print "Name is ", name
If (condition) Then If (mark > 50) Then
statement Print "You have passed"
Endif Endif
If (condition) Then If (mark > 50) Then
Statement Print "You have passed"
Else Else
statement Print "You have failed"
SELECTION
Endif Endif
Switch (expression) Switch (position)
case value1: statement case 1 : Print "First"
case value2: statement case 2: print "Second"
default: statement case 3: print "Third"
End default: print "No place"
End
For counter = initial value For counter =1 to 10
to end value increment inc print counter
ITERATION
statement End
End

Fundamentals of Computer Programming 6/11


While (condition) While (counter <= 10)
Statement print counter
End counter = counter + 1
End
Do Do
Statement Print counter
While (condition) counter = counter +1
While (counter <= 10)

Table 3.2 – Key words in pseudo code dialect

Now let us look at some examples of Pseudo code:


Example 1: Add two numbers.
Begin
Read number1, number2
Result = number1 + number 2
Print "Result is ", Result
End
Example 2: Calculate the interest earned at the end of the year by investing a
certain sum at a bank at a given interest rate.

Begin
Read investment, interest_Rate
interest_Earned = investment * interest_Rate
Print "Interest Earned", interest_Earned
End
Example 3: Modify the algorithm in Example 2 to print the value of the
investments at the end of each year for 10 years, if the interest is
reinvested in the bank.
Begin
Read investment, interestRate, numberOfYears
For counter = 1 to numberOfYears increment 1
interestEarned = investment * interestRate
investment = investment + interestEarned
Print "Total Investment at the end of year",

Fundamentals of Computer Programming 7/11


counter, " is ", investment
Next Counter
End

These are only some simple algorithms. More complicated programmes would
use selection statements and iterative statements as well. When programmes
are too large, sub-programme statements can be used to decompose them into
more manageable chunks. Each sub-programme can be designed separately and
checked for correctness and then combined together to form a single solution.

Since the final use of any algorithm is to write a computer programme that can
be executed, we will now translate some of the algorithms that we have written
into java.

Example 1: Add two numbers.


Pseudo-code:
Begin
// Get the input values required
Read number1, number2
// Process the data
Result = number1 + number 2
// Produce the output
Print "Result is ", Result
End
Java Code:
public class AddTwo {
public static void main(String args[]) {

int number1 = 20; // Simulates reading the value


int number2 = 30; // Simulates reading the value
int result;
result = number1 + number2;
System.out.println("Result is " + result);

Fundamentals of Computer Programming 8/11


}
}

Some of the things that you need to note here are:


public class AddTwo {
public static void main(String args[]) {
ƒ The following two statements are added to the beginning of the programme
public class AddTwo {
public static void main(String args[]) {
In fact, you will need these two statements in most of the programmes you write.
You will only need to change the class of the name, which in this case is
"AddTwo". Also note the use of a Capital letter in String keyword.
ƒ The variables used in the pseudo code have no data type associated with
them, but the variables in the Java program has a type associated with it
(int).
ƒ The Java programme does not read the two numbers as required (for the sake
of simplicity), but assigns two values to the variables.
ƒ Instead of Begin and End to mark blocks java uses { and }.
Each Java statement ends with a semicolon (";"). Each { used in the
programme to begin a block must have a corresponding } to end the block.
ƒ In the Print statement, in order to print multiple values we use the
concatenation operator "+". What it does is to put two Strings together. In this
case the int type value result (in this example, 50) is first converted to a
String ("50") and then concatenated with the String "Result is", giving "Result is
50".
ƒ We have used indentation to make the programme read easier.

Fundamentals of Computer Programming 9/11


Example 2: Calculate the interest earned at the end of the year by investing a
certain sum at a bank at a given interest rate.
Pseudocode:
Begin
// Get the input values required
Read investment, interestRate
// Process the data
interestEarned = investment * interestRate
// Produce the output
Print "Interest Earned", interstEarned
End
Java Code:
public class InvestmentCalculator{
public static void main(String args[]) {
float investment = 50000; // Simulates reading the value
int interestRate = 5; // Simulates reading the value
float interestEarned;
interestEarned = investment * interestRate /100;
System.out.println("Interest Earned " + interestEarned);
}
}
Some of the things that you need to note here are:
ƒ In this example, the Java programme does not read the two numbers as
required (for the sake of simplicity), but assigns two values to the variables.
ƒ Instead of Begin and End to mark blocks, java uses { and }.
Each Java statement ends with a semicolon (";").
ƒ In the Print statement, in order to print multiple values we use the
concatenation operator "+". What it does is to put two Strings together. In this
case the int type value result (in this example, 50) is first converted to a
String ("50") and then concatenated with the String "Result is", giving "Result is
50".

Fundamentals of Computer Programming 10/11


3.3 Implementation and Analysis of Algorithms

Now one known how to design a programme and code it using pseudo code.
Actually pseudo code is an algorithm we are writing to accomplish a task. So it
should be a finite set of well defined instructions that would take the input to
the problem and produce the desired output (Figure 3.3).

INPUT Processing Instructions OUTPUT


(Algorithm)

Figure 3.3 – Task of Algorithms

An algorithm should possess certain important characteristics such as:


Precise: There should not be any ambiguity in the instruction
Robust: Would work for all possible scenarios
Executable: Every instruction in an algorithm should be executable (should be
able to carry out the instruction)
Terminate: Every algorithm must end or if it runs indefinitely must be
deliberately ended so
Efficient: Must work fast using minimum resources, efficient algorithms and
data structures

Summary
This section discussed how to design a computer programme, using a top-down
design. We also discussed about programme design techniques, such as
flowcharts and pseudo-codes. Finally we examined certain important
characteristics algorithms should have. This lesson is the last lesson in this
week.
In the next week’s sessions, we will discuss how to define data types and how
to declare variables.

Fundamentals of Computer Programming 11/11

Das könnte Ihnen auch gefallen