0% found this document useful (0 votes)
15 views10 pages

p2 Notes 3

1. The document discusses algorithm design and problem solving techniques including abstraction, decomposition, and stepwise refinement. 2. It also covers basic programming concepts like loops, logic statements, modules, and data types. Common loops include for, while, and repeat loops. 3. The document discusses data representation including numeric, character, string, boolean, and array data types. It provides examples of how to declare and initialize arrays in pseudocode.

Uploaded by

zujaj khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

p2 Notes 3

1. The document discusses algorithm design and problem solving techniques including abstraction, decomposition, and stepwise refinement. 2. It also covers basic programming concepts like loops, logic statements, modules, and data types. Common loops include for, while, and repeat loops. 3. The document discusses data representation including numeric, character, string, boolean, and array data types. It provides examples of how to declare and initialize arrays in pseudocode.

Uploaded by

zujaj khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

condition at end of statements


1. ALGORITHM DESIGN & PROBLEM-SOLVING As for selecting what loop to use, it is best to use
FOR loops when you know the number of iterations
Abstraction: filtering out and concentrating on the required, and a WHILE or REPEAT loop if you do not
relevant information in a problem; allowing a know the number of iterations required.
programmer to deal with complexity o Iterate over an array: FOR Loop
o Reading a file into a variable: WHILE Loop
Decomposition: breaking down problems into o Asking for user input: WHILE/REPEAT Loop
subproblems in order to understand a process o A loop that should execute n times: FOR Loop
more clearly; program modules, procedures and
functions all help the programmer to break down 1.3 Stepwise Refinement
large problems. Process of developing a modular design by splitting
a problem into smaller sub-tasks, which themselves
Algorithm: a solution to a problem expressed as a are repeatedly split into even smaller sub-tasks
sequence of steps until each is just one element of the final program.

1.1 Identifier Table 1.4 Program Modules


Identifier: name given to a variable in order to call it This refers to a modular program design
An identifier table depicts information about the Subroutines: self-contained section of code,
variable, e.g. performing a specific task; part of the main
Rules for naming identifiers: program
o Must be unique
o Spaces must not be used Procedures: performs a specific task, no value
o Must begin with a letter of the alphabet returned to part of code where called
o Consist only of a mixture of letters and digits and Functions: performs a specific task, returns a value
the underscore character ‘_’ to part of code where called
o Must not be a ‘reserved’ word – e.g. Print, If, etc.
1.5 Logic Statements
1.2 Basic Program Operations
Operator
Assignment: an instruction in a program that places
a value into a specified variable < Less than
Sequence: programming statements are executed >= Greater / equal to
consequently, as they appear in the program <= Less /equal to
= Equal
Selection: control structure in which there is a test
> Greater
to decide if certain instructions are executed <> Not equal
o IF selection: testing 2 possible outcomes
o CASE selection: testing more than 2 outcomes
Repetition/Iteration: control structure in which a 2 DATA REPRESENTATION
group statements is executed repeatedly 2.1 Data Types
o FOR loop: unconditional; executed a set no. of Integer:
times Positive or negative number; no fractional part
o WHILE loop: conditional; executed based on Held in pure binary for processing and storage
condition Some languages differentiate short/long integers
at start of statements (more bytes used to store long integers)
o REPEAT loop: conditional; executed based on Real:
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

