Mod 1.

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Algorithms:

What is an Algorithm​:
- An algorithm is a step by step procedure for solving a problem.
- It is a description of the processing steps necessary to transform inputs into outputs
infinite time.
- E.g a recipe
- Think of an algorithm as a plan for a piece of software.

Standard methods for writing algorithms are


- Flowcharts
- Pseudocode
These methods are a language used to develop algorithms. They allow others to understand the
algorithms we write.

Pseudocode:
English like sentences describe each step in the algorithm
Eg. BEGIN Add2Numbers
Get firstNumber
Get secondNumber
total=firstNumber + secondNumber
Display “The sum of your two numbers is” total
END Add2Numbers

Pseudocode has rules:


Keyword pairs- indicate the start and end of control structures, e.g.
BEGIN MAINPROGRAM/ END MAINPROGRAM
WHILE/ENDWHILE
Uppercase for keywords

P.
Pseudocode example
BEGIN MAINPROGRAM
Total = random_number between 1 and 100
loop flag = true
WHILE​ loopflag
Print out total
Number = user input
Total = total - number
IF​ total <=0 ​THEN
loopflag = false
ENDIF
ENDWHILE
Print “Game Over”
End Main program

Flowcharts:
- A graphical method of representing an algorithm.
- Flow from top to the bottom of the page
- Each flowchart should be about one page each

Process

Start/End

Decision
Output/Input

- Algorithms are converted into software using a programming language.


- Because the top/down design process is often used, developing algorithms using
subprograms is strongly advocated.

Basic control structures

In theory, all problems can be solved using just the four control structures.

-Sequence​:​ One instruction followed by another etc


- A series of instructions in a specific order
- e.g of Sequence pseudocode
One instruction followed by another, a series of instructions in a specific order
The decision is in the form of a condition. A different oath is taken depending on whether the
condition is true or false
Iterating or looping blocks of code.there is a pre and post test loop. The code will iterate as long
as the condition holds true.
BEGIN MAIN PROGRAMME​ (MakeTea)
……………….
Process 1 (Fill the kettle with water)
Process 2 (Boil the water in the kettle)
Process 3 (Place tea leaves in the pot)
Process 4 (Pour boiling water into pot)
Process 5 (Pour tea into cup)
……………….
END MAIN PROGRAMME

eg
Create a pseudocode and flowchart algorithm that prompts a user to enter two numbers Num1
and Num2, displays them, swap their values, and displays them.
BEGIN MAIN PROGRAM​ NumberSwap
Get Num1
Get Num2
Display Num1 and Num2
Temp=num2
Num2=Num1
Temp=Num1
Display Num1 and Num2
END MAIN PROGRAM​ NumberSwap

Meant to be slanted cause it output


- Selection:​ a decision
- THe decision is in the form of a condition
- A different path is executed depending on the truth or falsity of a condition
- When the processes along a path are completed processing continues at the first
statement past the selection statement.
…………
IF condition THEN

Process 1
Process 2
……….
ENDIF
More instruction
……………….

Involves comparing two or more operands (variables, value)

Age​ ​>=​ ​16

__​ = Operand
__​ = Relational operand

= Equal to
≠ Not equal to
<Less than
<​Less than or equal to
>Greater than
>​Greater than or equal to

EG.
BEGINMAINPROGRAM ​RideChecker
Get Age from user
If (Age​<​16 NO ride)
If (Age >16 Welcome to ghost train)
Print Thankyou message
END MAINPROGRAM
Complex Conditions
● These are constructed using logical operators
○ AND (All conditions need to be true)
○ OR (Either of the conditions are true)
○ NOT (Neither conditions are true)
● EXAMPLE
○ If (Age ​< ​16) AND (Height >120 cm) THEN

● EXAMPLE
○ eof=False
○ IF NOT eof THEN………………

IF (cohort = “Yr11”) THEN


Print “”
ELSE
Print “Loser”

