You are on page 1of 41

ICT1101

PROGRAM LOGIC FORMULATION


TOPIC 2: PROBLEM SOLVING CONCEPTS FOR THE
COMPUTER
Vimala.doraisamy@s.newinti.edu.my
2020
LEARNING OUTCOMES
• Describe six problem solving steps in solving problems with
algorithmic solutions.
• Differentiate between algorithmic and heuristic solutions to a
problem.
• Explain the following concepts:
• Constants and variables
• Data types
• Functions
• Operators
• Equation and Expressions
GENERAL PROBLEM
SOLVING
• People make decisions to solve problems in their everyday lives.
• Bad decisions lead to wasted time and resources.
• Steps to make good decisions:
• Identify the problem
• Understand the problem
• Identify alternative ways to solve the problem
• Select the best way to solve the problem from the list of alternative
solutions
• List the instructions that enable you to solve the problem using the
selected solution
• Evaluate the solution
TYPE OF PROBLEMS
• Problems can be categorized based on the type of solutions.
• Some problems can be solved with a series of actions.
• This is known as an algorithmic solution
• The solution is reached by completing a series of actions in steps.
• These steps are called the algorithm.
QUESTION

• What are real life examples that can be


solved with algorithmic solutions?
TYPE OF PROBLEMS
(CONT.)
• Some problems do not have straightforward or obvious solutions.
• Solutions require reasoning built on knowledge and experience.
• Includes trial and error process.
• Solutions that cannot be reached through a direct set of steps are
known as heuristic solutions.
QUESTION

• What are real life examples that must be


solved with heuristic solutions?
PROBLEM SOLVING WITH
COMPUTERS
• For computer problem solving, the following terms are used:
• Solution – Instructions that must be followed to solve a problem.
• Results – Outcome or completed computer-assisted answer.
• Program – Set of instructions that make up the solution after they
have ben coded into a particular computer language.

Coding Processing

Solution Program Results


