Sunteți pe pagina 1din 52

Contents:

Contents
Computer Problem Solving
Algorithm and Its Characteristics
Algorithm Writing Techniques
Algorithms for Simple Problems
Programming Techniques
Structured Programming
Functional Design
Object Oriented Design

By: User Interface Design Strategies

Jahangir Alam
When problems are solved with the help of
computers following steps are to be performed:
Problem & Requirements Analysis
Problem Partitioning
Algorithm Development
Coding
Testing

Algorithm development is the most time and


efforts consuming part of computer problem
solving. It takes around 80% time and efforts
of the whole process.
An algorithm consists of a set of explicit
and unambiguous finite steps which when
carried out for a given set of initial
conditions produce the corresponding
output and terminate in a finite time.

In simple words we can say that an


algorithm is a step by step procedure to
solve a problem of a particular type.
Definiteness:
Definiteness Steps in an algorithm must be
unambiguous and precise i.e. each step of
algorithm must reflect single meaning.
Finiteness:
Finiteness Each instruction must be such that
it can be performed in a finite time. Further
the algorithm must be terminated in a finite
number of steps.
Non-Intuitiveness:
Non-Intuitiveness Steps must be such that
one is able to execute them mechanically with
the help of paper and pencil.
Generality:
Generality It should be general so that it
is able to solve any problem of the type
for which it is designed.
Input/ Output:
Output It should have precise
inputs (initial data) and outputs generated
in intermediate steps and in the final step.
Algorithms may be good or bad. We have
already discussed the characteristics of
good algorithms
Steps in an algorithm may be:
Input (Initial Data)
Assignment
Decision
Repetition or Iteration
Output (Intermediate or Final)
Algorithm Writing Techniques:
Techniques
There is no hard and fast technique to write
algorithms.
It may be written using simple English language or
PSEUDO Codes.
PSEUDO codes being more closer to programming
languages will be discussed here.
Answer:
Step 1: START
Step 2: Get the three numbers X,Y,Z
Step 3: SUM <- X + Y + Z
Step 4: AVG <- SUM/3.0
Step 5: PROD <- X*Y*Z
Step 6: WRITE SUM, AVG, PROD
Step 7: STOP
Answer:
Step 1: START
Step 2: Get the two numbers X,Y
Step 3: IF X>Y THEN BIG <- X ELSE BIG <- Y
Step 6: WRITE BIG
Step 7: STOP
Answer:
Step 1: START
Step 2: Get the three numbers X,Y,Z
Step 3: IF X>Y THEN GO TO STEP 4 ELSE GO TO STEP 6
Step 4: IF X>Z THEN WRITE X IS BIGGEST
Step 5: EXIT
Step 6: IF Z>Y THEN WRITE Z IS BIGGEST ELSE WRITE Y IS
BIGGEST
Step 7: STOP
Answer:
Step 1: START
Step 2: Get the number X
Step 3: IF X < 0 THEN WRITE NUMBER IS LESS
THAN ZERO ELSE WRITE NUMBER IS
GREATER THAN ZERO
Step 4: STOP
Answer:
Step 1: START
Step 2: Get How Many Numbers N
Step 3:SUM <- 0
Step 4: I <- 1
Step 5: SUM <- SUM + I
Step 6: I <- I+1
Step 7: IF I <= N THEN GO TO STEP 5
Step 8: WRITE SUM
Step 9: STOP
Answer:
Step 1: START
Step 2: Get How Many Numbers N
Step 3: Get First Number X
Step 4: BIG <- X
Step 5: I <- 1
Step 6: Get Next Number X
Step 7: IF X > BIG THEN BIG <- X
Step 8: I <- I + 1
Step 9: IF I < N THEN GO TO 6
Step 10: WRITE BIG
Step 11: STOP
No hard and fast rule for writing
algorithms.
When they are written using natural
languages they lead to ambiguity as
natural language statements are
ambiguous in nature.
To overcome these drawbacks flow charts
were developed to express algorithms.
It is an excellent vehicle for man-to-man
communication to explain the steps of an
algorithm.
Flow chart is a graphical or pictorial
representation of specific sequence of steps
of an algorithm.
It is a diagram of shaped boxes connected by
directed line segments. Each box represents
a group of elementary steps of algorithm.
Each directed line shows the flow of
algorithm.
START/
DECISION
STOP