Multiway Selection:
An alternative to using multiple binary selection statements
More than two alternative parts
The first choice that equates to true.
Basically IF with Elif and else.

Example of pseudocode
……………………………..
CASEWHERE Expression is
Choice A: Process 1
Choice B: Process 2
………………………………..
OTHERWISE: Default Process
ENDCASE

Example of Multiway Selection


BEGIN MAINPROGRAM​ TrafficLights
Read traffic_light
CASEWHERE traffic_light is
Red: Stop the vehicle
Amber: Stop if safe to do so
Green: Keep going
OTHERWISE Slow and proceed cautiously
ENDCASE
END MAINPROGRAM

a. Flowchart algorithm:

b) ​BEGIN MAINPROGRAM​ TrafficLights


Read traffic_light
IF (traffic_light = red) THEN Stop the vehicle
IF (traffic_light = amber) THEN Stop if safe to do so
IF (traffic_light = green) THEN Keep going
ELSE Slow and proceed cautiously
ENDCASE
END MAIN PROGRAM

​ EGIN MAINPROGRAM​ TrafficLights


B
Read traffic_light
IF (traffic_light = red) THEN
Stop the vehicle
ELSE:
IF (traffic_light = amber) THEN
Stop if safe to do so
ELSE:
IF (traffic_light = green) THEN
Keep going
ELSE:
Slow and proceed cautiously
ENDIF
ENDIF
ENDIF

ENDCASE
END MAIN PROGRAM
- Repetition​:​ repeating blocks of code
- iterating or looping code.
- repeating a sequence of steps.
Two types of loops use a condition
- Pre test loop- the condition is laced at the start of the loop.
- Post test loop, the condition is placed at the end of the loop.
The process within the loop structure are known as the body of the loop
The loop body is repeated while ever he condition holds true.

The pretest loop (guarded loop)


● The condition is before the body of the loop
● Repetition of the loop body continues while the condition remains true
● When the condition is false the loop ends
● Pseudocode uses the keyword WHILE and ENDWHILE

BEGIN MAINPROGRAM​ PencilSharpen


Read pencil_sharpness
IF NOT sharp THEN
Insert into sharpener
WHILE pencil not sharp DO
Turn pencil in sharpener
Check pencil sharpness

ENDWHILE
ENDIF
END MAIN PROGRAM

Post test loop(unguarded loops)


- The condition is at the end of the loop, after the body
- Process in the loop execute at least once.
- Processing in the loop body continues while the condition is false
- Keywords: ​Repeat, Until
- As soon as the condition becomes true, the loop terminates and the execution of the first
statement following the loop occurs.

Pseudocode:

………………
REPEAT:
Process1
Process2
…………….
Processn
UNTIL ​condition
Process
…………………

Repetition:
The body of the loop is to be repeated a fixed number of times, OR
Repetition is to occur while a sentry value increments between within the limits of two values
E.g. We wish a variable to take the values 1, 2 ,3, 4...100

Increments:
It is possible to alter the magnitude of each increment (or step)
Example sequences:
2,4,6,8
10 ,9 ,8, 7...
OR -1, -0.5, 0, 0.5, 1, 1.5, 2

Keywords:
FOR……..(STEP).......NEXT
FOR……..(STEP)......ENDFOR

Pseudocode syntax
FOR ​loopcounter = initial value​ TO​ final value ​BY​ stepvalue
Process
Process
………………………..
ENDFOR

FOR ​loopcounter = initial value ​TO​ final value ​BY​ stepvalue


Process
Process
………………………..
NEXT ​loopcounter

Flowcharts:
These are just while loops

Tasks:
Develop a pseudocode algorithm that displays the first ten multiples of 3
Develop a flowchart algorithm which outputs the following:
-100, -95, -90, -85, …, 0
PSEUDOCODE
BEGIN MAINPROGRAM BY 3
FOR loopcounter = 3 TO 30 BY 3
Display the value loopcounter
………………………………….
NEXT loopcounter

