In a program logic formulation of a solution to a given problem, there are two most commonly used tools that will be of great help for you to formulate a better solution.
These are the flowchart and algorithm.
We will limit the topic on algorithm as a tool in the development of a program.
An algorithm is a set or series of instructions for carrying a particular task. It is also a procedure to produce the required output from the given input. It is similar with a paragraph that expresses a single thought. It uses English language. It is also known as pseudocode.
Pseudocode is a mixture of language and symbols, terms and other features commonly from one or more high-level languages. High-level languages are programs used by a computer.
The structure of an algorithm can be displayed in a structure diagram that shows the various steps that must be performed and their relation to one another. These diagrams are especially useful in describing algorithms for more complex problem.
An example of an algorithm represented in pseudocodes is shown below:
Algorithm that determines if the Average is “Passing” or “Failing” :
ALGORITHM PASS _FAIL
INPUT AVE
IF AVE < 75 THEN
OUTPUT “FAILING”
ELSE
OUTPUT “PASSING”
END IF
END PASS_FAIL
The algorithm is written using all capital letters for easy readability.
Basic Elements Used to Construct Algorithms
• Constants
• Variables
• Expressions
Constants. A constant is a literal value that does not change during program execution. It is a stored value.
Types of constant:
1. Numeric Constant– may be an integer or real number.
Two types of Numeric constant
Integer Numeric constant – is a whole number. It has an exact value and does not
contain a decimal point.
Real numeric constant – number that contains a decimal point or an exponent
(or both). It has approximate value.
2. Character Constant – is a single character, enclosed in an apostrophe (single quotation marks).
3. String Constant – consist of any number of consecutive characters (including none) enclosed in double quotation marks.
Variables. A variable is a portion of the computer’s main memory used to store a numeric or a string constant. It is also named memory location. It represents a certain value. The value stored in the variable may be changed during program execution but it can only hold one value at a time
Expressions
An expression is group of program elements consisting of operands and operators. Operands can be a constant or a variable. Operators can be arithmetic and relational. Expressions always yield a value of a certain type depending on the type of expressions used.
Types of Expressions
1. Arithmetic Expressions. An arithmetic expression is an expression consisting of numeric constants or numeric variables separated by arithmetic operator which yields a numeric value.
The general format of an arithmetic expression is as follows:
Operand operator operand operator operand . . .
Example:
4 * 2
4 and 2 are operands
Multiplication symbol * is operator
There are five arithmetic operators in algorithm. They are:
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division or div. operator
% Modulus division
String Expression. A string expression is an expression consisting of string constants or string variables separated by string operator, which yields a string value.
The general format of string expression is the same as in arithmetic expressions above.
Example: A = “Jesus”
B = “will”
C = “guide us”
D = ‘ ‘
D = A + B + C
In D = A,B, and C are string variables
+ operator
Output: D = “Jesus will guide us”
String expressions use only one operator, the + operator. The effect of + operator on string is concatenation. Defined, concatenation means appending the value of the second operand to the end of the first value of the first operand.
Boolean Expressions. A Boolean expression is either a relational expression or logical expression, which yields either a TRUE or FALSE value. (Since this module is Algorithm based on C programming language, the equivalent of TRUE is 1 and FALSE is 0.
Types of Boolean Expressions:
1. Relational Expressions. A relational expression is a Boolean Expression consisting of a numeric or string expressions, numeric or string variables and numeric or string constants, separated by a relational operator.
The general format of a relational expression is: operand operator operand . . .
Both the operands must be of the same type, that is either both numeric or both string. The operand can be a numeric or string expression, numeric or variable, or numeric or string constant.
The following are the relational operators:
Operator Meaning
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
= Equal to (in C, equal is = =
< > Not equal to (in C, equal is !=)
2. Logical Expressions. A logical expression is an expression consisting of one or more relational expressions separated by the logical operator that yields either TRUE or FALSE.
The general format of a logical expression is as follows:
The relational expression before the logical operator is optional only if the logical operator NOT is used.
The following are the logical operators:
NOT
AND
OR
Source: C Programming Modules by Aleta Fabregas, Polytechnic University of the Philipines