The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

PSPD GUIDE BOOK is written specifically for Polytechnic students who are studying Problem Solving and Program Design. The main purpose of creating and publishing this book is to introduce the guidance of the techniques in Problem Solving and Program Design. The concepts learned in this book can be applied to many real life problems which can be solved by writing the programming codes using C++ languages and also can be a useful guide for undergraduates.
This book is designed with the infographic concepts with the intention of providing the guidance not only for the students but also for the educators too. This book provides quick information and guidance for problem solving and program design concepts with various examples, activities and exercises.

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Penerbit PSIS, 2023-11-20 04:01:20

GUIDE BOOK PSPD, PROBLEM SOLVING AND PROGRAM DESIGN

PSPD GUIDE BOOK is written specifically for Polytechnic students who are studying Problem Solving and Program Design. The main purpose of creating and publishing this book is to introduce the guidance of the techniques in Problem Solving and Program Design. The concepts learned in this book can be applied to many real life problems which can be solved by writing the programming codes using C++ languages and also can be a useful guide for undergraduates.
This book is designed with the infographic concepts with the intention of providing the guidance not only for the students but also for the educators too. This book provides quick information and guidance for problem solving and program design concepts with various examples, activities and exercises.

Keywords: GUIDE BOOK PSPD

Control Structures Repetition Control Structure used to repeat a block of code repeatedly based on a specified condition. three common types of loops: for loop, while loop, and dowhile loop. create programs that perform complex tasks, make decisions, and repeat actions as needed. for loop useful when we know the number of iterations to perform beforehand. consists of three parts: initialization, condition, and counter (increment/decrement). initializes variables and is executed only once Initialization Condition Counter if true, the body of for loop is executed if false, the for loop is terminated updates the value of initialized variables and again checks the condition 142