Number that contains a decimal point ● ASCII values can take many forms:
Referred to as singles and doubles depending upon numbers, letters (capitals and lower case
number of bytes used to store are separate), punctuation, nonprinting
Character: A character is any letter, number, commands (enter, escape, F1)
punctuation or space Takes up a single unit of 2.3 Unicode
storage (usually a byte). ASCII allows few number of characters; good for
English
String: Unicode allows others too: Chinese, Greek, Arabic
● Combination of alphanumeric characters etc.
enclosed in “ ” Different types of Unicode:
● Each character stored in one byte using o UTF-8: compatible with ASCII, variable-width
ASCII code encoding expand to 16, 24, 32, 40, 42
● Each character stored in two bytes using o UTF-16: 16-bit, variable-width encoding can
Unicode expand to 32 bits
● Max length of a string limited by available o UTF-32: 32 bit, fixed-width encoding, each
memory. character exactly 32 bits
● Incorrect to store dates or numbers as
strings
● Phone no. must be stored as string else
initial 0 lost
Boolean:
Can store one of only two values; “True” or “False”
Stored in 1 byte: True = 11111111, False =
00000000
Date:
Dates are stored as a ‘serial’ number
Equates to the number of seconds since January Pseudocode:
1st, o 1-D Array: A[1:n]
1904 (thus they also contain the time) Initialize an array
Usually take 8 bytes of storage For Count <- 1 TO N
Displayed as dd/mm/yyyy or mm/dd/yyyy Array[count] <- 0
Array: NEXT count
Data structure consisting of a collection of Input in Array
elements For Count <- 1 TO N
Identified by at least one array index (or key) OUTPUT “Enter element” & count
File: INPUT Array[count]
Object that stores data, information, settings or NEXT count
commands Output from an Array
Can be opened, saved, deleted & moved For Count <- 1 TO N
Transferable across network connections OUTPUT Array[count]
NEXT count
2.2 ASCII Code
● Uses 1 byte to store a character
● 7 bits available to store data and 8th bit is a
check digit
● 2^7 = 128, therefore 128 different values
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

o 2-D Array: A[1:m, 1:n]


DECLARE List : ARRAY [row:row ,col:col] OF <data type>

2.5 Bubble Sort

Search in 2D array

● A FOR loop is set to stop the sort


● Setting a variable ‘sorted’ to be ‘true’ at the
beginning
● Another FOR loop is set up next in order to
search
● through the array
● An IF is used to see if the first number of the
array is greater than the second. If true:
o First number stored to variable
o Second number assigned as first number
o Stored variable assigned to second number
o Set ‘sorted’ to ‘false’ causing loop to start again
● The second FOR loop is count based thus
will stop after a specific number of times
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

● Goes through bigger FOR loop ∴ ‘sorted’ 2.7 File Handling


remains ‘true’ Files are needed to import contents (from a file)
● This exits the loop ∴ ending the program saved in secondary memory into the program, or to
save the output of a program (in a file) into
2.6 Linear Search secondary memory, so that it is available for future
A FOR loop goes through the array use
It compares item in question to those in list using Pseudocode:
an IF: Opening a file:
o If item matches with another then search is OPENFILE <filename> FOR READ/WRITE/APPEND
stopped Reading a line from a file: READFILE <filename>
o Also the location where it was found is returned Writing a line of text to the file:
o If not found it exits the FOR loop WRITEFILE <filename>, <string>
Then returns fact that item is not in the list. Closing a file: CLOSEFILE (FileName)
Testing for end of the file:
EOF()

2.8 Abstract Data Types (ADT)


An Abstract Data Type (ADT) is a collection of data
with associated operations. There are three types
of ADTs:

Stack: an ordered collection of items where the


addition of new items and removal of existing items
always takes place at the same end.

Queue: a linear structure which follows the First In


First Out (FIFO) mechanism. Items are added at
one end (called the rear) and removed from the
other end (called the front)

Linked List: a linear collection of data elements


whose order is not given by physical placements in
memory (non-contiguous). Each element points to
the next.

3 PROGRAMMING
Programming is a transferable skill
Transferable skill: skills developed in one situation
which can be transferred to another situation.

3.1 Variables
Declaring a variable:
o Pseudocode: DECLARE <identifier> : <data type
Assigning variables:
<identifier> ← <value> or <expression>
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

Procedure: subroutine that performs a specific task


without returning a value
Procedure without parameters:

PROCEDURE <identifier>
<statement(s)>
ENDPROCEDURE

When a procedure has a parameter, the function


can either pass it by either reference or value

Pass by value: data copied into procedure so


variable not changed outside procedure