In a hexagon
a group of
READ/ steps or sub-
WRITE algorithm can
be indicated

Flow Chart
Continuation
ASSIGNMENT Symbol
Step 1: START
START

Step
2: Get the two READ X, Y

numbers, X,Y
Step 3: TEMP <- X TEMP <- X
X <- Y

Step 4: X <- Y
Y <- TEMP

Step 5: Y <- TEMP WRITE X, Y

Step 6: WRITE X,Y


STOP

Step 7: STOP
START

Step 1: START
Step 2: Get the number,
X
READ X

Step 3: IF X < 0 THEN


Y

X <- -X
X<0

Step 4: WRITE X N

X <- -X

Step 5: STOP WRITE X

STOP
Step 1: START
Step 2: Get How Many Numbers, N
Step 3: Initialize I and Flag
Step 4: I <- 0
FLAG <- 0
Step 4: Get Number to be Searched, TARGET
Step 5: Get the next number, X
Step 6: IF (X=TARGET) THEN FLAG=1 : GO TO 9
Step 7: I <- I+1
Step 8: IF I<=N THEN GO TO 5
Step 9: IF (FLAG =1) WRITE FOUND ELSE WRITE NOT FOUND
Step 10: STOP
Step 1: START
Step 2: Get the number, NUM

Step 3: Calculate Remainder

Step 4: REM <- NUM INT(NUM/10)*10

Step 5: WRITE REM

Step 6: NUM <- INT(NUM/10)

Step 7: IF NUM< > 0 THEN GO TO 4

Step 8: STOP
Step 1: START
Step 2: Get the number, NUM
Step 3: Initialize SUM
SUM <- 0
Step 4: Calculate Remainder
REM <- NUM INT(NUM/10)*10
Step 5: SUM <- SUM + REM
Step 6: NUM <- INT(NUM/10)
Step 7: IF NUM< > 0 THEN GO TO 4
Step 8: WRITE SUM
Step 9: STOP
Step 1: START
Step 2: Get the number and base, NUM,R

Step 3: Calculate Remainder when NUM is divided


by R
REM <- NUM INT(NUM/R)*R
Step 4: WRITE REM

Step 5: NUM <- INT(NUM/R)

Step 6: IF NUM< > 0 THEN GO TO 3

Step 7: STOP
Step 1: START
Step 2: Get How Many Numbers, N
Step 3: I <- 1
Step 4: Get Next No, X
Step 5: IF X MOD 3 = 0 THEN WRITE X
Step 6: I <- I+1
Step 7: IF I <= N THEN GO TO 4
Step 8: STOP
Following three conditional operators may be used in Algorithms:
AND
OR
NOT

a b a AND b a b a OR b a NOT a
F F F T F
F F F
F T F F T T
F T
T F F T F T

T T T T T T
Step 1: START
Step 2: Get the Number, X
Step
3: IF X MOD 5 = 0 OR X MOD 11 = 0
THEN WRITE YES ELSE WRITE NO
Step 4: STOP

*FLOWCHART has been left as an exercise.


