Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
Prepared for:
CHAPTER 1:PROGRAM DEVELOPMENT
LIFE CYCLE
CSC 128 – Fundamentals of Computer Problem
Solving
© Najwa Abd Ghafar – UiTM
Johor
OBJECTIVES OF THIS CHAPTER
In this chapter, you will learn about:
The steps to do programming
How to develop an algorithm
How to write simple computer program in C++
Syntax and logic error
© Najwa Abd Ghafar – UiTM
Johor
COMPUTER SYSTEM
Programming Language:
Is used by programmers to write computer programs
It is used to write instructions written by programmer in
order to tell the computer what to do
Most people refer programming language to high level
language
What is a Programming
Language?
© Najwa Abd Ghafar – UiTM
Johor
HOW A C++ PROGRAM IS PROCESSED
SOURCE
CODE
(.cpp)
OBJECT
CODE
(.obj)
EXCECUTABL
E FILE
(.exe)
Compiler Linker
Program is
written in
C++
MyProgram.c
ppProgram is
translated into
Machine Language
MyProgram.o
bj
Translates source
program into Machine
Language
Link the object program
with other programs from
the library
Program is combined
with programs from
library and is ready for
execution
MyProgram.e
xe
© Najwa Abd Ghafar – UiTM
Johor
PROBLEM SOLVING PROCESS
Programming:
Is a process of PROBLEM SOLVING
Before you can write your codes inside the computer,
you NEED TO KNOW HOW TO SOLVE the problem first
What is Programming?
© Najwa Abd Ghafar – UiTM
Johor
PROBLEM SOLVING PROCESS
To solve a problem, you should follow the steps below:
1. Analyze the Problem
Start by outlining the problem
Design an algorithm
2. Implement the Algorithm
Use a programming language (C++) to prove that the
algorithm works
3. Maintain the Program
Modify the program wherever necessary
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPEMENT LIFE CYCLE
Analysis
Design
Implementation
Testing
Maintenance
Program Development Life Cycle (PDLC):
Act as a guide for programmers when building a program
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
1. Problem Analysis:
When analyzing a problem, you need to
determine:
1. Input:
What kind of data do you need from the
user
2. Process:
What do you need to do get your output
3. Output:
What do you want to display to the user
PROCESSINPUT OUTPUT
© Najwa Abd Ghafar – UiTM
Johor
EXAMPLE
Analyze the following problem:
Find the summation of 2 numbers
Input Process Output
1st number
2nd number
sum = 1st number + 2nd
number
“The sum of 2 numbers
is”
sum
Use an IPO table for Problem
Analysis
132
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Analyze the following problem:
The program should print out your name and the
phrase “C++ is my favourite subject!”
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Analyze the following problem:
Find the total of days within the month of June,
July and December.
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Analyze the following problem:
Find the perimeter of a rectangle
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
2. Algorithm Design:
Algorithm:
Is a set of sequential instructions that are followed to solve a
problem
What is an ALGORITHM?
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
Algorithm for finding the summation of 2 numbers
(BEFORE refinement as pseudocode):
1. Read 2 numbers
2. The summation of 2 numbers is calculated by adding up the 1st
number and the 2nd number
3. Display the summation of 2 numbers that was calculated
Basically, algorithm is a STEP-BY-STEP instructions on how to
solve a problem
HOWEVER, when human language is used,
the meaning of the sentence can be imprecise at times
(what is written might be different than how it is read)
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
2. Algorithm Design:
Therefore in order to make an algorithm more precise,
there are 2 ways an algorithm can be designed:
1. Pseudocode:
In written form
2. Flowchart:
In a graphical form
These algorithm design is more
precise because:
Uses limited vocabulary
Make use of variables
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
2. Algorithm Design:
Pseudocode:
Is an algorithm in written form (text-based) by using a
human language
The flow of algorithm should be written in sequence
with:
A start
An end
TIP:
The flow of your pseudocode can clearly be seen
when you do numbering for each step
© Najwa Abd Ghafar – UiTM
JohorEXAMPLE
Design a pseudocode for the following problem:
Find the summation of 2 numbers
1. Start
2. Display “Enter the 1st number”
3. Read num1
4. Display “Enter the 2nd number”
5. Read num2
6. sum = num1+ num2
7. Display “The sum of 2 numbers is” and sum
8. End
INPUT
PROCESS
OUTPU
T
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
The following pseudocode algorithm has an error. The
program is supposed to ask the user for the length and width
of a rectangular room, and then display the room’s area. The
program must multiply the width by the length in order to
determine the area.
Find the error.
1. Begin
2. area = width x length
3. Read width
4. Read length
5. Display “The area of the room is” and area
6. End
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a pseudocode for the following problem:
The program should print out your name and the
phrase “C++ is my favourite subject!”
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a pseudocode for the following
problem:
The program should find the total of days within
the month of June, July and December.
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a pseudocode for the following problem:
The program should find the perimeter of a
rectangle
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a pseudocode for the following problem:
Determine whether a person is eligible to enter the
theme park based on their age. Appropriate message
should be given to that person:
Age Description
Less than 10 Not eligible because you’re too young
Between 10 and
49
Eligible to enter
50 or more Not eligible because it might be dangerous
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
2. Algorithm Design:
Flowchart:
Is an algorithm in graphical form which is made up of
symbols
The flow of algorithm should be written in sequence
with:
A start
An end
This algorithm design allow us to see the actual
“flow” of the program
© Najwa Abd Ghafar – UiTM
Johor
Symbols Use of Symbols
Start / End
Input / Output
Process
Decision
Connector
Flow Line
PROGRAM DEVELOPMENT LIFE CYCLE
Flowchart Symbols:
© Najwa Abd Ghafar – UiTM
Johor
EXAMPLE
Design a flowchart for the following
problem:
Find the summation of 2 numbers
Start
Read num1,
num2
sum = num1 +
num2
Display “The sum of 2 numbers is”
, sum
A
A
End
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a flowchart for the following problem:
The program should ask for the user’s age and
display the following message “This year you are
____ years old”.
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a flowchart for the following problem:
Find the average of 5 numbers.
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a flowchart for the following problem:
Determine whether a person is eligible to enter the
theme park based on their age. Appropriate message
should be given to that person:
Age Description
Less than 10 Not eligible because you’re too young
Between 10 and
49
Eligible to enter
50 or more Not eligible because it might be dangerous
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Design a flowchart for the following problem:
Find the largest of 3 numbers.
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
3. Algorithm Implementation:
Once the algorithm is designed and has been correctly
verified, the algorithm can now be implemented
But HOW?
This is when you do your CODING inside the
computer
by FOLLOWING the algorithm that you have
designed
© Najwa Abd Ghafar – UiTM
Johor
EXAMPLE
The following is a program that will find the summation of 2 numbers:
#include <iostream>using namespace std;
int main(){
int num1, num2, sum
cout << "Enter the 1st number: ";cin >> num1;cout << "Enter the 2nd number: ";cin >> num2;
sum = num1 + num2;
cout << "The sum of 2 numbers is : "<< sum
return 0;}
INPUT
PROCE
SSOUTPU
T
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
Compiling:
Once you have finished writing your codes, you can try
and run your program
But WHAT DO YOU NEED to run your program?
COMPILER:
Will ensures that your program follows the constructs of
the language
(a.k.a NO SYNTAX ERROR!!)
When there are no syntax error, it will then translate the
program into machine code
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
4. Program Testing:
Your program should be tested with all types of data and
cases
so that you can find and eliminate errors
Errors is also known as bugs
The process of removing bugs is called debugging
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
Possible Errors in a Program:
1. Syntax Error:
Also known as compile-time error
Occurs during compilation when you incorrectly write a
command or some other part of the program
2. Logic Error:
Occurs when a program runs, but unexpected results are
produced
Often produced by an incorrect algorithm.
3. Runtime Error:
Also known as execution-time error
Occurs during execution, which results in an error
message and an abnormal end to program execution
(program crash).
© Najwa Abd Ghafar – UiTM
Johor
QUICK EXERCISE
Answer the following questions:
1. What type of error occurs when you misuse a C++
language, similar to a grammatical error?
2. What type of error occurs when your code doesn’t perform
the task it’s intended to perform?
3. What type of error occurs when there is a severe logic error
that prevents your program form executing?
4. What type of error would be identified by the compiler?
© Najwa Abd Ghafar – UiTM
Johor
PROGRAM DEVELOPMENT LIFE CYCLE
5. Program Maintenance:
Involves you modifying or upgrading the existing program
or system
It is important to document your program so that you and
other programmers can make changes or updates later
There are 3 types of documentation:
1. Documentation in the Program:
Comments can be used to explain certain codes
2. Documentation outside the Program:
Diagrams, flowcharts or descriptions can be used to
explain how the programming problem was solved
3. Documentation for the User:
User documentation or user manual is used to explain
the functions of the program