PROCEDURE <identifier> (BYVALUE


<param>: <datatype>)
<statement(s)>
ENDPROCEDURE
Built in functions
LCASE( )/UCASE( ) –for chars Pass by reference: link to variable provided so
TO_UPPER and TO_LOWER for STRING variable changed after going through procedure
LEFT( string, how many chars)
RIGHT(string, how many chars) PROCEDURE <identifier> (BYREF
MID(string, starting position, how many chars) <param>: <datatype>)
CHR(denary number from ascii) <statement(s)>
ASC( character to convert to get ascii value ) ENDPROCEDURE
ISNUM(String) returns true if argument is numeric
LENGTH(string) returns the number of characters in Calling a procedure:
a string CALL <identifier>() Identifier()
INT(real number) /truncates decimal
STRING_TO_NUM( string) /returns integer data type
RANDOMBETWEEN (min,max) /generates a
random number between the given range
RND() /generates a random decimal between 0
and 1
RAND(n) [generates a number between 1 to n. n is
exclusive]

Extracting a character:
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

● Formalize solution: review program code,


revise internal documentation and create
end-user documentation
● Maintain program: provide education and
support to end-user, correct any bugs and
modify if user requests

There are three different development life cycles:


Waterfall model: a classical model, used to create a
system with a linear approach, from one stage to
Another

Iterative model: a initial representation starts with a


small subset, which becomes more complex over
time until the system is complete

Rapid Application Development (RAD) model: a


prototyping model, with no (or less) specific
3.7 Function
planning put into it. More emphasis on
Function: subroutine that performs a specific task
development and producing product-prototype.
and returns a value.
Functions are best used to avoid having repeating
4.2 Integrated Development Environment (IDE)
blocks of code in a program, as well as increasing
A software application that allows the creation of a
the reusability of code in a large program.
program e.g. Python/VB
FUNCTION <identifier> (<parameter>:
Consists of a source code editor, build automation
<data type>)
tools, a debugger
RETURNS <data type>
<statement(s)>
ENDFUNCTION Coding:
Reserved words are used by it as command
4 SOFTWARE DEVELOPMENT prompts
4.1 Program Development Cycle Listed in the end-user documentation of IDE
● Analysis: define problem, record program A series of files consisting of preprogrammed
specifications and recognize inputs, subroutines may also be provided by the IDE
process, output & UI
● Design: develop logic plan, write algorithm Initial Error Detection:
in e.g. The IDE executes the code & initial error detection
● pseudocode or flowchart and test solution carried out by compiler/interpreter doing the
● Coding: translate algorithm into high level following:
language with comments/remarks and o Syntax/Logic Error: before program is run, an
produce user interface with executable error message warns the user about this
processes o Runtime Error: run of the program ends in an error
● Testing: test program using test data, find Debugging:
and correct any errors and ensure results Single stepping: traces through each line of code
are correct and steps into procedures. Allows you to view the
effect of each statement on variables
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

Breakpoints: set within code; program stops


temporarily to check that it is operating correctly up
to that point
Variable dumps (report window): at specific parts
of program, variable values shown for comparison

4.3 Structure Charts


● Purpose: used in structured programming to
arrange program modules, each module
represented by a box.
● Tree structure visualizes relationships
between modules, showing data transfer
between modules using arrows.
● Example of a top-down design where a
problem
4.4 Types of Errors
● (program) is broken into its components.
Features:
Syntax errors:
● Iteration
When source code does not obey rules of the
● Selection
language Compiler generates error messages
● Hierarchy of modules
● Sequence of execution
Examples:
Advantages of structured approach
o Misspell identifier when calling it
● Easy to read and fix code
o Missing punctuation – colon after if
● Problems broken down into many easy to
o Incorrectly using a built-in function
manage chunks
o Argument being made does not match data type
● Procedures and functions are reusable, you
can use them in different projects.
Run-time errors:
● You can test modules individually.
● Source code compiles to machine code but
● Different teams can work on different parts
fails upon execution
of the solution.
● When the program keeps running and you
have to kill it manually