Step 1: START
Step 2: Get How many numbers and constants, N,p,q
Step 3: I <- 1
Step 4: Get Next Number, X
Step 5: IF X MOD p = 0 AND X MOD q = 0 THEN WRITE
X
Step 6: I <- I+1
Step 7: IF I <= N THEN GO TO 4
Step 8: STOP
Finite Series:
1. 2+4+6+8+..N terms
2. 3+5+7+9+N terms
3. 1+(1+2)+(1+2+3)+(1+2+3+4)+.N terms
Infinite Series (Desired Accuracy is .0001):
4. 1+x+x2+x3+.
5. Series of Cos(x)
6. Series of Sin(x)
7. Exponential Series (ex)
8. Logarithmic Series (logex)
9. ax
*Try to Implement all the above algorithms in C.
Structured Programming (SP):
There is no standard definition of structured
programs. A definition of programming may be
Designing, Writing and Testing of a Program. By
the word structured we could say, structured
programming is Designing, Writing and testing of a
program in a prescribed pattern of Organization.
In SP our attention is directed to Form and
Organize.
In SP a programmer follows some basic logic
structures when developing programs.
However, there is no standard definition available
for structured programs but it is often thought to
be programming without the use of a GO TO
statement.
According to Edsger Dijkstra GO TO statement
is harmful for programs. Thus in that manner
structured programming is merely go-to less
programming.
The goal of structured programming is to linearize
the control flow through a program so that the
execution follows the sequence in which algorithm
is written. This enhances readability of code which
eases understanding, debugging, testing,
documentation and modification of programs.
By linearizing the flow of control Structured
Programming tries to avoid the bad programming
structures.
Linear flow of control can be achieved by restricting
the set of allowed program constructs to single entry,
single exit formats.
What are Single Entry and Single Exit Formats? :
According to Barry Bohem sequencing, selection
among alternatives and iteration is a sufficient set of
constructs for describing the control flow of every
conceivable algorithm.
A sufficient set of single entry, single exit constructs
for specifying control flow in an algorithm is:
Sequencing: S1; S2; S3;
Selection: IF B THEN S1 ELSE S2;
Iteration: While B Do S;
Structured programs can be written by using the following
three concepts:
i. Top Down Analysis for Problem Solving.
ii. Modularization for Program Structure and
Organization.
iii. Treatment of GO TO Statements in a disciplined
manner.
Top Down Analysis for Problem Solving:
The activities for performing the top-down analysis of
a problem can be grouped in the following four steps:
1. Define the complete scope of problem to
determine the basic requirements for its
solution. Three factors must be considered in the
definition of a programming problem:
Input: What data will need to be processed by the
program?
Processing: What must be done on Input Data?
Output: What results the program should produce? In
what form they must be presented?
2. Based on the definition of the problem divide it
into two or more parts.
3. Carefully define the scope of each of these
separate tasks and subdivide them if necessary
into two or more further smaller tasks
4. Repeat Step 3 until every step at the lowest
level describes a simple task which cant be
broken further.
Modularization for Program Structure & Organization:
A module is a logically separable part of a
program. It is a program unit that is discrete and
identifiable with respect to compiling and loading.
In terms of a common programming language
constructs a module can be a macro, a function or
a procedure. Partitioning a system into modules is
useful only when the modules are solvable and
modifiable separately. A system or program is
considered to be modular if it consists of
discrete components such that each component
has minimal impact on other components.
Clearly modularity is a desirable property when
developing programs for large tasks identified by
top-down analysis of problem.
A program cant be made modular by simply
breaking it into discrete modules. Some criteria
must be used to select modules so that the
modules are solvable and modifiable separately.
Coupling and Cohesion are two such modularization
criteria.
Coupling means how tightly the modules of a
program are connected to each other and Cohesion
means how tightly internal elements of a module
are connected (bounded) to each other.
While designing modules we should strive for
high cohesion.
Treatment of GO TO Statements:
The GO TO Statement provides unconditional
transfer of control and thus allows the violation
of single entry and single exit condition of SP.
In some situations and particularly in some
programming languages it is almost impossible to
avoid the GO TOs. Whenever mandatory GO
TOs should be used but with following things in
mind:
Try to replace GO TOs with if-else statements.
GO TOs as error handlers and conditional exit from a
loop are acceptable.
There are two strategies known for program design:
Functional Design or Function Oriented Design.
Object Oriented Design.
Functional Design:
The program is designed from the functional point
of view.
Each module represents a function or an operation
to perform an activity in the system.
In functional approach a S/W consists of some data
items that represent some information and a set of
procedures which manipulate data.
There is a clear separation of data from
procedures in the functional approach. This
approach concentrates upon the actions (tasks)
to perform and the data upon which the actions
are performed play a secondary role during
program design.
In functional approach data may be intentionally
or unintentionally modified by the functions
other than required to modify the data.
Hence the functions have exclusive control over
the data and limited access to the data cant be
achieved using this technology.
The program is viewed as a collection of objects
rather than functions.
In Object Oriented approach a module is an object
which is not a functional entity that represents an
operation but is an entity that represents some
data items in the system together with the
operations that can be performed on the data.
The Object Oriented view is that the data and
procedures are not treated as independent of each
other and are combined together in the form of
what are known as Objects i.e. a sort of meaningful
entity.
Neither the data nor the operations on the data can
be separated from the Object. Hence data items
are treated as integral parts of the program design.
The decomposition of a program into modules is
based upon objects since during program design
objects are considered as the basic units in which a
program can be decomposed.
The functions of an Object Unit operates on the
Objects data only and are called member functions.
An Object itself is a subset of a more general entity
called Class. A Class is a general name given to
represent few things having some common
attributes. An Object is an instant of a class.
For Example Class of four wheelers represents
buses, trucks, cars.. etc. Each of these objects
has common attribute (Four Wheels) with other
attributes which distinguish it from others.
The basic philosophy of using objects in program
design is simple The World is composed of
classifiable and identifiable objects interacting
with each other by passing messages. Therefore
programs too can be structured that way.
Characteristics of OOPs include Data Hiding and
Encapsulation, Inheritance, Polymorphism etc.
User Interface means how a program or a large
Software interacts with the users. The User
Interface of a system is often the scale by which
that system is judged.
A User Interface which is difficult to use will at best
result in a high level of users errors. At worst it will
cause the Software System to be discarded
irrespective of its faults and error free functionality.
If information is presented in confusing manner the
user can misunderstand. He can initiate a sequence of
events which corrupts the data or even causes system
failure.
So a Software System must provide a user
friendly and strong User Interface. Two types
of user interfaces are available in Computer
Industry:
Textual Interface
Command Line Systems
Menu Based Systems
Graphical User Interface (GUI)
Textual User Interfaces:
a) Command Line Interfaces:
Require some commands to be typed by the
user to initiate some services of the Software.
Users of some operating systems e.g. DOS and
UNIX work with command line interfaces.
All commands must be known to the user. Wrong
commands entered by the user are discarded by the
machine.
b) Menu Based Systems:
Users select one of the number of options to issue a
command or to initiate an activity.
A very good example is Turbo C++ Compiler.
Various types of menus are:
Pull Down Menus
Popup Menus
Walking Menus.
User selects the option(s) from number of
graphical objects displayed on the screen. A
GUI is characterized by following general items:
a) Pointing Devices:
Allow users to point at different parts on the
screen.
Selecting Objects on the Screen.
Moving Objects on the Screen.
Drag Drop Operations.
b) Windows:
Multiple windows allow different information to be
displayed on the screen simultaneously.
c) Icons:
Are the pictures or objects that represent different types
of information.
d) Menus:
Commands are selected from a menu rather than typing them
in a command language.
e) Dialogue Boxes:
Are used to collect information from the users or to display
information to the users.
f) Desktop Metaphor:
Instead of initiating actual program, user can initiate any
application built by using that program.
The application will automatically invoke the program. For e.g.
a word file when opened with windows explorer automatically
invokes MS-Word.
Textual Graphical
Are not easy to use and to learn then Easy to learn and use. Users with no
a brief training period is required for computing experience can easily learn
the users. the interface after a short training
period.
Only one task can be carried out in an User has multiple screens to interact
interactive manner. However, multiple with the system. Switching from
tasks can be carried out in non- current task to another task is
interactive manner if the OS is possible without losing sight of
multitasking information generated during current
task.
Rare use of pointing devices Very difficult to use without pointing
devices.
Several commands are required to Fast full screen interaction is possible
remember to work efficiently with immediate access to anywhere in
the screen.
Examples are DOS, UNIX/ LINUX Examples are Macintosh (Apple),
with X-Windows, Novell Netware 3.12 UNIX/ LINUX with X-Windows
etc. System, Windows OSs etc.

S-ar putea să vă placă și