CSC415
Course CO1 CO2 CO3 CO4
Outcomes
Learning LO1, LO2 L01,LO2
outcome
Lesson Outcome :
Learn the program development life cycle (C1,A1)
Explore and apply the process of problem solving techniques (C1,C3)
Identify and write the algorithm (C2, P1)
Problem Solving
Programming is a process of problem solving
Problem solving techniques
o Analyze the problem
o Outline the problem requirements
o Design steps (algorithm) to solve the problem
o
Algorithm:
o Step-by-step problem-solving process
o Solution achieved in finite amount of time
Problem Solving Process
Step 1
Analyze the problem
Thoroughly understand the problem
Outline the problem and its requirements
Understand problem requirements
Does program require user interaction?
Does program manipulate data?
What is the output?
If the problem is complex, divide it into subproblems
Analyze each subproblem as above
Design steps (algorithm) to solve the problem
If problem was broken into subproblems
o Design algorithms for each subproblem
Check the correctness of algorithm
o Can test using sample data
o Some mathematical analysis might be required
1
NORMAZIAH - FSKM
CSC415
Step 2
Implement the algorithm in code
1. Write the Code
Once the algorithm is designed and correctness verified
Write the equivalent code in high-level language (C++)
Enter the program using text editor
2. Compiling and Linking
Run code through compiler
If compiler generates errors
Look at code and remove errors
Run code again through compiler
If there are no syntax errors
Compiler generates equivalent machine code
Linker links machine code with system resources
3. The Loader and Executing
Once compiled and linked, loader can place program into main memory for
execution
The final step is to execute the program
Compiler guarantees that the program follows the rules of the language
Does not guarantee that the program will run correctly
Verify that the algorithm works
Step 3
Maintenance
Use and modify the program if the problem domain changes
2
NORMAZIAH - FSKM
CSC415
Errors can be made during coding which prevent the successful compiling and linking.
Common errors :
Syntax error = caused by a typographical errors and incorrect use of the
programming language.
Logic error = an error due to incorrect order of statements, incorrect formula others
that can cause the incorrect output.
Runtime error = occurs when a program gives the computer instructions that it is
incapable of executing. Causes a program to terminate abnormally (crash or abort).
3
NORMAZIAH - FSKM
CSC415
Algorithm
Algorithm in Real Life
Consider the following .
Problem: Baking a Cake
How to solve:
1. Start
2. Preheat the oven at 180oC
3. Prepare a baking pan
4. Beat butter with sugar
5. Mix them with flour, eggs and essence vanilla
6. Pour the dough into the baking pan
7. Put the pan into the oven
8. End
Structured Design in Algorithm
Problem: Prepare a Breakfast
Step 1 :
1. Start
2. Prepare a Breakfast
3. End
Step 2 :
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
Step 3 :
1. Start
2. Prepare a Breakfast
2.1. Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2. Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
4
NORMAZIAH - FSKM
CSC415
2.3. Make a cup of coffee
2.3.1 Boil water
2.3.2 Add water with sugar and coffee
3. End
What is the connection between these real life processes and algorithm?
Algorithm
A specific and step-by-step set of instructions for carrying out a procedure or solving
a problem, usually with the requirement that the procedure terminate at some point
2 types of algorithm representation will be explained:
o Flowchart
o Pseudocode/Algorithm
Flowchart
Flowchart represents algorithm graphically. It is intended for communication and
documentation
5
NORMAZIAH - FSKM
CSC415
Flowchart Basic Syntax
Symbol Semantic
Start/End
Process
Input/Output
Test
Connector
Flow of activities
6
NORMAZIAH - FSKM
CSC415
Problem Solving Process
Input Process Output
Example 1 - Rectangle
Design an algorithm to find the perimeter and area of a rectangle
The perimeter and area of the rectangle are given by the following formulas:
perimeter = 2 * (length + width)
area = length * width
Example 1
Algorithm:
- Start
Get length of the rectangle
Get width of the rectangle
Find the perimeter using the following equation:
perimeter = 2 * (length + width)
- Find the area using the following equation:
area = length * width
- Display perimeter and area
- End
Example 2
Calculate and display the price of a number of apples if the quantity in kg and price per
kg are given.
Example 3
A car park has the following charges:
The 1st hour costs RM2.00. The subsequent hours cost RM1.00 per hour. Write an
algorithm based on a vehicles entry and exit time.
Example 4
Write a program to calculate the average mark of three CSC 125s students.
Calculate payCheck using the equation
payCheck = baseSalary + bonus + additionalBonus
7
NORMAZIAH - FSKM
CSC415
Structured Programming
Structured design:
Dividing a problem into smaller subproblems
Structured programming
Implementing a structured design
The structured design approach is also called
Top-down design
Stepwise refinement
Modular programming
ANSI/ISO STANDARD C++
C++ evolved from C
C++ designed by Bjarne Stroustrup at Bell Laboratories in early 1980s
C++ programs were not always portable from one compiler to another
In mid-1998, ANSI/ISO C++ language standards were approved
Algorithm Development Guidelines
Identify the input and output of the problem.
If necessary, use Divide & Conquer strategy to decompose the problem into smaller
and manageable sub problems. Decompose the problem until all the sub problems can
be solved to get the required results
For each sub problem, identify and list out the steps involved in solving it
Pseudocode /Algorithm
An outline of a program, written in a form that can easily be converted into real
programming statements. It resembles the actual program that will be implemented
later. However, it cannot be compiled nor executed.
Pseudocode normally codes the following actions:
Initialisation of variables
Assignment of values to the variables
Arithmetic operations
Relational operations
Example of Pseudocode
1. Start
2. Read quantity
3. Read price_per_kg
4. total = quantity * price_per_kg
5. Print total
6. End
8
NORMAZIAH - FSKM