GUIDE BOOK PROBLEM SOLVING AND PROGRAM DESIGN Noorashikin Nazir Ts. Zurita Abdul Kadir Nik Nor Azlyzae Abdul Manaf
PROBLEM SOLVING AND PROGRAM DESIGN Noorashikin Nazir Ts. Zurita Abdul Kadir Nik Nor Azlyzae Abdul Manaf GUIDE BOOK PUBLISHED BY
POLITEKNIK SULTAN IDRIS SHAH KEMENTERIAN PENDIDIKAN TINGGI COPYRIGHT RESERVED PUBLICATION 2023 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the written permission of the owner and publisher of Politeknik Sultan Idris Shah. Published by Politeknik Sultan Idris Shah Sungai Lang, 45100 Sungai Air Tawar Selangor Darul Ehsan Telephone No: 03-3280 6200 Fax No: 03-3280 6400 Website: www.psis.mypolycc.edu.my eISBN 978-967-2860-75-4
Noorashikin Nazir an accomplished author and educator with over 18 years of expertise in the Information Technology field. Graduated in Information Technology (Multimedia Studies), she embarked on a journey in the multimedia development industry for a few years before finding her true calling as a lecturer at Politeknik Sultan Idris Shah, where she has been inspiring and nurturing young minds for over 14 years. Her rich experiences in both industry and academia have shaped her into a passionate individual, particularly in the domains of problem solving and program design, mobile application development, VR and human computer interaction. Ts. Zurita Abdul Kadir is an inspiring leader and educator, currently as a Head of Information and Communication Technology Department at Sultan Idris Shah Polytechnic. With a strong academic background in Computer Science she has 17 years of experience as a lecturer at Politeknik Sultan Salahuddin Abdul Aziz Shah. Prior to her teaching career, she spent four years as an Analyst in development systems. Her passion for Mathematics Engineering, Problem Solving & Program Design, Software Requirement & Design, and Programming Fundamentals shines through her 10 Engineering Mathematics Books written for engineering students. Her dedication to education and expertise in her field make her a valuable asset to the academic community. Nik Nor Azlyzae Abdul Manaf is an accomplished author and educator whose contributions to academia. She has been a dedicated lecturer for over 14 years at Politeknik Sultan Idris Shah and over 2 years at Politeknik Kota Bharu, equipped with an Ijazah Sarjana Muda Sains Komputer from Universiti Teknologi Malaysia. She is a skilled writer who is also passionate about Problem Solving and Program Design, Operating System, and Human-Computer Interaction.
ACKNOWLEDGEMENT PREFACE SYNOPSIS CHAPTER 1 Introduction to Programming Language CHAPTER 2 Problem Solving Methods CHAPTER 3 Fundamentals of Programming Language CHAPTER 4 Basic Programming Codes BIBLIOGRAPHY 1 23 69 165 i ii iii 198
Thank s to the Almight y who has gi ven Hi s Bles s to us as the writer s for fini shing the Problem Sol v ing and Program Des ign eBook entitled PSPD GUIDE BOOK. We would li ke to expres s our s incere and deep gratitude for those who has guided in completing thi s eBook especiall y to rev iewer panel s for their comment s , consultation and guidance in order to make thi s eBook perfectl y for our s tudent s as a guide book . Las tl y , a special thank you to our famil y for being so supporti ve and under s tanding during the preparation of thi s PSPD GUIDE BOOK. Hopefull y thi s eBook can help and guide the reader s to expand their knowledge about Problem Sol v ing and Program Des ign. Noorashikin Nazir Ts. Zurita Abdul Kadir Nik Nor Azlyzae Abdul Manaf i
PSPD GUIDE BOOK i s written specificall y for Pol y technic s tudent s who are s tudy ing Problem Sol v ing and Program Des ign. The main purpose of creating and publi shing thi s book i s to introduce the guidance of the techniques in Problem Sol v ing and Program Des ign. The concept s learned in thi s book can be applied to many real life problems which can be sol ved by writing the programming codes us ing C++ languages and al so can be a useful guide for undergraduates . Thi s book i s des igned with the infographic concept s with the intention of prov iding the guidance not onl y for the s tudent s but al so for the educator s too. Thi s book prov ides quick information and guidance for problem sol v ing and program des ign concept s with various examples , acti v ities and exerci ses . It i s the writer s hope that with the completion of thi s book , it can benefit s the s tudent s especiall y in sol v ing the programming problems from the various scenarios . ii
Thi s book i s written with the intention of prov iding the quick information and guidance for the Pol y technic s tudent s in Problem Sol v ing and Program Des ign. For each topic of thi s book , there are examples of acti v ities and solutions prov ided. The exerci se ques tions al so prov ided at the end of the chapter to further s trengthen s tudent s under s tanding. The book has been organized by chapter. Chapter 1 i s about the Introduction to Programming Language. It cover s the hi s tor y of programming language, the t ypes of programming language and generations . In addition, Chapter 1 cover s the various of language trans lator. Chapter 2 describes the Programming L ife Cycle (PLC). Chapter 2 al so cover s the problem sol v ing concept. In addition, Chapter 2 describes the different t ypes and patterns in algorithms to sol ve the problem. Chapter 3 i s about the fundamental s of programming language. It cover s the data and identifier, describe the operator s in a program and al so describe the control s tructure in problem sol v ing. Chapter 4 i s about the bas ic programming codes . It cover s the element s of the programming language and identif y the s tandard and bes t practices in programming language. In addition, it al so cover s the s teps in creating a C++ program and cover s the bas ic programming concept. Thi s chapter al so includes a topic on cons tructing a pseudo code and flowchart based on a gi ven programming code. In summar y , thi s book i s a comprehens i ve guide to under s tanding the Problem Sol v ing and Program Des ign for the s tudent s and educator s . iii
1.1.1 Describe the history of programming language and approaches. 1.1.2 Differentiate the different types of programming languages: a. Machine Language b. Assembly Language c. High Level Language 1.1.3 Identify the generations of programming language with example: a. First generation b. Second generation c. Third generation d. Fourth generation e. Fifth generation 1.1 PROGRAMMING LANGUAGE Chapter 1 INTRODUCTION TO PROGRAMMING LANGUAGE 1
Programming Language is a set of rules and reserved words (keywords) that can be used to tell a computer what are the operations to be done. Artificial language composed by a fixed vocabulary and set of rules is used to create instructions for the computer to follow. History of Programming Language and Approaches Now, there are over 200 programming languages in the world. For example: Cobol, Pascal, C, C++, JAVA and Fortran. 2
First Generation Language (Machine Language) Second Generation Language (Assembly Language) Third Generation Languages (High Level Language) Fourth Generation Languages (High Level Language) Categories of Programming Language Fifth Generation Languages (High Level Language) 3
The lowest level of language. Uses 1s and Os / binary digits to represents data and instructions. Examples: 0 represents “off” and 1 represents “on”. The only language that the computer could understand. Does not require translator. First Generation Language (Machine Language) Example: Machine Language 4
The computer processes the instructions in machine language very quickly. Programmers must have knowledge of the machine hardware and its configuration. Programmer needs to remember a number of binary codes to write machine language programs. Advantage Disadvantages First Generation Language (Machine Language) 5
Known as symbolic language. It uses mnemonic codes - a symbol chosen to help user to remember. replace “0” and “1” For example, ADD and SUM Computer does not understand the language so it needs an assembler to translate the program to machine language. Second Generation Language (Assembly Language) Example: Assembly Language 6
It is easy for programmers to remember the alphanumeric codes than the binary codes. Debugging is very easier when compared to machine language. The major disadvantage is that, it is machine dependent. Program written for one computer might not run in other computers with different hardware configuration. Advantages Disadvantage Second Generation Language (Assembly Language) 7
It is called a procedural language. Translation is done by compiler / interpreter. Example: C, C++, FORTRAN, COBOL and BASIC. Third Generation Languages (High Level Language) Example: C++ and C Language 8
Easier to learn and understand than an assembler language as instructions (statements) that resemble human language or the standard notation of mathematics. Have less-rigid rules, forms, and syntaxes, so the potential for error is reduced. Less efficient than assembler language programs and require a greater amount of computer time for translation into machine instructions. Advantages Disadvantage Third Generation Languages (High Level Language) 9
Program is written in English-like statements. It is a short programming language where the operation needs a few lines instead of hundreds of lines. It is a non-procedural language. Known as query language, which can be used to retrieve data from database. Example: FOCUS, LISP and SQL. Fourth Generation Languages (High Level Language) Example: SQL Language 10
Result-oriented where they emphasize on what instead of how. It is simpler and easier, so programmers and non-programmers can use this language with minimum training. Generally far less efficient during program execution that programs in highlevel languages. Advantages Disadvantage Fourth Generation Languages (High Level Language) 11
A programming language that resembles human language. Known as knowledge-based language. Translates human instructions into code (machine language) where a computer can understand. Example: PROLOG, OPS5 and Mercury. Fifth Generation Languages (High Level Language) Example: PROLOG program 12
Programs are almost machine-independent. It is not necessary for the programmer to have knowledge about computer hardware. It is very easy to learn and write programs in high-level languages. Programs continue to get more complicated and the automated system is unable to sort through the design of an original algorithm. Advantages Disadvantage Fifth Generation Languages (High Level Language) I3
Chapter 1 INTRODUCTION TO PROGRAMMING LANGUAGE 14 1.2 FUNDAMENTALS OF PROGRAMMING LANGUAGES 1.2.1 State the definitions of programmer, program and programming. 1.2.2 Explain the following language translators: a. Assembler b. Compiler c. Interpreter 1.2.3 Relate the programming languages application in real life.
Person who writes the program Programmer Definitions of Programmer, Program and Programming Set of step-by-step instructions that directs a computer to perform a specific task and to produce the required results Program A process of designing / creating a program Programming I5
Language Translator Checks syntax of a program to ensure the programming language is used correctly, by giving you the syntaxerror messages, which is known as diagnostics Produces descriptive error messages known as syntax-error to programmer if there are errors in the programs Translates a program into language that the computer can understand I6
Types of Translator ASSEMBLER Translates the whole program at one time. Also called assembler program. A program, which is used to convert assembly language to machine language. I7
COMPILER Translate the whole program at one time. Translates the source code, for example COBOL language to machine language. Used to translate C, COBOL and other language. I8 Types of Translator
INTERPRETER Program that translates a high level language to machine language. It translates one line at a time and executes each line after it is translated. Displays feedback as the error occurred. Used to translate BASIC and SQL language. I9 Types of Translator
QUIZ! Time 1 2 3 Answ er: 1. A 2. A 3. Executes other programs directly, running through C program code and executing it line-by-line A set of instructions for a specific central processing unit (CPU), designed to be use by a computer without being translated 20 D) Assembler Identify the type of programming language based on the statement below. A) Machine Language B) Assembly Language C) High Level Language D) Program Language Identify the programmer terminology. A) A person who writes the program B) A process of designing or creating a program C) A set of step-by-step instructions that directs a computer to perform a specific task D) A set of rules and reserved words that can be used to tell a computer the operation that is required to be done Select the translator based on the statement below. A) Program B) Compiler C) Interpreter
D) iii and iv Identify the translator used to translate C++ programming language. A) Compiler B) Interpreter C) Assembler D) Translator Choose the term related to the statement below. A) Compiler B) Converter C) Assembler D) Interpreter Select the disadvantages of Machine Language. i. Result-oriented where they emphasize on what instead of how. ii. The computer processes the instructions in machine language very quickly. iii. Programmers must have knowledge of the machine hardware and its configuration. iv. Programmer needs to remember a number of binary codes to write machine language programs. A) i and ii B) ii and iii C) ii and iv QUIZ! Time A program which is used to convert assembly language to machine language 4 5 6 Answ er: 4. A 5. C 6. D 21
State the programming language based on the generation in table below. Answer : List TWO (2) characteristics of second generation language. Answer : QUIZ! Time Generation Programming Language i. First generation ii. Second generation iii. Third generation 7 8 22
Chapter 2 PROBLEM SOLVING METHODS 2.1 PROGRAMMING LIFE CYCLE 2.1.1 Explain the phases in Programming Life Cycle (PLC) with examples. a. Specify the problem b. Analyze the problem c. Design the algorithm to solve the problem d. Implement the algorithm e. Test and verify the completed program f. Maintain and update the program g. Documentation 23
Programming Life Cycle A framework or discipline, which uses certain techniques needed in computer programming development 24
Phases in Programming Life Cycle SPECIFY THE PROBLEM ANALYZE THE PROBLEM DESIGN THE ALGORITHM IMPLEMENT THE ALGORITHM TEST AND VERIFY MAINTAIN AND UPDATE THE PROGRAM DOCUMENTATION 25
PHASE 1 – SPECIFY THE PROBLEM - To find the need Studies the problem. Investigate the need for possible software automation in the given system. Specific recommendations for the problem. Purposes: - To define the problem that needs to be solved - To identify the operators - To identify the formula that needs to be used 26 Phases in Programming Life Cycle
Purpose: To describe in detail a solution to a problem and information needed in solving the problem. How? - Study and understand the problem. - Identify: The needed input. The required output. The needed process. For example: scientific formula or particular theories. PHASE 2: ANALYZE THE PROBLEM 27 Phases in Programming Life Cycle
PHASE 3: DESIGN THE ALGORITHM 1. Algorithm Definition: It is a framework or flow that shows the steps in problem solving. Methods to design a program: 2. Flowchart 3. Pseudo code 28 Phases in Programming Life Cycle
PHASE 4: IMPLEMENT THE ALGORITHM Definition: Writing problem solving into certain programming language such as C, COBOL and others. Problem solving: Instructions before it is coded into programming language. Purpose: To produce a program to develop a system. 29 Phases in Programming Life Cycle
PHASE 5: TEST AND VERIFY THE PROGRAM Testing: Using a set of data to discover errors and to ensure accuracy of the program. Debug: Debugging is a process of identifying and correcting error. Three types of error: - Syntax error (grammatical error) - Runtime error - Logic error 30 Phases in Programming Life Cycle
31 Phases in Programming Life Cycle Occurs when the rules of programming language are not applied. Correction is done during the program coding. The bug can be traced during the compilation. Also known as compile-time error. Must be corrected before executing and testing the program. Syntax error (grammatical error) Occur when a program with no syntax error asks the computer to do something that the computer is unable to reliably do. Example: Runtime error - trying to divide by a variable that contains a value zero - trying to open a file that does not exists Cannot be traced by compiler. Corrected during the problem solving process. Example output for average is 4, but when it runs, the output is 2. Logic error Types of Error
PHASE 6: MAINTAIN AND UPDATE THE PROGRAM 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. Adjustment – Adding needs to new system. Repair – For example: An old system cannot update the new data. Updating – Update the database. Activity that verifies whether the operational system is performing as planned or an activity to modify the system to meet the current requirement. The process of changing a system after it has been applied to maintain its ability. The changes may involve simple changes such as error correcting. How to maintenance? 32 Phases in Programming Life Cycle
PHASE 7: DOCUMENTATION Description of the program. Specification of program requirement. Program design such as pseudo code and flowchart. List of program and comments (to explain about the program). Test results. User’s manual book. Program capabilities and limitation. Definition: A written or graphical report of the steps taken during the development of a program. Purpose: It will be useful in the future, in case of modification or maintenance. Content of documentation: 33 Phases in Programming Life Cycle
2.2 PROBLEM SOLVING CONCEPT Chapter 2 PROBLEM SOLVING METHODS 2.2.1 Define input, process and output. 2.2.2 List the steps in problem solving. 2.2.3 Identify the input, process and output based on problem statement. 2.2.4 Illustrate the problem solving concept using IPO chart based on the problem statement. 34
INPUT Refers to the process of entering data, program and instructions into the computer system using input devices. PROCESS Computer processes raw data into usable information to be used by user. Data processing 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 an understandable form before being displayed or printed. 35 Problem Solving Concept
Input: Card number, ATM pin number, type of transaction, type of account and amount of money to withdraw. Process: Process to identify card number, valid pin number, type of transaction, type of account and deducts the withdrawal from the user’s account. Output: Receipt will show balance in user’s account and money withdrawn. Flow of ATM Program 36 Problem Solving Concept Example 1
PROBLEM: Identify the input, process and output in calculating the average of three numbers. Input Process Output Number1, Number2, Number3 Average = (Number1 + Number2 + Number3) / 3 Average Input Process Output Radius Area = 3.14 * Radius * Radius Area PROBLEM: Identify the input, process and output to calculate the area of a circle. 37 Problem Solving Concept Example 2 Example 3
Chapter 2 PROBLEM SOLVING METHODS 2.3 DIFFERENT TYPES AND PATTERNS IN ALGORITHMS 2.3.1 Describe logic. 2.3.2 Relate logic with algorithm. 2.3.3 Define algorithm. 2.3.4 Describe the different patterns (construct) in algorithm: Sequential, Conditional, Iterational. 2.3.5 Write an algorithm. 38
LOGIC The word, or what is spoken, (but coming to mean thought or reason) is generally held to consist of the systematic study of the form of arguments. An algorithm can be regarded as consisting of a logic component, which specifies the knowledge to be used in solving problems. The logic component determines the meaning of the algorithm. LOGIC AND ALGORITHM 39 Logic and Algorithm
Algorithm is a sequence of instructions to solve a problem, written in human language, and it can solve any problems when it is used with the correct procedure. Example: 1. Get Input 3 numbers 2. Total up the 3 numbers using formula: Total = number1 + number2 + number3 3. Calculate average 3 numberS using formula: Average = Total/3 4. Display Total, Average 40 Algorithm
4 Essential Properties of an Algorithm: Each step of an algorithm must be exact An algorithm must terminate An algorithm must be effective An algorithm must be general - must be precisely and unambiguously described, so that there remains no uncertainty. - If the program does not stop when executed, we will not be able to get any result from it. - must provide the correct answer to the problem. - it must solve every instance of the problem. 41 Algorithm