PROBLEM SOLVING WI
COMPUTERS (CON
• Type of problems solved on computers:
• Computational – Involves mathematics


Logical – Relational or logical processing


Repetitive – Repeating mathematical or logical instructions
DIFFICULTIES WITH
PROBLEM SOLVING
• Some people have not been taught how to solve problems.
• Fear to make wrong decisions.
• One or more steps are completed inadequately when going through
the problem-solving process.
• Problem not defined correctly or not understood properly.
• Insufficient list of alternatives.
• Too hasty when selecting a suitable solution.
• Focus too much on details before getting the general idea in place.
• Incorrectly or haphazardly evaluate the solution.
DIFFICULTIES WITH PROBLEM
SOLVING USING COMPUTERS
• Greatest challenge: Writing the instructions
• A computer needs detailed steps to complete a task.
• Without a proper sequence of steps, a computer cannot produce
desired results.
• Remember, a computer cannot think on its own.
CONSTANTS AND
VARIABLES
• Constants
  and variables are used by a computer to solve problems.
• Constant
• A specific alphabetical or numeric value that never changes during
the processing of instructions in a solution.
• Can be any type of data.
• A constant can be given a name.
• Once a constant has been assigned a value it cannot be
changed during the execution of a program.
• Example: The value of is 3.14159 and cannot be changed.

Imagine a constant as a rock


CONSTANTS AND
VARIABLES (CONT.)
• Variable
• Values of a variable may change during processing.
• Also known as identifiers in some languages because its name identifies
what the value represents.
• The name also helps to find the memory location that stores the value of a
particular data type.

Imagine a variables as playdough


CONSTANTS AND
VARIABLES (CONT.)
• Variables
• Variable name should be consistent with what the value represents.
CONSTANTS VS
VARIABLES
Constants Variables
Your bank account number. The amount of money in your bank.
The size of your wallet. The amount of money in your wallet.
The maximum marks you can get for your Your actual marks.
exam (100).
NAMING CONVENTION
• Naming conventions vary based on the company you work for:
• variableName
• variable_name
• VariableName
• Be consistent in naming variables:
• E.g. if Hours is used for number of hours, do not use Hrs or
HoursWorked because both these names refer to different variables.
• Importance of naming convention:
• Avoid conflicting names.
• Easy to read due to consistent naming.
• Easy to maintain the code.
RULES FOR NAMING AND
USING VARIABLES
• Name a variable according to what it represents.
• E.g. If a variable describes the number of hours, then name it as Hours.
• Do not use spaces in a variable name.
• Start a variable name with a letter.
• Do not use a dash or any other mathematical operator in a variable name.
• After you have introduced a variable name, use the exact name in the rest
of the code.
• A different name represents a different variable.
• Be consistent using upper- and lower-case characters.
• Use naming convention specified by the company.
• Constants can be named as all upper-case characters.
RULES FOR NAMING AND
USING VARIABLES (CONT.)

HoursWorked If the variable represents Hours Worked


Hours the number of hours worked. Bob
TimeInHours Hours-Worked
hoursWorked Hours!Worked
hours_worked 1Hours
HoUrSwOrKeD

KCONSTANT A constant called K. K constant


k-constant
K!constant
kCoNstaNt
DATA TYPES
• Data are unorganized facts.
• Data go into the computer as input.
• Data is processed by the computer to produce information.
• Information is returned to the user as output.
DATA TYPES (CONT.)
• Computers must be told the data type of each variable or constant.
• Numeric
• Character
• Logical
• User-defined
• Data types cannot be mixed.
• What do you think these data types
represent?
DATA TYPES (CONT.)
FUNCTIONS
• Functions are small sets of instructions that perform specific tasks
and return values.
• They are basic tasks that can be used repeatedly to solve
problems.
• Advantages:
• Shorten problem solving time.
• Improve readability of solution.
• Functions can be written by programmers or taken from a library
of functions.
FUNCTIONS (CONT.)

int main() int FunctionName(int


{ data)
int data = 0; {
data = data = data + 5;
FunctionName(data); return data;
} }
• Mathematical calculations such as square root, generate random
Mathematical numbers or absolute value.
• SQRT(4), RANDOM(), ABS()

• Manipulate string variables such as copying or finding the


String length.
• COPY(“Bob”, Name), LENGTH(“My name is Bob”)

• Convert one data type to another.


Conversion • STR_TO_INT(“123”), INT_TO_STR(123)

• Used to calculate things such as minimum, maximum, mean


Statistical values.
• MIN(100,20,500), MAX(100,20,500), MEAN(100,20,500)

• Used to access information outside the program such as time


Utility and date.
• TIME(), DATE()

Function Classes
OPERATORS
• Operators are the data connectors within expressions and equations.
• Operators tell a computer how to process data and what type of
processing needs to be done.
• Mathematical
• Logical
• Relational

+ - < > x AND OR NOR


OPERANDS VS
RESULTANT
• Operands are the data connected and processed by operators.
• Resultant is the answer when the operation is complete.
• For the mathematical operation below, identify the operands,
operator and resultant.

1+1=2
MATHEMATICAL
OPERATORS
• Examples:
RELATIONAL OPERATORS
• Relational operators compare two operands and make a decision.
• The resultant of relational operators is logical data: TRUE or
FALSE
LOGICAL OPERATORS
• Connect relational expressions.
• Perform operations on logical data.
• Operators: NOT, AND, OR
• Operands and resultants are both logical data.
• Example:
• Relational Operation: Object = “Pokemon” Resultant: TRUE
• Relational Operation: Color = “Yellow” Resultant: TRUE
• Relational Operation: Type = “Electric” Resultant: TRUE
• Logical Operation:
(Object = “Pokemon”) AND (Color = “Yellow” ) AND (Type
= “Electric”)
Resultant: TRUE
Summary of Logical Operations
• Find the resultants:
• 5+4
• True OR False
• 5<8
• 10<3
• “Bob” = “Brad”
• True AND False
• “Brad” <> “Billy”
HIERARCHY OF
OPERATIONS
• Note the following expression:
• 1+2/3*5>2*5+4/3
• How would you evaluate the expression?
• All operators have a hierarchy which decides which operations
take precedence (comes first).
HIERARCHY OF
OPERATIONS (CONT.)
Operator Order of Operand Data Resultant Data
Operations Type Type
1. Parenthesis ()
2. Functions
Mathematical 3. Power Numeric Numeric
4. \, Modulo (MOD) Numeric Numeric

5. *,/ Numeric Numeric


6. +,- Numeric Numeric
Relational 7. ==, <, >, <=, >=, Numeric, string or Logical
<> character
Logical 8. NOT Logical Logical
9. AND Logical Logical
10. OR Logical Logical
EXERCISE
• Evaluate the following expression based on the hierarchy of
equations:

• 1+4/2*5>2*5+18/3
ANSWER
• 1+4/2*5>2*5+18/3
• Based on hierarchy of operations:
• 4/2*5 = 10
• 2*5 = 10
• 18/3 = 6
• 1+10>10+6
• 11>16
• Resultant: FALSE
TRUTH TABLE
• A
  table that shows all the possible resultants of a logical
expression.
• The number of possible resultants for n variables is .
• Example:
• NOT A OR B will have resultants.
A B NOT A NOT A OR B
TRUE TRUE FALSE TRUE
TRUE FALSE FALSE FALSE
FALSE TRUE TRUE TRUE
FALSE FALSE TRUE TRUE
EXPRESSIONS AND
EQUATIONS
• Expression
• Translate a real life situation into a mathematical equation.
• Never stores resultant data for later use.
• E.g.
Length * Width
• Equation
• Stores resultant data of an expression in a memory location.
• Also known as assignment statements, because the resultant is
assigned to a variable.
• In an equation, “=“ refers to an assignment.
• E.g.
Area = Length * Width
EXPRESSIONS AND
EQUATIONS (CONT.)
Expressions Equations

A and B are numeric. C, A, and B are numeric.


The resultant is numeric and is not stored. The resultant is stored in C.

A and B are numeric, character, or string. A and B are numeric, character, or string.
The resultant is logical and is not stored. C is logical.
The resultant is stored in C.

A and B are logical. C, A, and B are logical.


The resultant is logical and is not stored. The resultant is stored in C.
REVIEW LEARNING
OUTCOMES
• Describe six problem solving steps in solving problems with
algorithmic solutions.
• Differentiate between algorithmic and heuristic solutions to a
problem.
• Explain the following concepts:
• Constants and variables
• Data types
• Functions
• Operators
• Equation and Expressions
QUESTIONS?
END OF TOPIC 2

You might also like