0% found this document useful (0 votes)
2 views

CSM 157 PROGRAMMING-Week2

The document provides an overview of algorithms, highlighting their properties such as finiteness, definiteness, generality, effectiveness, and input/output capabilities. It also classifies algorithms based on control shifts and repetitive steps, and discusses common programming errors including syntax, logical, and runtime errors. Additionally, it covers variable naming conventions, declarations, and initializations in programming.

Uploaded by

chamahdavida30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSM 157 PROGRAMMING-Week2

The document provides an overview of algorithms, highlighting their properties such as finiteness, definiteness, generality, effectiveness, and input/output capabilities. It also classifies algorithms based on control shifts and repetitive steps, and discusses common programming errors including syntax, logical, and runtime errors. Additionally, it covers variable naming conventions, declarations, and initializations in programming.

Uploaded by

chamahdavida30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ALGORITHMS

• Is a logical sequence of instructions required to solve a given problem


or a step-by-step instructions for solving a given problem

• They are normally written using English and Mathematical


Expressions
Properties of Algorithms
• Finiteness – Capable of producing the required output after a finite
number of steps for any input or should be able to terminate after a
certain number of steps with the right output.

• Definiteness- All instructions should be precise and not to give room


for ambiguity

• Generality – The instructions should be capable of solving all


problems of a particular type or class
Properties of Algorithms

• Effectiveness – All instructions should be basic and capable of being


performed mechanically within a finite amount of time.

• Having input and Output Capabilities – Capable of accepting data as


input and generate an expected output
Classification of Algorithms
Two main ways of classifying algorithms, by control shifts and by
repetitive steps
- Control Shift – number of alternatives, two alternatives gives
deterministic algorithm while more than two alternatives gives non-
deterministic. If the shift is based on a random number then we have
Random algorithm

- Repetitive – number of repetitive steps. If the number of repetitions


is known in advance then we have direct otherwise indirect
Types of programming Errors
There are three types of error namely
- Syntax: violation of a programming rule or not writing an instruction in
conformity with the rules of the language being used.
Examples are: x + y = z instead of z = x + y
INPUT ABC when A, B and C are variables
IF A>B<C instead of IF A> B and B<C
When a syntax error occurs, it is usually flagged by the interpreter or the
compiler. It can also be detected by the programmer
Types of programming Errors
- Logical: error as a result of one’s reasoning. It is usually detected when
there is disparity between the program output and the expected output.
Example, the roots of the equations 𝑥 2 − 4 = 0 are 2 and -2. If a program
gives 3 and 6 as the roots then there is a problem with the program

- This type of error can only be detected by the programmer or the


program user
Types of programming Errors
Runtime: Occurs when a program is executing. Some of the common
runtime error messages are as follows:
- Subscript out of range
- Device time out
- Illegal function call
- Overflow
What goes into writing Algorithms
• Variables

• Input statements

• Output statements

• Operator and Expressions

• Looping/iterative/repetitive statements

• Decision making statements


Variables and their naming
• Names used in a computer solution to store data are known as variables

• Variable name must start with either an English alphabet (a-z or A-Z) or the
underscore symbol (_). If more than one character is used, the rest of the
characters should be English alphabets, digits (0-9) or the underscore
symbol.

• Variable names can be of any reasonable length

• Variable names must be Short and meaningful to make our programs easy
to read and understand.

• They should not include special characters such as @, #, $


Variables and their naming
• Variable names can be in lower, upper and/or a combination of lower
and uppercase characters.

• Note that some programing languages such as C++ are case sensitive
as such Pay, PAY and paY are different variable names. In this course,
we will assume case sensitivity.

• Variable names must not be keywords or reserved words such as for,


while, repeat, until, if, do, repeat. For this course all words being
used as reserved words will be written in UPPERCASES.

• Whenever two words are concatenated as a variable name, we


will capitalise each word such as NetPay, TotalMark,
IncomeTax, etc
Variables – Try questions
• Indicate whether the following are valid variable names. Those that
are invalid indicate why they are so.
i. d.o.b vii. βeta xiii. base 2
ii. Interest-rate viii. interest_rate xiv. why?
iii. Else ix. IV xv. program
iv. B x. 5Times_Table xvi. εtesen
v. 7y xi. Bra_Kwame xvii. Office 2013
vi. You xii. KNUST xviii. sum
Variables Declarations
• Most programming languages such as C++ require that variables be
declared before their first usage. Hence we will declare all variables before
their usage.
• The format for declaring a variable will be as follows:


DECLARE variable , 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 𝐴𝑆 dataType
0
Where datatype is one of the following:
INTEGER for integer numbers
DOUBLE for real numbers
BOOL for Boolean (True/False)
CHAR for variables that store single characters
STRING for variables that stores more than one character.
Variables Declarations
• Variables that are of the same data type be declared using a single
DECLARE statement.
Examples
(i) To declare A, B and C as integers, this can be written as
DECLARE A, B, C AS INTEGER

(ii) To declare Sum as integer and Average as a real number we can have
DECLARE Sum AS INTEGER
DECLARE Average AS DOUBLE
Variables Initialisation
• In some programming languages, when a variable is declared and it is
used without being assigned an initial value, it would have an
unknown value that can lead to unpredictable outputs when used in
an expression especially if the variable appears to the right hand side
of the replacement sign.
• Initialise yourself any variable whose initial value is obtained from an
input statement or an assignment statement.
Effect of using Integers and Real numbers in an
expression
Operator Integer Real/Double
Integer Integer Real/Double
Real/Double Real/Double Real/Double
Examples
Evaluate each of the following expressions bearing in mind that table
above.

i. 37/38*38+5.0
ii. 37/38.0*38.0+5.0
iii. 37.0/38*38+5.0
iv. 37.0/38.0*38+5.0
v. 37/38*38+5

You might also like