Examples:
o Division by 0
o Infinite loop – will not produce error message,
program will just not stop until forced to

Logic errors:

Program works but gives incorrect output


Examples:
o Out By One – when ‘>’ is used instead of ‘>=’
o Misuse of logic operators
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

4.5 Corrective Maintenance The value of a variable is manually followed to


check whether it is used and updated as expected
White-Box testing: making sample data and running Used to identify logic errors, but not execution
it through a trace table errors
Trace table: technique used to test algorithms;
make sure that no logical errors occur e.g. Walkthrough testing:
A test where the code is reviewed carefully by the
developer’s peers, managers, team members, etc.
It is used to gather useful feedback to further
develop the code.

Integration testing:
4.6 Adaptive Maintenance Taking modules that have been tested on
Making amendments to: individually and testing on them combined together
o Parameters: due to changes in specification This method allows all the code snippets to
o Logic: to enhance functionality or more faster or integrate with each other, making the program
both work.
o Design: to make it more user friendly
Alpha testing:
4.7 Testing Strategies This is the testing done on software ‘in-house’,
meaning it is done by the developers
Black box testing: Basically another term for ‘first round of testing’
Use test data for which results already calculated &
compare result from program with expected results Beta testing:
Testing only considers input and output and the This is the testing done on the software by beta
code is viewed as being in a ‘black box’ users, who use the program and report any
problems back to the developer.Basically another
term for ‘second round of testing’
White box testing:
● Examine each line of code for correct logic Acceptance testing:
and accuracy. A test carried out by the intended users of the
● May record value of variables after each line system: the people who requested the software.
of code The purpose is to check that the software performs
● Every possible condition must be tested exactly as required. The acceptance criteria should
completely be satisfied for the program to be
Stub testing: released.
Stubs are computer programs that act as
temporary replacement for a called module and Features that support modular programming:
give the same output as the actual product or ⦁ The use of functions and procedures
software. Important when code is not completed ⦁ The use of local and global variables
however must be tested so modules are replaced ⦁ The use of parameters and arguments
by stubs.
Advantages of using subroutines:
Dry run testing: 1) Subroutines step wise refinement of a program.
A process where code is manually traced, without 2) Subroutines are used when tried and tested
any software used library routines have to be re-used.
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

3) Subroutines support modular programming


4) Subroutines can be tried and debugged
individually.

What is a program fault?


Something that makes the program not do what it
is supposed to do under certain circumstances.

Ways to minimise risk of faults:


● Use tried and tested subroutines
● Modular programming techniques to break
down the problem so it is easier to solve
● Good programming practices such as
indentation, meaningful identifiers and th3e
use of programmer’s comments
● IDE features such as parameter type
checking, context sensitive prompts, auto
complete, steppin
PAPER 2 : SUMMARY AND NOTES Minahil Hassan Tariq

String operations example:

FUNCTION ValidateRegistration( Registration : STRING) RETURNS BOOLEAN


DECLARE Valid : BOOLEAN
DECLARE L : INTEGER
DECLARE Count : INTEGER
DECLARE MyChar : CHAR
L <- LENGTH(Registration)
Valid <- TRUE
IF L <6 AND L > 9
THEN
Valid <- FALSE
ELSE
FOR COUNT <- 1 to 3
MyChar <- MID(Registration,count,1)
IF MyChar < ‘A’ OR MyChar > ‘Z’
THEN
Valid <- FALSE
ENDIF
ENDFOR
FOR Count <- 4 TO 5
MyChar <- MID(Registration,count,1)
IF MyChar < ‘0’ AND MyChar > ‘9’
THEN
Valid <- FALSE
ENDIF
ENDFOR
FOR Count <- 5 TO L
MyChar <- MID(Registration, Count,1)
IF MyChar < ‘A’ OR MyChar > ‘Z’
THEN
Valid <- False
ENDIF
ENDFOR
END IF
RETURN Valid
ENDFUNCTION

You might also like