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

Problem Solving

Uploaded by

stxvie.ee
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)
24 views

Problem Solving

Uploaded by

stxvie.ee
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/ 12

Problem Solving

Problem solving in IT looks at how to get the computer to solve a task / problem that the user has; this usually involves
writing a program.

STEPS IN PROBLEM SOLVING:

• Define the problem – clearly identify what the problem is leaving no uncertainty
• Analyse the problem – break down the problem into its basic components (Input, Processing and Output)
• Propose and evaluate solutions – identify different solutions
• Determine the most efficient solution
• Develop the algorithm
• Test and evaluate the algorithm / solution
• Write the program

Algorithms
This is a step-by-step solution to a problem. An algorithm is NOT the computer program and does not affect the computer
in any way; it’s just a design of the program solution (similar to a blue print). An algorithm can be represented using a
FLOWCHART (uses reserved shapes); PSEUDOCODE (uses reserved words and phrases); NARRATIVE (in
English).

CHARACTERISTICS OF A GOOD ALGORITHM

A good algorithm should have the following characteristics:

• Finite number of steps – the algorithm should complete its task at some point
• Precise – the instruction must not leave room for any misunderstanding
• Unambiguous (clear) instruction
• Flow control from one process to the next (ensuring vales are correct before moving on)
• Termination (stop)

ADVANTAGES OF AN ALGORITHM

• Makes creating the program easier (because it acts as a guide)


• Allows for continuation (if the programmer leaves then another programmer can continue)
• Useful for remembrance
• Helps in finding errors

ADVANTAGES AND DIS-ADVANTAGES OF FLOWCHARTS

• Gives a better representation of the solution


• Better analysis and problem solving
• Less reading
• Can become very complicated
• Difficult to alter (entire drawing may have to be redone)
• Requires drawing to be made so it takes a longer time to produce
ADVANTAGES AND DIS-ADVANTAGES OF PSEUDOCODES

• Easier to convert to an actual program as it is similar to a programming language


• it is fairly easy to understand
• it does not matter if there are errors in the syntax - it is usually still obvious what is intended
• changes to the design can be incorporated quite easily
• It can be hard to see how a program flows due to excessive reading
• It can be time consuming to produce (not as long as a flowchart)

MATHEMATICAL SYMBOLS USED IN ALGORITHM DEVELOPMENT INCLUDE

• + • <= • =
• – • > • 
• / • >= • MOD / MODULUS
• * • <> • DIV
• < • !=

Pseudocode
Pseudocode uses reserved words; some of these reserved words are:

• START • ENTER • IF…ENDIF


• STOP • INPUT • IF…ELSE…ENDIF
• BEGIN • WRITE • WHILE…ENDWHILE
• END • PRINT • REPEAT…UNTIL
• DECLARE • OUTPUT • FOR…ENDFOR
• READ • PUT
• GET • DISPLAY

Flowchart
This is an example of an algorithm that is written using reserved words and statements. These shapes are:

PROCESS READ AND


WRITE CONNECTOR
REPEAT
AND
START AND DECISION
DECLARATION
STOP
Identifiers
This a named area of memory that is used to store ONE value. There are three different identifiers:

• Variable – named area of memory that can store one value that can be changed.
• Constant - named area of memory that can store one value that cannot be changed.
• Literal - named area of memory that can store one value that is taken as is / whose value speaks for itself.

When creating variables and constants the following rules should be followed:

• Cannot start with a number or special character


• Can be upper or lower case or a mixture
• The name should indicate the value being stored
• Can use the underscore to join two words
• Cannot have any spaces

Data Types
These are used to determine the values that can be stored in a variable or constant. Examples of data types are:

• Integers – positive or negative whole numbers (4, 7, -13)


• Real / Float / Floating point – positive or negative decimal numbers (9.0, 7.4, -3.1)
• String – this is a group of characters (“hello”, “stop”,” help”)
• Characters – this is a single letter or symbol (‘e’,’?’)
• Boolean – this stores either TRUE / FALSE

N.B. Variables are normally initialized before use. Initialization means to prepare a variable for use
Control Structures
These are strategies used to create programs that are more organised, efficient and shorter. These control
structures are:

• SEQUENCING

This ensures that the instructions follow a logical (sensible) order.

get num1 get num1

get num2 get num2

get num3 calculate total

calculate total print average

calculate average calculate average

print average get num3

• DECISION MAKING / BRANCHING

allows different parts of a program to execute based on the results of certain logical conditions.

grade = 65

F grade T
Print “Fail” Print “Pass”
>=60

STOP
• LOOPING / ITERATION

This allows certain parts of a program or the entire programs to repeat based on the outcome of certain logical
conditions.

count = 1

count F
<= 5 STOP

Print “Hello”

count = count + 1

There are four types of loops:

• Post test loop – the test condition comes AFTER the action(s). This is shown in the diagram below.

count = 1

Print “Hello”

count = count + 1

T count F
STOP
<= 5
• Pretest loop – the test condition comes BEFORE the action(s)

• Definite loop / Bounded Loop (FOR…ENDFOR) – This number of times this loop will repeat is known

FOR <condition> DO
ACTION
ENDFOR

• Indefinite loop / Unbounded Loop (WHILE…ENDWHILE / DO…WHILE / REPEAT…UNTIL) – The amount


of time this loop will repeat is unknown

WHILE <condition> DO

ACTION

ENDWHILE

DO

ACTION

WHILE <condition>

REPEAT

ACTION
UNTIL <condition>
Algorithm Examples
E.g., 1 Design a program to print “hello world” to the screen

I P O
NONE Printing “Hello World” to the screen Hello World

