introduction to problem solving c++
DESCRIPTION
C++TRANSCRIPT
CHAPTER 1 INTRODUCTION TO PROBLEM SOLVING
MOHD RIDZUAN B ABDUL RAHMAN
BACHELOR IN COMPUTER FORENSIC
MANAGEMENT & SCIENCE UNIVERSITY
Objectives
Introducing programming concept
Introducing technique to develop a program
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
1.1 Definition of Program, Programming and Programmer
To understand program design in application software, user should know definitions of:
Program:
A set of step-by-step instructions that directs a computer to perform a specific
task and to produce the required results.
Programming:
Programming is a process of designing or creating a program.
Programmer:
Programmer is a person who writes the program.
1.1.1 Steps of Program Execution
Input – Refer to the process of entering data, program and instructions into the
computer system using input devices.
Process – Computer processed raw data into usable information to be used by
user. The process is done by the CPU (Central Processing Unit ).
Output – Output is raw data that has been processed by the computer (result).
Output will be converted to understandable form before displayed or printed.
MOHD RIDZUAN B ABDUL RAHMAN 1
INPUT (data) OUTPUT (information)
PROCESS (processor)
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
Example: Flow of ATM program
Assume that our transaction is money withdrawal. The instructions are:
a. Get card number from user (Input)
b. Get pin number from user (Input)
c. Process the input data (Process)
d. Get the transaction chosen by user (Input)
e. Get the account type from user (Input)
f. Process the transaction (Process)
g. Withdraw amount of money required by user (Output)
h. Print Receipt for user (Output)
Input: Example: Card number, pin number, type of transaction, type of account,
amount of money to withdraw.
Process: Example: Process to identify card number, valid pin number, type of
transaction, type of account and deducts the withdrawal from user account.
Output: Example: Receipt will show balance in user account and money withdrawal.
1.1.2 Executing A Program
To execute a program, CPU will examine each program instruction in memory and send
out the required command signals to carry out the instruction.
During execution, data can be entered into memory and manipulated in some specified
way (delete and modify) as required.
Program instructions are used to copy a program's data (data input) into memory.
After the input data are processed, instructions for displaying or printing are executed.
Result displayed by a program is called “program output”.
MOHD RIDZUAN B ABDUL RAHMAN 2
INPUT PROCESS OUTPUT
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
Example: Money withdrawal from ATM machine.
First step:
Data entered by user and stored in memory (input).
Second step:
CPU will instruct program to process the card number and ATM pin number with the
data in memory at the same time. Program will execute the transaction choose by
user and store the result in memory.
Third step:
The outputs are money and receipt (output).
1.2 Programming Process (Programming Life Cycle)
Cycle: Refers to the needs for changes of old program to new program (the cycle will
start again).
Programming Life Cycle: A framework or discipline, which using the techniques needed
in computer programming development.
MOHD RIDZUAN B ABDUL RAHMAN 3
Machine language program for processing card number and PIN number.
Data entered during execution.
Computed results.
Central Processing
Unit.
Output results:Receipt and money
Input data: Card Number, PIN Number, and transaction.
Program Output
Step 1
Step 2
Step 3
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
Steps that involved programming life cycle:
STEP 1: PROBLEM ANALYSIS
Purpose:
To describe in details a solution to a problem by providing the needed information for the
problem.
How?
First, understand & study the problem.
Identify:
o The input to the problem.
o The required output.
o The relevant process. For example, scientific formula or appropriate theories if any.
Problem 1: Write a program that get 3 numbers as input from user. Find the average
and display the numbers and the average.
Problem Analysis:
Input: 3 numbers.
Process: 1. Add the numbers
2. Divide the total with 3
Output: The 3 numbers and average
MOHD RIDZUAN B ABDUL RAHMAN 4
PROGRAM DESIGNPROGRAM DESIGN
PROGRAM CODINGPROGRAM CODING
TESTING AND DEBUGGINGTESTING AND DEBUGGING
MAINTENANCEMAINTENANCE
DOCUMENTATION
PROBLEM ANALYSISPROBLEM ANALYSIS
Documentation: Process of recording the steps carried out during the development.
Each step must be completed before moving to next step.
Documentation: Process of recording the steps carried out during the development.
Each step must be completed before moving to next step.
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
STEP 2: PROGRAM DESIGN
Definition: It defines the framework or the flow or the problem solution
Method to design a program:
1. Algorithm
Algorithm is a sequence of instructions to solve a problem written in human
language and problem can be solved if follow the correct procedure
A programmer writes the solution in the form of an algorithm before coding it into
computer language.
Example of algorithm to calculate the average of 3 numbers:
1. Set Total=0, average=0;
2. Input 3 numbers
3. Total up the 3 numbers
Total= total of 3 numbers
4. Calculate average
average=Total/3
5. Display 3 numbers and the average
2. Flowchart
A graphical representation of data, information and process or an orderly step-by-step
solution to a problem.
Example:
MOHD RIDZUAN B ABDUL RAHMAN 5
INPUT PROCESS OUTPUT
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
3. Pseudocode
Steps in problem solving written in certain of programming code and certain in human
language.
For example, some part use C++ language code and some part use English-like
phrases.
Example:
STEPS 3: PROGRAM CODING
Definition: Write a solution into a specific programming language such as C, C++, COBOL
and etc.
Solution: Instructions before it is coded into programming language.
Purpose:
To produce a program to develop a system.
MOHD RIDZUAN B ABDUL RAHMAN 6
START
INPUT a, b, c
Total = a + b + c
average = Total / 3
OUTPUT a, b, c
OUTPUT average
END
# include <iostream.h>
main( ){
int a, b, c, HasilTambah; double purata;
cout<<" Enter 3 numbers:"; cin>>a >> b >> c; HasilTambah = a + b + c; purata = HasilTambah / 3; cout<< a = “<<a <<”\n”; cout<<b = “<<b<<endl; cout<<”\nc = “<<c; cout<<"Average is = “<<purata;
return 0; }
Example of syntax in C+
+ language to display
instruction for user to
enter 3 numbers on
computer screen.
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
Example of programming in C++ language:
STEP 4: TESTING AND DEBUGGING
Definition of Testing:
Using a set of data to discover errors and to ensure accuracy of the program.
Testing Process:
Diagram indicates the process of testing.
Example 1:
Assume that a program to find the average of 3 numbers has been coded. Then, execute the
program with a few sample data (value) in order to verify whether the program is functioning well
and produce the accurate output (result).
Testing 1:
Input :4,5,8
MOHD RIDZUAN B ABDUL RAHMAN 7
Input three numbers:
4 4 4
Average of the numbers: 4.00_
Input: Three numbers entered by user.
Output: The average of 3 numbers
Output Screen:
#include<iostream.h>#include<conio.h>
main( ){
int a,b,c; // input variables int sum; // variable - holds the result of (a + b + c) float avg; // output variable clrscr( );
// Ask user to enter 3 numbers cout<<"\n Input three numbers : "; cin>>a >> b >> c;sum = a + b + c; // calculate the addition of a, b, c avg = sum / 3; // calculate the average of a, b, c
// display the average of the 3 numbers cout<<"\n Average of the numbers : "<<avg;
return 0;
}
Input sample of data set Executing Program
Output (functioning well or error discovered
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
Testing 2:
Input :7,8,6
Definition of Debugging:
An error is called as bug.
Bug must be identified and corrected; the process is called debugging.
Two types of error:
1. Syntax Error (grammatical error)
Occurs when the rules of programming language are not applied.
It is usually found and corrected during coding a program.
Can be traced by the compiler during compilation.
Also known as compile-time error
Must be corrected before executing and testing the program.
2. Logic error
Cannot be traced by compiler.
Always found and corrected during problem solving.
Also known as run time error.
Example: The correct output is 4 but when it runs the output is 2.
MOHD RIDZUAN B ABDUL RAHMAN 8
From the output, is the program produce the correct output?
Line 9 where the syntax error occurred.
Syntax error message
- Syntax error will
occur if any mistake
in the program.
- Example:
No semicolon.
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
STEP 5: MAINTENANCE
Definition:
Activity to verify that the operational system is still performing as planned or modified to
enhance the system to meet the current requirement.
The process of changing a system after it has been applied is to maintain its ability. The
changes may involve simple changes such as correct errors, coding and enhancement.
How to do maintenance?
By performing activities such as:
o Testing – test the ability of the system.
o Measurement – access data time. Example, time to save, print and others.
o Replacement – replace the old system to new system.
o Adjustments – adding needs to new system.
o Repairs – Example, old system cannot update new data,
MOHD RIDZUAN B ABDUL RAHMAN 9
SYNTAX ERROR
#include<iostream.h>#include <conio.h>
main(){
int a, b, c;int sum;
float avg;clrscr();
cout<<“Input three numbers: “ ;cin>>a >> b >> c;sum = a + b + c; avg = sum/6;cout<<“Average of the three numbers: “<<avg;
}
Logic ErrorLogic Error
Input three numbers:4 4 4Average of the numbers: 2
The wrong outputThe wrong output
LOGIC ERROR
Logic error is an error that occurred
because of incorrect of logical statement
in program.
Example:
Sum is total of the 3 numbers.
Supposed the average is sum / 3
because there are 3 numbers.
Logic error is an error that occurred
because of incorrect of logical statement
in program.
Example:
Sum is total of the 3 numbers.
Supposed the average is sum / 3
because there are 3 numbers.
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
o Updating – modify database.
Example: Maintenance of Telecommunication System TELEKOM.
o Telecommunication System TELEKOM provides
communication service from one house to another house. The telephone number is
7 digits.
o Assume that management wants to change 7 digits to 8
digits.
o Programmer will change the old system to new system and
test the system whether it functioning well or not.
o Then, new system will be tested its capability overall. For
example, test the capability to identify caller’s and user’s location and time is taken to
connect them (measurement).
o Any old function that no longer used will be replaced with
new function (replacement).
STEP 6: DOCUMENTATION
Definition:
Documentation is a written report or graphic record of the steps carried out
during the development of program.
Purpose: It is useful in the future if the program needs modification/maintenance. It is
a reference for the development team.
Content of Documentation:
o Description of the program.
o Specification requirement of program
o Program design such as pseudocode and flowchart
o List of program and comments (to explain about the program).
o Test results.
o End user manual.
o Program capabilities and limitation.
Example: Problem solving using the steps in Programming Life Cycle
Problem: To calculate total payment for fee in Murni Tuition Centre. Provided
packages:
Package 1 (RM 55): Bahasa Melayu, Bahasa Inggeris, Sejarah
Package 2 (RM 50): Matematik, Sains, Geografi
Package 3 (RM 50): Bahasa Melayu, Matematik, Sejarah
MOHD RIDZUAN B ABDUL RAHMAN 10
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
Package 4 (RM 55): Bahasa Inggeris, Sains, Geografi
Package 5 (RM 50): Bahasa Melayu, Sains, Geografi
Package 6 (RM 55): Bahasa Inggeris, Matematik, Sejarah
STEP1: Problem Analysis
Input: Subjects taken according to package
Process: Calculate fees based on the package
Output: Monthly fees to pay
STEP 2: Algorithm
1. Set payment:
Package_1: RM 55
Package_2: RM 50
Package_3: RM 50
Package_4: RM 55
Package_5: RM 50
Package_6: RM 55
2. Set Fee = 0
3. Enter name and package
4. If Package = 1
Fee = Fee + Package_1
Else
If Package = 2
Fee = Fee + Package_2
Else
If Package = 3
Fee = Fee + Package_3
Else
If Package = 4
Fee = Fee + Package_4
Else
If Package = 5
Fee = Fee + Package_5
Else
If Package = 6
Fee = Fee + Package_6
Else
“Invalid data
MOHD RIDZUAN B ABDUL RAHMAN 11
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
5. Display name and fee
Flowchart
PseudocodeSTART
Package_1= RM 55Package_2= RM 50Package_3= RM 50Package_4= RM 55Package_5= RM 50 Package_6= RM 55Fee = 0Input name, Package If Package = 1
Fee = Fee + Package_1Else
If Package = 2Fee = Fee + Package_2
MOHD RIDZUAN B ABDUL RAHMAN 12
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
ElseIf Package = 3
Fee = Fee + Package_3Else
If Package = 4Fee = Fee + Package_4
ElseIf Package = 5
Fee = Fee + Package_5Else
If Package = 6Fee = Fee + Package_6
Else“Invalid Data”
OUPUT name, FeeEND
STEP 3: Program Coding (C Language)
#include <iostream.h>
main (){
float Package_1= 55, Package_2= 50, Package_3= 50, Package_4= 55;float Package_5= 50 , Package_6= 55, Fee = 0;
cout<<“Enter student name:<<”\n”;cin>> nama;
do{cout<<“Enter Package:” <<”\n”;cin >>Package;if (Package == 1)
Fee = Fee + Package_1;else
if (Package == 2)Fee = Fee + Package_2;
elseif (Package == 3)
Fee = Fee + Package_3;else
if (Package == 4)Fee = Fee + Package_4;
elseif (Package == 5)
Fee = Fee + Package_5;else
if (Package == 6)Fee = Fee +
Package_6;else
cout<<“Invalid data”; cout<<“Do you want to continue?”; cin>>terus;
} while (terus == ‘Y’);cout<<“Name: ”<<nama;cout<<“Monthly fee to pay:”<<Fee;
}
MOHD RIDZUAN B ABDUL RAHMAN 13
TCS1013 INTRODUCTION TO PROGRAMMING CHAPTER 1
STEP 4: Testing and Debugging
No error has detected. Program runs efficiently after testing.
Output as required.
STEP 5: Maintenance
Maintenance to enable the program to process number of students in one time.
o Programmer will modify current system to new system and
test the system whether it’s functioning or not.
o Then, new system will be tested overall. For example, test
the capability to process student’s data in one time and the duration to process the
data (measurement).
o Any old function that no longer used will be replaced with
new function (replacement).
MOHD RIDZUAN B ABDUL RAHMAN 14