Subprograms:
Aka subroutines or modjules
Self contained fragments of algorithm code
They are incorporated into a admin algorithm
Top/down designs
● Subprograms are a consequence of developing solutions using the top down design
approach
● The highest level of algorithm code is known as the main program or mainline

Subprograms in pseudocode
● In the mainline a call to a subprogram is indicated by the underlined name of the
subprogram
● To call a subprogram means to transfer execution to the subprogram

Pseudocode Keywords
● BEGIN SUBPROGRAM
● END SUBPROGRAM

Modularity:
- Many subprograms are stored separately
- Usually in a code library (sometimes called dynamic link library)
- The subprograms are called modules
- Modularity refers to the process of creating reusable code modules
- A module can contain one or many subprograms
- Subprograms can be stored in a code module have to have complete, self contained
blocks of code, independent of the calling code.
Writing Software:
● First step - write the mainline:
○ Include calls to required code modules
○ Try to avoid too many selections or repetitions in the mainline
○ Include calls to code modules where necessary
○ Stubs are written in place of the complete modules
○ Stubs are empty subprograms or perform simple commands like a print
statement are least to begin with
○ Code for the modules are developed separately
○ When the module is complete it replaces the stub
Parameters:
● Provide data to the sub programs (modules)
● Return data to the mainline
● Are an interface between the module of code and the mainline
● Data is passed via parameters to the subroutines during calling
● A key factor that enables reuse of code module

Modularity
● A software module*(subprogram) can
○ Call other lower level modules and
○ Pass values to the lower level modules using parameters
● The order of the parameters is important
○ It determines how they are mapped to the formal parameters in the subroutine
● We should aim to develop software with
- a clear uncluttered mainline
- Independent subprograms
As long as a subprogram is independent it can be reused in other applications or the same
program.

Pseudocode:
BEGIN SUBPROGRAM​ DigitalRoot
Input num value
WHILE num>=10:
Set num=SumDigits(num)
ENDWHILE
Display num
END SUBPROGRAM

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Ergonomics:
- The study of the relationship between people and their work environment
- Our emphasis - ergonomics & software design

Aims:
To create a work environment/experience that caters to:
- Physical
- Emotional and
- Psychological needs
Ergonomic are highly productive
- Produce better quality products
- Better morale
Types:
Repetitive Strain Injury
- Cause: repetition of the same movement
- Can occur in any part of the body
- Computer users - mainly the wrist, arm, hand and fingers

Carpal tunnel syndrome - repeated movement of the wrist


- Carpal tunnel - within the bones of the wrist
- Transverse carpal ligament
- Median nerve - controls the thumb, index and long fingers
- Broken bones or arthritis

Tenosynovitis - inflammation of the tendon sheath


- Overuse of the tendons running through the carpal tunnel
- Causes swelling of the lubricating sheath (surrounds each tendon)
- The carpal tunnel is unable to expand, pressure of the median nerve, numbness of the
hand.

Treatments
● Wrist and hand braces
○ Wrist kept in a straight position
○ Allows ten
○ don to move freely
● Anti inflammatory drugs eg Aspirin, Cortisone injection into the carpal tunnel
● Surgery (extreme cases)
○ Cut the transverse carpal ligament
○ Larger carpal tunnel
● Workplaces with poor ergonomic practices
○ Increased costs =>workers compensation
○ Lower/lost productivity
○ Staff absenteeism

To avoid injury:
- Variety: Do different things and avoid repetitive tasks
- Autonomy: Allow people to help make the decisions. Employee decides the order of task
completion
- Identity: Tasks should be complete jobs and provide a feeling of accomplishment
- Feedback: Effective communication between workers and management
- Social contact: With co workers
- Achievement: Employees feel contribution is valued
- Training: formal and informal and must be on-going
- Job Demand: too much/little work causes stress. Must be a balance

Ergonomic Use of equipment:

● Standards developed for makers of ergonomic products