End Condition Control Structures } for (initialization; condition; counter) { // code to be executed in each iteration Syntax Flowchart True False InitialSization Start loop stSatement counterS update 143 Repetition Control Structure


In this example, the program will print numbers from 1 to 5 Example Start for (int i = 1; i <= 10; i++) { Display i; } End End i <= 5 S Start S S Pseudo code Flowchart True False i = 1 Display i i++ S Iteration Variable i<=5 Output How It Works? 1st 2nd 3rd 4th 5th 6th i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 true true true true true false i++ 1 2 3 4 5 the loop is terminated i is increased to 2 i is increased to 3 i is increased to 4 i is increased to 5 i is increased to 6 144 Control Structures Repetition Control Structure


while loop used when the number of iterations is not known beforehand, and the loop continues until a specific condition becomes false. Condition A while loop evaluates the condition. If true, the code inside the while loop is executed. The condition is evaluated again. This process continues until the condition is false. When the condition false, the loop terminates. 145 Control Structures Repetition Control Structure


Flowchart } while (condition) { // code to be executed in each iteration Syntax End Condition Start S True False loop statement 146 Control Structures Repetition Control Structure


In this example, the program will print numbers from 1 to 5 Example Start Set i = 1; while (i <= 5) { Display i; ++i; } End Pseudo code Flowchart S Iteration Variable i<=5 Output How It Works? 1st 2nd 3rd 4th 5th 6th i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 true true true true true false i++ 1 2 3 4 5 the loop is terminated i is increased to 2 i is increased to 3 i is increased to 4 i is increased to 5 i is increased to 6 End i <= 5 S Start S S True False i = 1 Display i i++ 147 Control Structures Repetition Control Structure


do..while loop do...while loop is a variant of the while loop with one important difference: "the body of do...while loop is executed once before the condition is checked." do..statement The body of the loop is executed at first. Then the condition is evaluated. If true, the body of the loop inside the do statement is executed again. The condition is evaluated once again. If true, the body of the loop inside the do statement is executed again. This process continues until the condition evaluates to false. Then the loop terminates. Condition 148 Control Structures Repetition Control Structure


Flowchart End Condition Start True False do..while Sloop body while (condition); do { // code block to be executed } Syntax 149 Control Structures Repetition Control Structure


In this example, the program will print numbers from 1 to 5 Display i; ++i; } while (i <= 5); } Example Start Set i = 1; do { End Pseudo code Flowchart S Iteration Variable Output i<=5 How It Works? 1nd 2rd 3th 4th i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 1 2 3 4 5 i++ true true true true false the loop is terminated i is increased to 2 i is increased to 3 i is increased to 4 i is increased to 5 i is increased to 6 End i <= 5 S Start S S True False i = 1 Display i i++ 150 Control Structures Repetition Control Structure


Based on the given algorithm, prepare a pseudo code and flowchart for for, while and do..while repetition control structures. LET'S CHECK! LET'S CHECK! Algorithm Initialize Counter = 1; Average = 0; Sum = 0; Input Number. Calculate Sum using formula: 1. 2. 3. Sum = Sum + Number 4. Update Counter using formula: Counter = Counter + 1 5. Compare whether Counter is greater than 5. If yes, go to step 6. If no, go to step 2. 6. Calculate Average of numbers using formula: Average = Sum / 5 7. Display Average. Start Set i=1, Sum = 0, Average = 0; for (i=1; i<=5; i++) { Input Number Sum = Sum + Number } Average = Sum / 5; Output Average; End Pseudo code - for loop 151


Based on the given algorithm, prepare a pseudo code and flowchart for for, while and do..while repetition control structures. LET'S CHECK! LET'S CHECK! Start Set i=1, Sum = 0, Average = 0; i = i + 1; } Average = Sum / 5; Output Average; End while (i <= 5) { Input Number; Total = Total + Number; Pseudo code - while loop Start Set i=1, Sum = 0, Average = 0; Output Average; End do { Input Number; Sum = Sum + Number; i = i + 1; } while (i <= 5) Average = Sum / 5; Pseudo code - do..while loop 152


Based on the given algorithm, prepare a pseudo code and flowchart for for, while and do..while repetition control structures. LET'S CHECK! LET'S CHECK! Flowchart - for loop End i <= 5 S Start S True False i = 1 Display Average SumS= 0 AveragSe = 0 InsertSNumber Sum = SumS + Number i = iS + 1 Average =S Sum / 5 153


Based on the given algorithm, prepare a pseudo code and flowchart for for, while and do..while repetition control structures. LET'S CHECK! LET'S CHECK! Flowchart - while loop End i <= 5 S Start S True False i = 1 Display Average SumS= 0 AveragSe = 0 InsertSNumber Sum = SumS + Number i = iS + 1 Average =S Sum / 5 154


S Start S S S S S Based on the given algorithm, prepare a pseudo code and flowchart for for, while and do..while repetition control structures. LET'S CHECK! LET'S CHECK! Flowchart - do..while loop i = 1 Sum = 0 Average = 0 Insert Number Sum = Sum + Number i = i + 1 i <= 5 True False End Display SAverage Average =S Sum / 5 155


Write a pseudo code and design a flowchart based on the following problem. Bumi Hijau Manufacturing hires temporary workers and pays them an hourly wage. The company employs 16 temporary workers who worked for varying hours. The hourly wage is RM15. Calculate the net income earned by each temporary worker. QUIZ! Time Pseudo code Flowchart 156


Write a pseudo code and design a flowchart based on the following problem. Isyq Media Berhad has 20 sales employees. Each salesperson receives a basic salary plus a sales commission based on their sales performance. The sales commission is 5% of their total sales. Calculate the net income earned by each sales employee after deducting 9% EPF from the total earnings. QUIZ! Time Pseudo code Flowchart 157


Write a pseudo code and design a flowchart using For, While and Do...while control structures based on the following algorithm. QUIZ! Time Algorithm Set x = 1 Compare whether counter x is less than or equal to 50. If yes, go to step 3. If no, go to step 7. Input name, matric_no, mark_theory, mark_practical. Calculate total_marks = mark_theory + mark_practical. Check if total_marks is greater than or equal to 50. 5.1 If yes, display "Pass" with the name and roll_num. 5.2 If no, display "Fail" with the name and roll_num. Increment counter: x = x + 1. Repeat step 2. End Exam Grading 1. 2. 3. 4. 5. 6. 7. 8. 158


QUIZ! Time Pseudo code Flowchart for loop 159


QUIZ! Time Pseudo code Flowchart do..while loop 160


Write a pseudo code and design a flowchart using For, While and Do...while control structures based on the following algorithm. QUIZ! Time Algorithm Set x = 1 Compare whether counter x is less than or equal to 10. If yes, go to step 3. If no, go to step 7. Input product_name, quantity, unit_price. Calculate total_price = quantity * unit_price. Check if quantity is greater than or equal to 10. 5.1 If yes, apply a 10% discount to the total_price. 5.2 If no, apply a 5% discount to the total_price. Display product_name, quantity, unit_price, and the discounted total_price. Increment counter: x = x + 1. Repeat step 2. End Product Discount 1. 2. 3. 4. 5. 6. 7. 8. 9. 161


QUIZ! Time Pseudo code Flowchart for loop 162


QUIZ! Time Pseudo code Flowchart while loop 163


QUIZ! Time Pseudo code Flowchart do..while loop 164


4.1.1 Explain the elements of programming language: a. comment b. preprocessor directive c. standard header file d. main function e. reserved word (standard and userdefined identifiers) f. identifiers (constants and variables) g. special symbol h. punctuation i. statements j. blocks 4.1 DESCRIBE BASIC PROGRAMMING LANGUAGE Chapter 4 BASIC PROGRAMMING CODES 165


Help to understand the program. Eg: 1. // xxxxxxx 2. /* xxxxxxx */ a. Comment A library or special instructions to the compiler. Eg : #include b. Preprocessor Directive To hold declarations for other files to use Eg : #include <iostream> c. Standard Header file Identify the start of the program. Eg : int main ( ) d. Main Function A Special word reserved by a program Eg: return, cout, float e. Reserved Word 166 Elements of Programming Language


The name of the variables, constant and function in a program. f. Identifiers To represent special symbol. Eg : \n, \t, { }, * g. Special Symbol Every statement in C++ must end with semi colon. Eg : return 0; h. Punctuation Eg : cout <<”Welcome to C++ Programming”\n; i.Statements Begin with { and closing with } Eg : { xxxxxxxxx Xxxxxxx} j. Blocks 167 Elements of Programming Language


/* The function of this program is to print the average for 30 students weight */ #include <iostream> using namespace std; int main ( ) { int weight, sum, i , average; sum = 0, i =1; for (i=1; i<=30; i++) { cout <<”input weight: “; cin >> weight; sum = sum + weight; } average = sum/30; cout << “Average of 30 weight =”<<average; return 0; } Elements Comment Preprocessor Directive Standard Header file Main Function Reserved Word Identifiers Special Symbol Punctuation Statements Blocks 168 Elements of Programming Language


Based on the Program Coding below, write the program code for the element of the programming : LET'S CHECK! LET'S CHECK! Program Code /* The function of this program is to print out welcome and name only */ #include <iostream> using namespace std; int main ( ) { cout << “Hi, I’m Adam Harris\n ”; cout << “Welcome to C++ Programming World\n”; return 0; } h. Comment : a. Preprocessor Directive : b. Punctuation : c. Header File : d. Function Main : e. Statement : f. Reserved Word : g. Block : 169


/* The function of this program is to print out welcome and name only */ #include <iostream> using namespace std; int main ( ) { cout << “Hi, I’m Adam Harris\n ”; cout << “Welcome to C++ Programming World\n”; return 0; } Based on the Program Coding below, write the program code for the element of the programming : LET'S CHECK! LET'S CHECK! Program Code welcome and name only */ a. #include b. return 0; c. iostream d. int main ( ) e. cout << “Hi, I’m Adam Harris\n ”; f. cout g. { cout << “Hi, I’m Adam Harris\n ” cout << “Welcome to C++ Programming World\n”; return 0; } h. /* The function of this program is to print out Answer 170


Label of the programming elements based on the program coding below : LET'S CHECK! LET'S CHECK! Program Code #include <iostream> using namespace std; int main( ) { float width, length, height, volume; xxxxxxxxxx; xxxxxxxxxxx; xxxxxxxxxxx; } A B C 171


Label of the programming elements based on the program coding below : LET'S CHECK! LET'S CHECK! Program Code #include <iostream> using namespace std; int main( ) { float width, length, height, volume; xxxxxxxxxx; xxxxxxxxxxx; xxxxxxxxxxx; } C – Identifiers A – Header File B – Main Function A B C Answer 172


Chapter 4 BASIC PROGRAMMING CODES 4.2.1 Describe the standards and best practices in writing program codes. 4.2.2 Discuss the disadvantages of not following standards and best practices while writing codes. 4.2 IDENTIFY STANDARDS AND BEST PRACTICES 173


Write visible clear code Standards and Best Practices in Writing Program Codes Follow the current industry method Request a review or test run of your code after you are done Put comments in your codes Name your conventions Prioritize daily backups Debugging Best Coding Practices 174


Decrease the efficiency throughout the software process Increase risk of project failure Maximum of complexity and not easy to maintain Will not improve the efficiency and productive in team working. Disadvantages of Not Following Standards & Best Practices While Writing Code 175


Chapter 4 BASIC PROGRAMMING CODES 4.3.1 Identify the steps in creating a program 4.3.2 Identify the data types 4.3.3 Declare variables and constants 4.3.4 Write a basic program codes based on simple problem 4.3.5 Analyze a program to identify input, process and output 4.3.6 Construct pseudo code and flowchart based on given programming codes 4.3 APPLY THE BASICS OF PROGRAMMING LANGUAGE 176


Steps in Creating a Program Write the source code (Ms Visual Studio) Compile the source code 1 2 177


Steps in Creating a Program If some errors occur, solve the errors (syntax & logical) Run the source code to get the output 3 4 178


Match the following data types to its description. 6 1 2 3 4 5 Basic Data Type Syntax in Source Code C++ Integer Float Double Character Data Types in a Program String int float double char string Boolean bool 179


Data Types in a Program no1, no2 and max_num are integer 180


Declare Variables and Constants Declare variable int number; Declare constant const int number = 10; Declaration of variable & constant 181


Problem Write a source code using Visual C++ to show the total of three marks of DFC10212 quizzes obtained by a student. Source Code Output Write a Basic Program Codes based on Simple Problem 182


Source Code Output Problem Write a source code using Visual C++ to show the total of three marks of DFC10212 quizzes obtained by a student. Answer Write a Basic Program Codes based on Simple Problem 183


Program Code Answer Example 1 Based on the simple program below, identify the input, process and output. #include <iostream> using namespace std; void main ( ) { float package, discount, newprice; cout << “Please key in the package : ”; cin >>package; discount = 20/100 * package; newprice = package – discount; cout << “The new price is : “, cout << newprice; } Input : package Process : discount = 20/100 * package newprice = package – discount Output : newprice Analyze a Program to Identify Input, Process and Output 184


Program Code Answer Example 2 Based on the simple program below, identify the input, process and output. Analyze a Program to Identify Input, Process and Output #include <iostream> using namespace std; int main ( ) { int studentmark; string status; cout << “Please key in the student mark : ”; cin >>studentmark; if studentmark >= 60 status = ‘Lulus’; else status = ‘ Cuba Lagi’; cout << status; } Input : studentmark Process : if studentmark >= 60 status = ‘Lulus’ else status = ‘ Cuba Lagi’ Output : status 185


Program Code Pseudo code Example 1 Based on the programming code given below, construct the pseudo code and draw the flowchart. Construct Pseudo code and Flowchart based on given Programming Code #include <iostream> using namespace std; int main ( ) { int n; for ( n=0; n<5;n++ ) cout << “Good Luck !”; return 0; } Start for ( n=0; n<5;n++ ) { Print “Good Luck !”; } End 186


Flowchart Example 1 Based on the programming code given below, construct the pseudo code and draw the flowchart. Construct Pseudo code and Flowchart based on given Programming Code 187


Program Code Pseudo code Example 2 The programming code below is to find the total of 5 numbers using while repetition. Based on the programming code given, contruct the pseudo code and draw the flowchart. Construct Pseudo code and Flowchart based on given Programming Code #include <iostream> using namespace std; void main ( ) { int number, i=1, total =0; while (i<=5) { cout << “Please key in the number : ”; cin >> number; total = total + number; i = i + 1; } cout << “The total of 5 numbers is : “, cout << total; } Start input number, i=1, total =0; while (i<=5) { input the number; total = total + number; i=1, total =0 i = i + 1; } Print total End 188


Flowchart Example 2 The programming code below is to find the total of 5 numbers using while repetition. Based on the programming code given, contruct the pseudo code and draw the flowchart. Construct Pseudo code and Flowchart based on given Programming Code 189


Write the correct declaration for the following variables in C++ statement. Variables Declaration in C++ a. QuizMark b. Blood_type c. Totalmark d. Student_name e. Average_height QUIZ! Time 190


Based on the program code given, identify the input, process and the output. Then, construct the pseudo code and draw the flowchart. QUIZ! Time Source code Input, process, output #include <iostream> using namespace std; void main ( ) { int weight, i=1, sum=0; while (i<=20) { cout << “Please key in the weight : ”; cin >> weight; sum = sum + weight; i = i + 1; } cout << “The sum of 20 weight is : “, cout << sum; } 191


Click to View FlipBook Version