Programming Handout - Problem Solving
Programming Handout - Problem Solving
Information Technology
4th Form March 2021
Topic: Introduction to Problem Solving
When you are faced with a problem, which of the following would you do?
1. Quit?
2. Yell for ‘Mummy’?
3. Migrate?
4. Pout?
5. Try to find a solution?
Computers are designed to solve problems speedily and accurately. Although computers are used
to solve problems, they do not have brains. They cannot think. A computer is a moron – it simply
does exactly what we tell it to do. This is why the role of the computer programmer is so
important. Instructions are given to the computer in the form of computer programs. A computer
program is a finite set of precise instructions, written in a programming language. Before we
write a computer program, we first have to find a way to solve the problem at hand.
* Input refers to the source data provided to solve the give problem. - Easily identified by the
keywords – given, read or accept, input, prompt.
* Output refers to the end result required - keywords are print, display, produce, output,
list, show, suitable label, appropriately labelled, or “caption
*Processing is the actions must be performed to achieve the required output - it answers the
question: What must I do with the inputs in order to produce the desired result. Check for words
like “calculate” and “compute, add, sum, subtract, divide average”.
VARIABLES: In the computer, values are stored in memory locations. In order to keep track of
where our values are stored, we need to place a label or identifier in a particular memory
location. The label or identifier is called a variable.
A variable is a symbolic name assigned to a memory location that stores a particular value.
E.g. name= Mary Jane.
It is good practice to choose variable names that reflect the kind of data that is being stored,
e.g. num1- it is going to store the first number entered by the user
age1- it is going the store the first age entered by the user.
Sum- it is going to store the total of a group of numbers.
Average- it is going to store the average of a group of numbers, grades or scores
A variable or constant can only store one piece of data.
INITALIZATION OF VARIABLES
Variables that are used as counters or used to store totals should always be assigned an initial
value of zero (0) before they are used. This is called initialization, e.g. Count = 0.
The initialization ensures that the variable is cleared of any values that may have been assigned in
a previous execution of the program
For example 1st Execution: Count= 1, 2nd Execution: Count=0
OPERATORS
Operators can be either Mathematical (computes arithmetic expressions) or Logical (performs
decision making where the answer is either True or False).
The Mathematical operators are:
NOT
Expression Result
False True
True False
1. Write the input (what is needed from the person using the algorithm).
2. Write the output (the end result that is stated in the problem).
3. In the processing column write "get" and anything that is under the input column.
4. Ask yourself the following question. “What do I have to do with the inputs in order to
produce the desired output?” Then write down what should be done.
5. Write "display" and anything under the Output column.
PROBLEM 1:
Write a solution to read three numbers and calculate and print the total.
Add 3 numbers
PROBLEM 2:
Given three integers representing the age of three boys respectively, write a solution to find their
average age.
Add 3 ages