START

WRITE “Hello World”

STOP

E.g., 2 Design a pseudocode to get a positive whole number from the user and print the square of the number to the
screen

I P O
A number Calculate the square of the number The square of the number

START
DECLARE num, square AS INTEGER
num = 0
square = 0
WRITE “Please enter a number”
READ (num)
square = num * num
WRITE “The square of the number is “, square
STOP

E.g., 3 Design a pseudocode to get a two whole number from the user and print the sum of the numbers

I P O
2 numbers Calculate the sum The sum of the two numbers

START
DECLARE num1, num2, sum AS INTEGER
num1 = 0
num2 = 0
sum = 0
WRITE “Enter 2 numbers”
READ (num1, num2)
sum = num1 + num2
WRITE “The sum of the two numbers are”, sum
STOP
E.g.,4 Design a pseudocode to read the length and width of a rectangle and output the perimeter.

I P O
Length Calculate perimeter of rectangle Perimeter
Width

START
DECLARE length, width, perimeter AS INTEGER
length = 0
width = 0
perimeter = 0
WRITE “Enter length and width”
READ (length, width)
perimeter = (length + width) * 2
WRITE “The perimeter of length and width is”, perimeter
STOP

E.g., 5 Design a pseudocode to read the diameter of a circle and print the radius

I P O
Diameter Calculate the radius Radius

START
DECLARE d, r AS INTEGER
d=0
r=0
WRITE “Please enter the diameter”
READ (d)
r = d /2
WRITE “The radius is “, r
STOP
E.g., 6 Bryan bought a shirt from a store downtown that was having a sale. He paid $2000 for the shirt and got a
20% discount. Calculate the discount and the final price of the shirt.

I P O
Cost of the shirt Calculate the discount Discount
Calculate the final price Final price

START
DECLARE cost, disc, discount, fp AS REAL
cost = 2000.00
discount = 20%
fp = 0.00 TRACE TABLE
Cost discount Disc Fp
disc = cost * discount 2000.00 20% 400.00 1600.00
fp = cost – disc
WRITE “The discount given is “, disc
WRITE “The final price is “fp
STOP
N.B. A traceable is used to locate errors and to trace the values in the variables. It is made up of columns that
contain the variables, constants, conditions and outputs.

E.g., 7 There are 12 marbles to be distributed among 3 children in the ratio 2, 4, 6. Design a program that will show
the number of marbles each child is to receive. Your answer should include a pseudocode, an algorithm and a trace
table.

I P O
Number of marbles Calculate number of marbles each child Number of marbles received
should receive

START
DECLARE ratio1, ratio2, ratio3, totalmarbles, totalratio AS REAL
ratio1 = 2
ratio2 = 4
ratio3= 6
totalmarbles = 12
totalratio = ratio1 + ratio2 + ratio3
ratio1 = (ratio1 / totalratio) * totalmarbles
ratio2 = (ratio2 / totalratio) * totalmarbles
ratio3 = (ratio3 / totalratio) * totalmarbles
WRITE “First child will receive “,ratio1, “marbles”

WRITE “Second child will receive “,ratio2, “marbles”


WRITE “Third child will receive “,ratio3, “marbles”
STOP
E.g., 8 Design a program to read two integers, which are not the same, and print which one is larger.

I P O
2 integers Identify the larger number Print the larger number

START
DECLARE integer1, integer2 AS INTEGER
integer1 = 0
integer2 = 0 TRACE TABLE
WRITE “Please enter integer 1” integer1 integer2 integer1 > integer2 Output
READ (integer1) 0 0
WRITE “Please enter integer 2” 8 2 TRUE Integer 1 is larger
READ (integer2) 1 7 FALSE Integer 2 is larger
IF integer1 > integer2 THEN
WRITE “Integer 1 is larger”
ELSE
WRITE “Integer 2 is larger”
ENDIF
STOP

E.g., 9 Design a program to read the age of a person, if the age is less than18 then the program should print error.

I P O
Age Check if age is less than 18 Appropriate message

START
DECLARE age AS INTEGER TRACE TABLE
age = 0 Age age < 18 Output
WRITE “Please enter age” 0
READ (age) 12 TRUE Error
IF age < 18 THEN 18 FALSE
WRITE “ERROR” 21 FALSE
ENDIF
STOP
E.g., 10 Design a program to read the name, age and grade of a student. It is to print back the name and letter grade
that the student received. The letter grade is determined by the following mark scheme:

80 – 100 = A

60 – 79 = B

40 – 59 = C

0 – 39 =D

I P O
Name Calculate the letter grade Letter grade
Age Name
Grade

START
DECLARE name AS STRING
age, grade AS INTEGER
letter_grade AS CHARATER
name = “ “
age = 0
grade = 0
letter_grade = ‘ ‘
WRITE “Enter student’s name”
READ (name)
WRITE “Enter student’s age”
READ (age)
WRITE “Enter student’s grade”
READ (grade)
IF grade >=80 THEN
letter_grade = ‘A’
ELSE
IF grade >=60 THEN
letter_grade = ‘B’
ELSE
IF grade >=40 THEN
letter_grade = ‘C’
ELSE
IF grade >=0 THEN
letter_grade = ‘D’
ELSE
WRITE “Error”
ENDIF
ENDIF
ENDIF
ENDIF
WRITE “Student’s name is”, name,” and the letter grade is”, letter_grade
STOP

TRACE TABLE
Name Age grade grade >=80 grade >=60 grade >=40 letter_grade
0 0
Jordan 17 0 F F F D
Ven 16 79 F T - B
Christie 18 100 T - - A
Ronald 12 51 F F T C

You might also like