Pseudocode Notes (1).pdf
Document Details
Beaconhouse School System
Tags
Related
- GE8151- PROBLEM SOLVING AND PYTHON PROGRAMMING - Question Bank PDF
- CPRO1 Introduction to Computer Programming PDF
- Introduction to Programming and Problem Solving (CS101) Lecture 3 PDF
- DS SEM-1, UNIT-1 PDF
- Fundamentals of Programming - Algorithms and Flowcharts PDF
- Algorithms and Flowcharts Lecture Notes PDF
Full Transcript
Beaconhouse School System Pseudocode There are 3 programming/Pseudocode constructs: 1. Sequence: it refers that instructions should be executed one after another. 2. Selection: This construct is used to make a decision in choosing an...
Beaconhouse School System Pseudocode There are 3 programming/Pseudocode constructs: 1. Sequence: it refers that instructions should be executed one after another. 2. Selection: This construct is used to make a decision in choosing an option from many available options on the basis of a condition. So, if a condition is true then one option would be chosen while if a condition is false then another option will be chosen. 3. Repetition: this construct is used to repeat a block of code as per the given condition. Algorithm is step by step solution to a given problem. It is written in plain English statements. Algorithm is usually transformed into pseudocode or program flowchart. Once the algorithm is tested in pseudocode or program flowchart then finally it is written in a specific programming language. Pseudocode is a false code which consists of plain English statements, mathematical notations and keywords that are commonly found in high level languages. It does not follow strict rules and style of any particular programming language. Pseudocode is used for learning programming concept and to describe ideas before coding begins. Arithmetic Operators: In pseudocode arithmetic operators are used to perform arithmetic operations. These operations are listed below. Arithmetic Operators Meaning + Addition - Subtraction * Multiplication / Division ^ Show Power of a number Comparison Operators: these operators are used to compare different values. Operators Comparison > Greater than < Less than = Equal >= Greater than or equal = 0 DO Total = Total + num INPUT num ENDWHILE PRINT “ Total is = “; Total Example 8: Write pseudocode that will take numbers input, add them and calculates the average while the input number is greater than or equal to 0. Print the average of all input numbers. Solution: Total = 0 Tahir Ali BMTL Boys Branch 9 Beaconhouse School System Count = 1 PRINT “Input Number “ INPUT num WHILE num >=0 DO Total = Total + num INPUT num Count = Count + 1 ENDWHILE Avg = Total /Count PRINT “The average is = “, Avg Note: Although there is a difference of operation between FOR…TO…NEXT loop and WHILE…DO…ENDWHILE Loop but still there is a possible way to rewrite a code written in FOR…TO…NEXT loop into WHILE…DO…ENDWHILE loop. Example 9: Rewrite the pseudocode given below by using WHILE…DO...ENDWHILE loop. Total = 0 FOR count = 1 TO 10 PRINT “Enter Number” INPUT num Total = Total + num NEXT Tahir Ali BMTL Boys Branch 10 Beaconhouse School System Avg = Total / 10 PRINT “Average of 10 Numbers is = “, Total Solution: Total = 0 Count = 1 WHILE Count = 90 AND marks = 80 AND marks < 90 THEN PRINT “ The Grade is A “ ELSE IF marks >= 70 AND marks < 80 THEN PRINT “ The Grade is B “ ELSE IF marks >= 60 AND marks < 70 THEN Tahir Ali BMTL Boys Branch 16 Beaconhouse School System PRINT “ The Grade is C “ ELSE IF marks >= 0 AND marks < 60 THEN PRINT “ The Grade is F “ ELSE PRINT “ Invalid Marks “ ENDIF ENDIF ENDIF ENDIF ENDIF Note: Each IF statement and ELSE IF statement must have an ENDIF statement to terminate. Example 17: Write pseudocode that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red. If the number is between 20 and 30, write the word green. If it is any other number, write that it is not a correct color option. Solution : (Solve at your own) Tahir Ali BMTL Boys Branch 17 Beaconhouse School System Example 18: Write down pseudocode that will take three numbers as input and tells a. The Largest Number b. The Smallest Number Solution: (a) PRINT “Enter three numbers “ INPUT num1 INPUT num2 INPUT num3 IF num1 > num2 AND num1 > num3 THEN PRINT “ The largest number is “, num1 ELSE IF num2 > num1 AND num2 > num3 THEN PRINT “ The largest number is “, num2 ELSE PRINT “ The largest number is “, num3 ENDIF ENDIF Solution: (B) Solve at you own Tahir Ali BMTL Boys Branch 18 Beaconhouse School System CASE … OF… OTHERWISE …. ENDCASE Statement: When there are too many available routes in an algorithm / pseudocode then it requires too many IF… THEN… ELSE … ENDIF statements to make a selection among these routes which is not an easy and it makes pseudocode difficult to manage. To overcome this issue CASE… OF…. OTHERWISE Statement is used. In Short, CASE … OF… OTHERWISE … ENDCASE is used in place of IF …. THEN … ELSE … ENDIF statement when we have to make a selection of route from too many available routes. Example 19: Write down pseudocode that will take a number as input (from 1 to 7) and print the day name for corresponding number e.g 1 for Monday 2 for Tuesday and so on. Solution: PRINT “Enter a number to display the day name. “ INPUT daynum CASE daynum OF 1: dayname = “Monday” 2: dayname = “Tuesday” 3: dayname = “Wednesday” 4: dayname = “Thursday” 5: dayname = “Friday” 6: dayname = “Saturday” 7: dayname = “Sunday” OTHERWISE PRINT “Error” daynum = “unknown” Tahir Ali BMTL Boys Branch 19 Beaconhouse School System ENDCASE PRINT “The day is : “, dayname Example 20: Write a pseudocode that will take two numbers as input perform any of the basic operation (+, -, *, /) as per user requirements. Solution: PRINT “Enter two numbers” INPUT num1 INPUT num2 PRINT “Enter the operation” INPUT Operator CASE Operator OF + : Ans = num1 + num2 - : Ans = num1 - num2 / : Ans = num1 / num2 *: Ans = num1 * num2 OTHERWISE PRINT “Invalid Operator” Ans = “Invalid” ENDCASE PRINT “Answer is: “, Ans Tahir Ali BMTL Boys Branch 20 Beaconhouse School System Practice Questions (Past Paper): In CAIE exam, mostly the pseudocodes are asked that can select maximum from many values, minimum from many values, can calculate average of given values, can count the number of digits in a given input or number or number and then performing various operations on its basis. Question 1: a. Write an algorithm, using pseudocode or a flowchart, which a. Inputs a set of positive numbers (which end with -1) b. Outputs the average (mean) value of the input numbers c. Outputs the value of the largest (highest) number input. b. Writhe an algorithm, using pseudocode or a flowchart, which a. Inputs a whole number (which is > 0) b. Calculates the number of digits in the number c. Outputs the number of digits and the original number (E.g. 147 would give an output of 3, 147) Solution: (a) Highest = -100; total = 0; count = 0 (1 mark) initialize values highest can’t be 0 INPUT number (1 mark) inputs in correct place WHILE number -1 DO (1 mark) loop until -1 is input Total = total + number (1 mark) calculate number total Count = count + 1 and count numbers input If number > highest then highest then highest = number (1 mark) highest INPUT number ENDWHILE Average = total / count (1 mark) calculate average values PRINT average, highest and output average and highest value Tahir Ali BMTL Boys Branch 21 Beaconhouse School System Solution: (b) D=0 (1 mark) initialize value INPUT number (1 mark) input number and set variable to this T = number Number REPEAT (1 mark) correct loop T = T / 10 (1 mark) method to find number of digits D=D+1 (1 mark) counting number of digits UNTIL T < 1 PRINT number, D (1 mark) correct output outside the loop (Note: there are other ways of finding number of digits e.g. IF number > 0 THEN d = 1 ELSE IF number > 9 THEN d = 2 ………………………………………………………………………….. ELSE IF number > 999999 THEN d = 7 etc) Question 2: Write a program that should get 50 values from user, Output how many values are greater than 100. Output total of those values that are less than 50. Solution: FOR I = 1 TO 50 INPUT num IF num > 100 THEN C = C + 1 Tahir Ali BMTL Boys Branch 22 Beaconhouse School System IF num < 75 THEN sum = sum + num NEXT i PRINT “Greater then 100”, c PRINT “sum of less than 75”, sum Question 3: Write a pseudocode that prints the sale of a shopkeeper for a week. Solution: FOR I = 1 TO 7 INPUT Customer FOR b = 1 TO Customer INPUT sale Total = total + sale NEXT b PRINT “Total sale for day “, I , “=”, total NEXT i Question 4: Write a pseudocode that print table of 7 till 100, using REPEAT UNTIL LOOP. I=0 REPEAT PRINT i I = I + 7 UNTIL (I < 100) Question 5: Write a pseudocode that print table of 7, using FOR TO NEXT LOOP. Tahir Ali BMTL Boys Branch 23 Beaconhouse School System In your program ask from user starting value of table and ending value of table through INPUT command. INPUT “Table number “, t INPUT “starting value”, st INPUT “Ending value”, ev FOR I = st TO ev Ans = t * st PRINT t * st “ = “ ans NEXT i Tahir Ali BMTL Boys Branch 24 Beaconhouse School System Past Papers Question Q. 4 a 0478 w 18 QP 23 This is a section of program code. 1 Total = 100.00 2 PRINT 'Enter the height of each member of your class, one at a time, when prompted' 3 FOR Count = 1 TO 30 4 PRINT 'Enter a height in metres' 5 INPUT Height 6 Total = Total + Height 7 PRINT Total / 30 8 Count = Count + 1 9 NEXT Count (a) There are three errors in this code. State the line numbers that contain the errors and describe how to correct each error. Error 1............................................................................................................................................................................................................................................................................................................................................................................................................................................. Error 2............................................................................................................................................................................................................................................................................................................................................................................................................................................. Error 3........................................................................................................................................................................................................................................................................................................................................................................................................................................... (b) State the purpose of this program...................................................................................................................................................................................................................................................................................................................................................................................................................................................... Q. 5 a 0478 w 18 QP 23 Tahir Ali BMTL Boys Branch 25 Beaconhouse School System The algorithm allows a number to be entered. It then calculates and outputs the next number in the mathematical series. Fib = 1 Prev2 = 0 Prev1 = 1 INPUT Number IF Number = 0 THEN Fib = 0 ENDIF WHILE Number > 2 Fib = Prev2 + Prev1 Prev2 = Prev1 Prev1 = Fib Number = Number - 1 ENDWHILE OUTPUT Fib (a) Complete the trace table for the input data: 7 Fib Prev2 Prev1 Number OUTPUT (b) Complete the trace table for the input data: 2 Fib Prev2 Prev1 Number OUTPUT Tahir Ali BMTL Boys Branch 26 Beaconhouse School System 2210 w 18 QP 22 Q. 2 (a) Write an algorithm, using pseudocode, to input three different numbers, multiply the two larger numbers together and output the result. Use the variables: Number1, Number2 and Number3 for your numbers and Answer for your result. (b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose each set. Test data set 1.......................................................................................................................... Reason........................................................................................................................................................................................................................................................................................ Test data set 2.......................................................................................................................... Reason................................................................................................................................................................................................................................................................................... 0478 w 18 QP 21 Q. 4 An algorithm is written in pseudocode: Total 0 FOR Count 1 TO 50 INPUT Num Total Total + Num NEXT Count OUTPUT Total (a) Describe the purpose of the algorithm.................................................................................................................................................... (b) Re-write the algorithm in pseudocode using a different type of loop.................................................................................................................................................... 0478 S 18 QP 23 Q. 3 This section of program code reads the contents of the array, totals the numbers and prints out the sum and average of the numbers. Assume the array is full. Complete the four missing items by writing them in the spaces provided in this code. 1 Numbers[1:30] 2 Total = 0 3.............................................. = 0 Tahir Ali BMTL Boys Branch 27 Beaconhouse School System 4 FOR Count = 1 TO......................................................... 5 Number = Numbers[Count] 6 Total =......................................................... + Number 7 Counter = Counter + 1 8.................................................................................................. Count 9 PRINT ′The sum of the numbers you entered is ′, Number 10 PRINT ′The average of the numbers you entered is ′, Number / Counter Q. 4 An algorithm is written in pseudocode: INPUT Number IF Number > 100 THEN OUTPUT ″The number is too large″ ELSE OUTPUT ″The number is acceptable″ ENDIF (a) Describe the purpose of the algorithm. (b) (i) The algorithm only allows one attempt at inputting an acceptable value. State how you would change the algorithm so that it continues until a suitable input is supplied. (ii) Re-write the algorithm in full, using pseudocode, to implement your answer to part (b)(i). 0478 S 18 QP 22 Q. 3 This pseudocode algorithm inputs two non-zero numbers and a sign, and then performs the calculation shown by the sign. An input of zero for the first number terminates the process. INPUT Number1, Number2, Sign WHILE Number1 0 IF Sign = '+' THEN Answer Number1 + Number2 ENDIF IF Sign = '-' THEN Answer Number1 - Number2 ENDIF IF Sign = '*' THEN Answer Number1 * Number2 ENDIF IF Sign = '/' THEN Answer Number1 / Number2 ENDIF IF Sign '/' AND Sign '*' AND Sign '-' AND Sign '+' THEN Answer 0 ENDIF IF Answer 0 THEN OUTPUT Answer ENDIF INPUT Number1, Number2, Sign ENDWHILE (a) Complete the trace table for the input data: 5, 7, +, 6, 2, –, 4, 3, *, 7, 8, ?, 0, 0, / Number 1 Number 2 Sign Answer OUTPUT Tahir Ali BMTL Boys Branch 28 Beaconhouse School System (b) Show how you could improve the algorithm written in pseudocode by writing an alternative type of conditional statement in pseudocode. 0478 S 18 QP 21 Q. 2 (a) Write an algorithm to input 1000 numbers. Count how many numbers are positive and how many numbers are zero. Then output the results. Use either pseudocode or a flowchart. (b) Give one change you could make to your algorithm to ensure initial testing is more manageable. Q. 3 The global trade item number (GTIN-8) barcode has seven digits and a check digit. This pseudocode algorithm inputs seven digits and calculates the eighth digit, then outputs the GTIN-8. DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2. MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3. FOR Count 1 TO 7 INPUT Number Digit(Count) Number NEXT Sum (Digit(1)+Digit(3)+Digit(5)+Digit(7))*3+Digit(2)+Digit(4)+Digit(6) IF MOD(Sum,10) 0 THEN Digit(8) DIV(Sum,10)*10 + 10 - Sum ELSE Digit(8) 0 ENDIF OUTPUT "GTIN-8" FOR Count 1 TO 8 OUTPUT Digit(Count) NEXT (a) Complete the trace table for the input data: 5, 7, 0, 1, 2, 3, 4 Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT Complete the trace table for the input data: 4, 3, 1, 0, 2, 3, 1 Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT Tahir Ali BMTL Boys Branch 29 Beaconhouse School System Q. 3 (b) Explain how you would change the algorithm to input eight digits (seven digits and the check digit) and output if the check digit entered is correct or not. 0478 W 17 QP 23 Section B Q. 2 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It checks that the numbers are in the correct range, and stores them in an array. It counts how many of the numbers are larger than 500 and then outputs the result when the program is finished. 1 Count = 0 2 FOR Index = 1 TO 80 3 INPUT 'Enter a number between 100 and 1000', Number 4 WHILE Number = 99 AND Number = 1001 5 INPUT 'This is incorrect, please try again', Number 6 ENDWHILE 7 Num = Number 8 IF Number > 500 THEN Count = Count + 1 9 UNTIL Index = 80 10 PRINT Index 11 PRINT ' numbers were larger than 500' There are four lines of code that contain errors. State the line number for each error and write the correct code for that line. Error 1 Line Number............................. Correct Code................................................................................................................................. Error 2 Line Number............................. Correct Code................................................................................................................................. Error 3 Line Number............................. Correct Code................................................................................................................................. Error 4 Line Number............................. Correct Code............................................................................................................................ 0478 W 17 QP 22 Q. 2 Write an algorithm using either pseudocode or a flowchart, to: input a positive integer use this value to set up how many other numbers are to be input input these numbers calculate and output the total and the average of these numbers. Tahir Ali BMTL Boys Branch 30 Beaconhouse School System 0478 S 17 QP 23 Q. 2 This section of program code asks for 50 numbers to be entered. The total and average of the numbers are calculated. 1 Total = 0 2 Counter = 50 3 PRINT 'When prompted, enter 50 numbers, one at a time' 4 REPEAT 5 PRINT 'Enter a number' 6 INPUT Number 7 Total + Number = Total 8 Number = Number + 1 9 UNTIL Counter = 50 10 Average = Number * Counter 11 PRINT 'The average of the numbers you entered is ', Average There are four errors in this code. State the line number for each error and write the correct code for that line. Error 1 Line number............................. Correct code................................................................................................................................... Error 2 Line number............................. Correct code................................................................................................................................... Error 3 Line number............................. Correct code................................................................................................................................... Error 4 Line number............................. Correct code.............................................................................................................................. Q. 5 (a) Describe the purpose of each statement in this algorithm. FOR I = 1 TO 300 INPUT Name[I] NEXT I............................................................................................................................................... (b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have used................................................................................................................................................ (c) Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The algorithm should prompt for the input and output an error message if the number is outside this range.................................................................................................................................................. 0478 S 17 QP 22 Section B Tahir Ali BMTL Boys Branch 31 Beaconhouse School System Q. 2 (a) Write an algorithm to input three different numbers, and then output the largest number. Use either pseudocode or a flowchart. (b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose each set. Test data set 1........................................................................................................................... Reason...................................................................................................................................... Test data set 2........................................................................................................................... Reason................................................................................................................................... Q.4 An algorithm has been written in pseudocode to input 100 numbers and print out the sum. A REPEAT … UNTIL loop has been used. Count = 0 Sum = 0 REPEAT INPUT Number Sum = Sum + Number Count = Count + 1 UNTIL Count > 100 PRINT Sum (a) Find the error in the pseudocode and suggest a correction. Error........................................................................................................................................... Correction................................................................................................................................ (b) Rewrite the correct algorithm using a more suitable loop structure................................................................................................................................................ Tahir Ali BMTL Boys Branch 32