● Visual display units
○ Workstation furniture
○ Input Devices
● Essentially all should be adjustable
● If desk is not height adjustable use vertical footrest.
Chairs:
- Five legged base
- Breathable fabric
- Dense foam: supportive, returns to original shape
- Armrests: height adjustable

Monitor:
- Height adjustable
- Larger the monitor use highest resolution
- Should be about an arms rest away
- Characters should be easily readable from an arms length away.
- Glare (artificial or natural light) can cause irritation. Monitors should be perpendicular to
light sources. Screen filters.

Keyboards:
- Detachable
- Forearms parallel to the floor
- Dished keys
- Matt finish to reduce glare
- Numeric keypad for extensive numeric entry.

Mouse:
- Fits the hand
- Same Level as the keyboard
- Use an armrest
- Rest the finger on the button ( not “held” above the button)
- Straight wrist
- Mid range speed
- Dragging kept to minimum
- New designs - use the thumb to operate the primary button

Copyholders:
- Users who perform input from paper based documents
- Eliminate the need to move the neck
- Positioned close to screen at similar distance

Lighting:
- Uniform and bright
- Position monitor perpendicular to windows
- Neutral wall colour
- remove/cover shiny objects
- Diffusers on overhead lights
- Desk lamps

Noise:
- Less than 55 db
- Sound absorbing partitions
- Impact printers
- Located away from work areas
- Contained within soundproof shields

Conditions:
- Air temp: 20-23
- Relative humidity: 30-70%

Ex 1.1 Q 2 and 4

Ergonomics Homework

Question 2: What factors of an office environment can affect the health of a worker? Describe
the way in which each of these factors affect the workers health.

Question 4: Report on the ergonomics of your school computer rooms. What improvements
would you make if you were allowed an unlimited budget?

Ergonomics in Software:
Software must enhance user experience
Critically evaluate user interfaces. They may not be perfect.
User interface should be intuitive. Used with minimal training.

Design tips for user interface


● Users transfer skills from between and within other applications
● Reinforces a mental model of the software.
● Buttons - consistent places on all screens
● Labels and messages - consistent wording
● Consistent colour scheme
Consistency
● Set standards for interface design
● Initially based upon industry standards
● Application specific standards are then developed
● Operating system (O.S) developers
○ Design standards
○ Often define application standards

● Use interface elements correctly


● When & how to correctly use screen elements
○ Command buttons
○ Check boxes
○ Radio buttons
○ List boxes
○ Text boxes
○ Menus
● Screen elements - perform as expected
● Use colours sparingly and have sufficient contrast between text and background
● Use fonts sparingly, make them easy to read. Have a max of 3.
● Alignment of data
● Editing field & adjacent labels - left justified
● Columns of data
○ Integer - right justify
○ Decimals - decimal point
○ Text - Left justify
Appropriate user messages
● Primary method to communicate problems
● Poorly worded messages, user perceives a poor product
● Messages should imply that the user is in control. Full words and sentences. Avoid
abbreviations and code.
● Error messages provide a visual clue of the severity of the error
● Messages can prompt the user.

Ease of use
● Good user interface should be easy to use
● Group together logically connected screen elements
● Keep unrelated screen elements separate
● White space
● Frames
● Application should support a variety of skill levels, novice and expert.
● Short cuts and advanced functionality should be available
● Functionality should not be hidden from the user.
Software that’s accessed by communication links/internet are vulnerable to response time lag
It is vital that developers include methods to ensure their products perform satisfactory under
adverse conditions.

BEGIN MAINPROGRAM​ PrimeNumber


Input number
IF number = 2 or 3 or 5 or 7 THEN
Print “Your number is prime”
ELSE
IF number modulus 2 = 0 or number modulus 3 = 0 or number modulus 5 = 0 or
number modulus 7 = 0 THEN
Print “Your number is not prime”
ELSE
Print “Your number is prime”
END IF
END IF

You might also like