Introduction To Problem Solving C++
Introduction To Problem Solving C++
Objectives
MOHD RIDZUAN B ABDUL RAHMAN BACHELOR IN COMPUTER FORENSIC MANAGEMENT & SCIENCE UNIVERSITY
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:
1.1.1
INPUT (data)
PROCESS (processor)
OUTPUT (information)
Input Refer to the process of entering data, program and instructions into the
Output Output is raw data that has been processed by the computer (result).
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
PROCESS
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.
CHAPTER 1
After the input data are processed, instructions for displaying or printing are executed. Result displayed by a program is called program output.
Step 1
Input data: Card Number, PIN Number, and transaction.
Machine language program for processing card number and PIN number.
Step 2
Central Processing Unit.
Step 3
Computed results. Program Output Output results: Receipt and money
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).
CHAPTER 1
PROBLEM ANALYSIS PROBLEM ANALYSIS PROGRAM DESIGN PROGRAM DESIGN PROGRAM CODING PROGRAM CODING TESTING AND DEBUGGING TESTING AND DEBUGGING MAINTENANCE MAINTENANCE
D O C U M E N T A T I O N
Documentation: Process of recording the steps carried out during the development. Documentation: Process of recording the steps carried out during the development. Each step must be completed before moving to next step. Each step must be completed before moving to next step.
STEP 1: PROBLEM ANALYSIS Purpose: To describe in details a solution to a problem by providing the needed information for the problem. How? o o o 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. First, understand & study the problem. Identify: The input to the problem. The required output. The relevant process. For example, scientific formula or appropriate
CHAPTER 1
Problem Analysis: Input: 3 numbers. Process: Output: 1. Add the numbers 2. Divide the total with 3 The 3 numbers and average
INPUT
PROCESS
OUTPUT
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 A programmer writes the solution in the form of an algorithm before coding it Example of algorithm to calculate the average of 3 numbers: 1. 2. 3. 4. 5. Set Total=0, average=0; Input 3 numbers Total up the 3 numbers Total= total of 3 numbers Calculate average average=Total/3 Display 3 numbers and the average
2. Flowchart
CHAPTER 1
S rt ta
In u a pt
, b, c
T ta o l
= a + b + c
ae g v ra e
= T ta o l
/3
D p ya is la , b, c D p ya e g is la v ra e
Ed n
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: START INPUT a, b, c Total = a + b + c average = Total / 3 OUTPUT a, b, c OUTPUT average END
COBOL and etc. Solution: Instructions before it is coded into programming language. Purpose: To produce a program to develop a system.
CHAPTER 1
# include <iostream.h> Example of syntax in C++ language to display instruction for user to enter 3 numbers on computer screen. 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; }
#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; }
Output Screen: Input: Three numbers entered by user. Input three numbers: 4 4 4 Average of the numbers: 4.00_
CHAPTER 1
Using a set of data to discover errors and to ensure accuracy of the program. Diagram indicates the process of testing. Output (functioning well or error discovered
Testing Process:
Executing Program
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 Testing 2: Input :7,8,6 From the output, is the program produce the correct output?
Definition of Debugging:
An error is called as bug. Bug must be identified and corrected; the process is called debugging.
1.
Two types of error: 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. 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.
2.
CHAPTER 1
Syntax error will occur if any mistake in the program. Example: No semicolon.
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 error is an error that occurred Logic error is an error that occurred because of incorrect of logical statement because of incorrect of logical statement in program. in program. Example: Example: Sum is total of the 3 numbers. Sum is total of the 3 numbers. Supposed the average is sum / 3 Supposed the average is sum / 3 because there are 3 numbers. because there are 3 numbers.
Logic Error Logic Error Input three numbers: 444 Average of the numbers: 2
LOGIC ERROR
MOHD RIDZUAN B ABDUL RAHMAN 10
CHAPTER 1
STEP 5: MAINTENANCE Definition: Activity to verify that the operational system is still performing as planned or modified The process of changing a system after it has been applied is to maintain its ability. to enhance the system to meet the current requirement. The changes may involve simple changes such as correct errors, coding and enhancement. How to do maintenance? o By performing activities such as: Testing test the ability of the system. Measurement access data time. Example, time to save, print and others. Replacement replace the old system to new system. Adjustments adding needs to new system. Repairs Example, old system cannot update new data, Updating modify database.
o
o o o o
Example: Maintenance of Telecommunication System TELEKOM. Telecommunication digits. System TELEKOM provides
communication service from one house to another house. The telephone number is 7 o digits. o Programmer will change the old system to new system and test the system whether it functioning well or not. Assume that management wants to change 7 digits to 8
Then, new system will be tested its capability overall. For example, test the capability to identify callers and users location and time is taken to connect them (measurement).
o
function (replacement).
Any old function that no longer used will be replaced with new
STEP 6: DOCUMENTATION Definition: Documentation is a written report or graphic record of the steps carried out during the development of program.
11
CHAPTER 1
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 o o o o o o Description of the program. Specification requirement of program Program design such as pseudocode and flowchart List of program and comments (to explain about the program). Test results. End user manual. 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): Package 2 (RM 50): Package 3 (RM 50): Package 4 (RM 55): Package 5 (RM 50): Package 6 (RM 55): 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. 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. 3. 4. Fee = Fee + Package_1 Else Set Fee = 0 Enter name and package If Package = 1 Set payment: Bahasa Melayu, Bahasa Inggeris, Sejarah Matematik, Sains, Geografi Bahasa Melayu, Matematik, Sejarah Bahasa Inggeris, Sains, Geografi Bahasa Melayu, Sains, Geografi Bahasa Inggeris, Matematik, Sejarah
12
CHAPTER 1
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 5. Display name and fee Flowchart
13
CHAPTER 1
START
Package _1: RM 55 Package _2: RM 50 P ackage _3: RM 50 Package _4: RM 55 Package _5: RM 50 Package _6: RM 55
Fee = 0
If Package =1 If Package =2
If Package 3
If Package 4
If Package 5
END
Pseudocode START Package_1= RM 55 Package_2= RM 50 Package_3= RM 50 Package_4= RM 55 Package_5= RM 50 Package_6= RM 55 Fee = 0 Input name, Package If Package = 1 Fee = Fee + Package_1 Else If Package = 2 Fee = Fee + Package_2 Else If Package = 3 Fee = Fee + Package_3
14
CHAPTER 1
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 OUPUT name, Fee END 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; 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 cout<<Invalid data; cout<<Do you want to continue?; cin>>terus; } while (terus == Y); cout<<Name: <<nama; cout<<Monthly fee to pay:<<Fee; } STEP 4: Testing and Debugging
15
CHAPTER 1
No error has detected. Program runs efficiently after testing. Output as required.
STEP 5: Maintenance o Maintenance to enable the program to process number of students in one time. Programmer will modify current system to new system and test the system whether its functioning or not.
o
data (measurement).
Then, new system will be tested overall. For example, test the capability to process students data in one time and the duration to process the
o
function (replacement).
Any old function that no longer used will be replaced with new
16