Professional Documents
Culture Documents
1 CS002
- Programs are simply solutions to problems.
CS002
2
General Problem-Solving
Concepts
Chapter 1
3 CS002
1. Identify the problem
◦ What is the specific problem?
(This means you should determine what is that
you want to change).
◦ Clearly define the goal that you want to
achieve. (What are you trying to achieve?).
◦ Determine what are the inputs and
outputs.
4 CS002
2. Understand the problem
◦ Gathering data relative to the problems which
your solution can be achieved.
◦ Do not automatically discard any information.
5 CS002
3. List possible solutions to the problem
◦ Generate as many potential solutions as
possible.
◦ List the features for each possible solution.
6 CS002
4. Select the best way to solve the problem
from the list of alternative solutions.
◦ Establish criteria to help when choosing the
solution
◦ Select the best solution.
7 CS002
5. List instructions that enable you to solve
the problem using the selected solution
(Do Stage).
◦ Planning:
Create a numbered, step-by-step set of
instructions
8 CS002
6. Evaluate the solution(Review and Revise)..
◦ Test the solution
Are the results accurate?
Does the solution solve the original problem?
Does it satisfy the needs of the user?
Is it acceptable to the user?
9 CS002
Now let’s put problem-solving into
action!
10 CS002
Problem description Steps Outcome.
Eman is a doctor and she Identify the problem. Problem: wanting to get contact to the old friends.
isn’t in regular contact Goal: get contact to the old friends.
with her old friends. She is
getting caught up in the Understand the problem. Facts: Searching for e-mail, telephone, home
demands of work and address…etc.
family and she’s not list possible solutions to the
1. Schedule a time e.g. every Sat. to contact by
setting aside time problem writing emails letters. (Cheap - long time to
specifically for old friends. write a letter – no need to be at the same time
available).
2. Buy plan tickets to visit them.
(real contacts, costly, must
have free time to do it)
1. Using phone call to contact.(real contact,
acceptable costs, need short free time ).
Select the best solutions to Criteria: 1- Time.
the problem. 2-Cost.
3- Interactivity.
So, phoning would be the best.
List instructions (Do Stage) Start calling tomorrow at launch time.
Schedule every Sunday at 10 am for calling
Write notes to remember on mirror and…
Tell the friend in advance to be ready.
Evaluate the solution Had great conversation but set a timer when she’s
(Review and Revise). talking so that she will not spend more on the phone
bill.
11 CS002
Problem Steps Outcome.
description
Today is the final Identify the problem. Problem: Enrolling in a university & department
day for Abeer in Goal: Name university and department that she will
high school, she & this year.
her family Understand the problem. Facts:
gathered to decide Searching for Universities available.
which university Knowing her grades, hobbies, specialties
for here to attend. and Gender.
list possible solutions to the go local university.(speed, not have desired
problem section, monthly reward)
go a broad.(trip expenses, have desired section,
free enrolment, monthly reward)
go private university.( trip expenses very
expensive , school I prefer, enrolment is not free )
Select the best solutions to Criteria:
the problem. 1- enters high ranked university and
desired section.
2- Not expensive.
3- Free.
So, solution 2.
List instructions (Do Stage) 1. Send an enrol application to the university.
2. Wait for reply.
3. Pack your stuff, go to university.
4. Attend first day.
Evaluate the solution I felt lonely, grades were low.
(Review and Revise). So, must change solution(revaluate)
12 CS002
Algorithmic Solutions
Steps involved in following a set of instructions.
The solution will be the same each time the algorithm is
followed.
Most computers use algorithmic types of problems.
Heuristic Solutions
Solutions that can’t be reached by following a direct set of
steps.
Created based on knowledge and experience…and by trial and
error.
The results may not produce the same results each time
the algorithm is executed.
Artificial intelligence deals with heuristic types of
problems.
13 CS002
The instructions that will result in the best
solution.
The result may be:
More Efficient
Faster
More Understandable
More Maintainable
Reusable
14 CS002
The outcome of the program
15 CS002
The set of instructions that make up the
solution after they have been coded into a
particular computer language.
17 CS002
1
BEGINNING PROBLEM-SOLVING
CONCEPTS FOR THE COMPUTER
Chapter 2
3 Types of Problems that can be solved on
computers :
2
Computational
problems involving some kind of mathematical
processing
Logical
Problems involving relational or logical processing
Repetitive
Problems involving repeating a set of mathematical
and/or logical instructions.
Fundamental Concepts
3
A value
a specific alphabetical and/or numeric value
Does not change during the processing of all the
instructions in a solution
Can be of any data type
Numeric, alphabetical or special symbols
Examples
3, 5, 10, “Sara”, “+”, “-”, “/”
Constants
5
For example:
√ StudentName for student name (correct).
x Student-Name Incorrect because using a dash symbol.
13
Data
unorganized facts.
They go into the computer as input .
Information
The output of processing of input .
Is printed in the form of reports.
Processing Data – How a computer
Balances a Checkbook
17
Checks
Deposits Calculates the Balance
Bank Charge Balance Sheet
Data Types
18
21
Character Types
22
B 66 b 98
C 67 c 99
D 68 d 100
E 69 e 101
COP1006 McManus
Comparing Characters & Strings
28
Data Storage
Each variable name is given a memory location
Each memory location can hold one and only one
value at a time.
This memories are temporary storage, when a user
enters a new value the old one is destroyed
Example:
COP1006 McManus
Functions
37
1. Mathematical
Used in science and business , Calculate such things as
square root, absolute value
2. String
Manipulate string variable
3. Conversion
Convert data from one data type to another
4. Statistical
5. Utility
Important in business programming
Mathematical Functions
41
1. Mathematical
The data types of operands and resultants of
mathematical operators are always numeric
+, -, *, /
\, Mod ( used in business )
^
functions
Two Special Math Operators
49
Relational operators
Perform comparisons
the use of relational operators is the only way for the
computer to make decisions .
The resultant of relational operation is logical data type .
The operand s can be numeric or character
Relational operatores
A B A AND B
True True True
True False False
False True False
False False False
The OR Operator
56
A B A OR B
True True True
True False True
False True True
False False False
The NOT Operator
57
A NOT A
True False
False True
Hierarchy of Operations
58
False
True
False
False
False
False
True
True
True
Expressions & Equations
62
Expressions
Process the data and the operands, through the use
of the operators
Does not store the resultant (answer)
Examples :
to find the number of square feet in a room :
Length * Width
Equations
63
Examples
A+B C= A + B
A and B are numeric. C ,A and B are numeric.
The resultant is numeric and is The resultant is stored in C .
not stored
A<B C=A<B
A and B are numeric , character, A and B are numeric , character,
or string. or string .
The resultant is logical and is The resultant is stored in C ;
not stored . C is logical.
A OR B C = A OR B
A and B are logical . C ,A and B are logical .
The resultant is logical and is The resultant is stored in C .
not stored
Example : evaluating a mathematical
expression .
5*(X+Y)–4*Y/(Z+6)
uses the following values to evaluate the expression :
X=2 Y=3 Z=6
Operation resultant
1. X + Y 5
2. Z + 6 12
3. 5 * resultant of 1 25
4. 4 * Y 12
5. Resultant of 4 / resultant of 2 1
6. Resultant of 3 – resultant of 5 24
Example : evaluating a relational expression .
A–2 B
uses the following values to evaluate the expression :
A = 6 b= 8
Operation resultant
1. A – 2 4
2. Resultant of 1 B false
Example : evaluating a logical expression .
A AND B OR C AND A
uses the following values to evaluate the expression :
A = true B = false C = true
Operation resultant
1. A AND B false
2. C AND A true
3. Resultant of 1 OR resultant of 2 true
Straight-Line Form
68
(X Y)
Y = (X – Y) / ( (X + Y)^2 / (X – Y )^2 )
(X Y )2
(X Y )2 straight line form
Attachment: ASCII Coding
69
COP1006 McManus
1
PROGRAMMING
CONCEPTS
Chapter 3
Communicating with the computer
2
• The Control module uses an End since this is the end of the processing
. The other modules use Exit because the processing continues .
Drawing the Flowchart
18
Order of
Execution of
Instructions
Algorithm and Flowchart Form
27
Pseudocode
28
Documentation
Internal External
documentation documentation
Internal Documentation
31
Is a file contains:
People who worked on the program
List of all variable name (details).
Note about the development of the program
External Documentation
32
- For example, the square root function is used in an assignment statement as follows:
A = Sqrt(X) + 7
Therefore, the result of the square root of X would be returned in the name of the
function. This value is added to 7 and then stored in A.
The Write instruction outputs values to the printer. The list of information to be printed
follows the Write, as in
Write Name, Age
- The Print instruction is similar to the Write instruction expect that the output is directed
to the screen rather than the printer.
35
Algorithm Instructions (continues)
36
1 T.Fatin Alhila
Decision Logic Structure
2 T.Fatin Alhila
Common Forms of decision structure
•If <condition(s)>
•Then
T •<True instructions>
•Else
F
•<False instructions>
3 T.Fatin Alhila
Decision Logic Structure
4 T.Fatin Alhila
Decision Logic Structure
- Logical operators are used to link more than one condition together.
- The programmer can set up the condition for a decision through the use
of operands and various operators.
6 T.Fatin Alhila
Decision Logic Structure
- These conditions can be used alone or linked with other conditions for
use in the instruction.
- The programmer derives the information needed to set up the
condition(s) for a decision from the problem itself during problem
analysis.
- The True branch and the False can come from any of the lower three
points of the diamond.
- The convention is to draw the True branch on the right and the False
branch on the left or bottom, although it depends on the decisions to be
made.
7 T.Fatin Alhila
Common Forms
The If Statement
The Single Selection If/Then
The Double If/Then/Else
The Multiple If/Then/Else
Straight-through Logic
The Case Statement
The Switch Statement
8 T.Fatin Alhila
1- Single Selection: If/Then
one condition one instruction(true section) no else part
9 T.Fatin Alhila
Algorithm Flowchart
StudentAlert()
1. Integer grade StudentAlert()
2. Enter grade
3. If grade < 4 Integer grade
then
1. Print “Alert:You must study hard”
1. end Enter grade
F T
If grade < 4
Print “Alert:You
must study hard”
End
10 T.Fatin Alhila
2- Double If/Then /Else Selection
one condition two instruction –true-false-else part
overtime pay
Pay=PayRate * (40 + 1.5 * (Hours-40))
11 T.Fatin Alhila
Algorithm Flowchart
PayCalculate()
1. Integer Hours, PayRate PayCalculate()
2. Enter Hours, PayRate
3. Real Pay
4. If Hours > 40 Integer Hours, PayRate
1. Then
1. Pay=PayRate * (40 + 1.5 Enter Hours, PayRate
* (Hours-40))
2. Else Real Pay
1. Pay=PayRate * Hours
5. End
F If Hours > T
40
Pay=PayRate * (40
Pay=PayRate * + 1.5 *
Hours (Hours-40))
12
End T.Fatin Alhila
3- Multiple Double If/Then /Else Selection
- Decision in which you have multiple conditions that lead to one action
or set of actions for True and False are slightly more complicated than
those with single condition.
13 T.Fatin Alhila
Using Straight-through logic
There is no Else part of the instructions; the False branch always goes
to the next decision, and the True branch goes to the next decision
after the instructions for the True branch have been processed.
14 T.Fatin Alhila
Using Straight-through logic
- You must use it to solve certain problems, those that require two or
more unrelated decisions, and those in which all decisions must be
processed.
15 T.Fatin Alhila
Straight-Through Logic Example
Write an algorithm to change the value of X to 0 when X becomes greater
than 100, and to change the value of Y to 0 when Y becomes greater than
250.
16 T.Fatin Alhila
Straight-Through Logic Example
Algorithm Flowchart
ChangeValue()
ChangeValue()
1. Integer X,Y
2. Enter X,Y Integer X,Y
3. If X > 100
1. Then
1. X= 0 Enter X,Y
4. If Y > 250
1. Then T
If X > 100
1. Y= 0
5. End X =0
F
T
If Y > 250
F Y =0
18 T.Fatin Alhila
Logic Conversion
Rules to convert from positive logic to negative logic or vice
versa:
Change all < to >=
Change all <= to >
Change all > to <=
Change all >= to <
Change all = to <>
Change all <> to =
Interchange all of the Then set of instructions with the
corresponding Else set of instructions.
19 T.Fatin Alhila
- For example:
It is to find the amount to charge people of varying ages for a concert
ticket. When the person is under 16, the charge is $7; when the person
is 65 or over, the charge is $5; all others are charged $10.
4. End
End
21 T.Fatin Alhila
Figure 6.12 Conversion from Positive Logic
to Negative Logic
22 T.Fatin Alhila
Figure 6.12 Conversion from Positive Logic
to Negative Logic
23 T.Fatin Alhila
Decision Table
A good way to simplify the process of discovering
complicated actions and conditions is to draw a decision
table.
Most consist of 4 parts:
1. The conditions
2. The actions
3. The combinations of True and False for the conditions
4. The action to be taken or the consequences for each
combination of conditions.
24 T.Fatin Alhila
Table 6.1 Decision Table Format
• The total number of possible combinations of True or False for the conditions is
2^ #conditions
25 T.Fatin Alhila
Decision table Example
Set up a decision table for a store policy for charging a purchase.
There are three conditions:
1. The purchase is less than $100.
2. The last payment to the account was made in the last 30 days.
3. The balance of the account is less than $1000.
The following actions could be taken:
1. Credit is okay, and the customer can charge the item.
2. Refer the customer to the credit department.
3. Credit is denied and the customer cannot charge the item.
26 T.Fatin Alhila
Table 6.2 Decision Table
27 T.Fatin Alhila
Decision Table
The four steps to develop a flowchart from the decision table
are:
1. Draw all decisions in flowchart form.
2. Compare the true and false sides of each decisions, starting
with the first one.
3. Eliminate any decisions that have the same instructions on
both the true and false sides, keeping the true consequence
or action.
4. Redraw the flowchart.
28 T.Fatin Alhila
Development of flowchart from the
Decision table.
29 T.Fatin Alhila
Elimination of Conditions
30 T.Fatin Alhila
Final Flowchart
31 T.Fatin Alhila
Decision Table Example#2
Set up a Decision Table for Numeric Grades associated with
Letter Grades?
A 90-100
B 80-89
C 70-79
D 60-69
F Below 60
32 T.Fatin Alhila
Decision Table Solution
Letter Grade
(Solution-Action)
A B C D F
Numeric Grade
(Condition)
90 <= n X
80 <= n < 90 X
70 <= n < 80 X
60 <= n < 70 X
< 60 X
33 T.Fatin Alhila
let’s work a problem
34 T.Fatin Alhila
Putting It All Together
The Putting It All Together (PIAT) are designed to show that how to
pull together and use the concepts from a previous sections.
The PIAT demonstrates how to use the six steps of problem solving on
the computer to develop a solution that uses the sequential and the
decision logic structures.
35 T.Fatin Alhila
Putting It All Together
- For example:
The Floral Company sells to wholesale and retail buyers. The wholesale
buyer needs a resale number in order to buy at no tax and to receive
discounts. The retail buyer pays 6% tax.
36 T.Fatin Alhila
A Fantastic Floral Company_
PAC (Problem Analysis Chart)
37 T.Fatin Alhila
Fantastic Floral Company –
Interactivity (Structure) Chart
38 T.Fatin Alhila
Fantastic Floral Company – IPO Chart
39 T.Fatin Alhila
Coupling Diagram and Data Dictionary
...
40 T.Fatin Alhila
The Algorithms and Flowcharts –
Control Module
41 T.Fatin Alhila
The Algorithms and Flowcharts –
Read Module
42 T.Fatin Alhila
The Algorithms and Flowcharts –
Calc Module
43 T.Fatin Alhila
The Algorithms and Flowcharts
Print Module
44 T.Fatin Alhila
PROBLEM SOLVING WITH
LOOPS
Chapter 7
Concept of Repetition Structure Logic
Average()
1. Real Sum, Average
2. Sum = St1_Grade+St2_Grade+St3_Grade+ St4_Grade+St5_Grade+St6_Grade+ St7_Grade
3. Average = Sum/ 7
4. Exit
Solution with looping structure
Average()
Real Student_Grade 1. Integer Count=1
Control () 2. Real Sum=0
3. While ( count <= 7)
1. Read() 1. Read()
2. Average() 2. Sum=Sum +Student_Grade
3. Count=Count+1
3. End 4. WhileEnd
5. Real Average = Sum / count
6. Exit
Read()
1. Enter Student_Grade
2. Exit
Average Module Algorithm Flowchart
Flowchart
What will
Happen If What will
Average()
we Happen If Sum has no initial
remove we
this? value, its value is
Integer Count=1 remove
this?
Unknown
Real Sum=0
Count has no
data, its value is F T
While
Unknown, So the Count<=7
value of the
condition is Read() What will
unpredictable. Happen If
Real Average = Sum / 7 we
Sum=Sum+Student_Grade
remove
this?
Exit Count=Count+1
Examples:
Totalsales = Totalsales + Sales
Repeat
Instruction
Instruction
.
.
.
Until< Condition(S)>
Example 1:
While/WhileEnd Repeat/Until
•You must initialize the data so that the •You can set the operand of the conditions
resultant of the condition is true the first anywhere within the loop since the
time through the loop. Otherwise , the loop condition is processed at the end .
instruction will never be processed . •Repeat the loop until the resultant of
•Repeat the loop until the resultant of condition is true.
condition is false. •The condition is processed at the end
•The condition is processed at the beginning •Instructions in the loop are processed
entirely at least once.
Automatic-Counter Loop
Increments or decrements a variable each time the
loop is repeated .
Use it when you know from the start the number of
times the loop will be executed .
The beginning value , the ending value , and the
increment value may be constant , variable , or
expressions .
Automatic-Counter Loop
Variable name
Loop: counter=begin To End Step S
Instruction
Instruction
Instruction
.
.
Loop –End: Counter
Automatic-Counter Loop:
General Rules
When the computer executes the Loop instruction its sets the
counter equal to the beginning number.
3 ( J -1 )
4 J,
5
( J – 1)
J,
Example 2:
Create the algorithm & flowchart to:
Prints the even numbers.
The series of numbers will:
Start From 0 .
Ends at 50.
Example 2 Solution
Algorithm
PrintEvenNum()
1. Loop: counter=0 To 50 Step 2
1. Print counter
2. Loop-End: counter
3. End
Recursion
Another type of loop structure .
A module or a function calls itself .
Example :
FactorialFunction (N)
1. If N > 1
then
Factorial = N * FactorialFunction (N-1)
Else
Factorial = 1
2. Exit