0% found this document useful (0 votes)
30 views6 pages

Pseudocode and Algorithm Exercises

The document contains 6 pseudocode descriptions and algorithms. The summaries are: 1) An algorithm that uses a loop to output 50 names stored in an array. 2) A part of a satellite navigation system that allows inputting a destination and displays directions. 3) An algorithm that inputs numbers and outputs those >= 100, with errors to correct. 4) An algorithm that processes names in an array using a flag and count. 5) An algorithm that checks prices < $10, with test data explained. 6) A function that converts inches to cm using a parameter.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Pseudocode and Algorithm Exercises

The document contains 6 pseudocode descriptions and algorithms. The summaries are: 1) An algorithm that uses a loop to output 50 names stored in an array. 2) A part of a satellite navigation system that allows inputting a destination and displays directions. 3) An algorithm that inputs numbers and outputs those >= 100, with errors to correct. 4) An algorithm that processes names in an array using a flag and count. 5) An algorithm that checks prices < $10, with test data explained. 6) A function that converts inches to cm using a parameter.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

.

1 Four pseudocode descriptions and five pseudocode statements are shown.

(a) Draw a line to link each pseudocode description to the most appropriate pseudocode
statement.

Some pseudocode statements will not be used.

(b) Using a single loop, write an algorithm in pseudocode to output 50 names that have been
stored in the array, Name[]

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................
2 A satellite navigation system is an example of a computer system that is made up of sub-systems.

Part of a satellite navigation system:


• allows the user to enter details for a new destination or select a previously saved destination
• displays directions in the form of a visual map or as a list.

Draw a structure diagram for this part of the satellite navigation system.
3 An algorithm has been written in pseudocode to input some numbers. It only outputs any numbers
that are greater than or equal to 100. The number 999 is not output and stops the algorithm.

INPUT Number
WHILE Numbers <> 999 DO
IF Number > 100
THEN
OUTPUT Number
ENDIF
ENDWHILE
OUTPUT Number

(a) Identify the four errors in the pseudocode and suggest corrections.

Error 1 .......................................................................................................................................

Correction .................................................................................................................................

...................................................................................................................................................

Error 2 .......................................................................................................................................

Correction .................................................................................................................................

...................................................................................................................................................

Error 3 .......................................................................................................................................

Correction .................................................................................................................................

...................................................................................................................................................

Error 4 .......................................................................................................................................

Correction .................................................................................................................................

...................................................................................................................................................

(b) Write a pseudocode statement to change the corrected algorithm to output all numbers
between 100 and 200 inclusive.

You do not need to rewrite the whole algorithm

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

..............................................................................................................................................
4 This flowchart represents an algorithm.

(a) The array Name[1:4] used in the flowchart contains the following data:

Name[1] Name[2] Name[3] Name[4]


Jamal Amir Eve Tara
Complete the trace table using the data given in the array.

Flag Count Name[1] Name[2] Name[3] Name[4] Temp

Jamal Amir Eve Tara

(b) Describe what the algorithm represented by the flowchart is doing.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

..............................................................................................................................................
5 A programmer has written an algorithm to check that prices are less than $10.00

These values are used as test data:

10.00 9.99 ten

State why each value was chosen as test data.

10.00 ................................................................................................................................................

..........................................................................................................................................................

9.99 ..................................................................................................................................................

..........................................................................................................................................................

ten ....................................................................................................................................................

..........................................................................................................................................................

6 A function is declared using pseudocode.

FUNCTION ConvertToCm(Inches: REAL) RETURNS REAL


RETURN Inches * 2.4
ENDFUNCTION

Tick (✓) one box which accurately describes the use of the variable Inches

A answer

B call

C parameter

D response

Common questions

Powered by AI

To output 50 names using a single loop in pseudocode, you would define an array Name[1:50] and then use a FOR loop. The pseudocode would be: FOR i = 1 TO 50 DO OUTPUT Name[i] ENDFOR. The constraints include ensuring that the array is properly indexed from 1 to 50, and each index in the array is assumed to have a valid name stored in it.

The flowchart algorithm is designed to iterate over an array of names, recording and possibly processing each name in a specific manner. It uses a flag and counter to manage iterations and possibly perform operations like sorting or selection on the names (e.g., marking or rearranging them), ensuring each name is processed. The goal is achieved by systematically iterating over all elements using loop controls, conditional checks, and potentially a temporary storage variable ("Temp") to manage intermediate operations.

The function ConvertToCm in pseudocode takes a real number labeled as "Inches" and returns its conversion to centimeters by multiplying it by 2.4. In this context, "Inches" serves as a parameter—a variable passed into the function to hold the input value for the calculations. Parameters allow functions to be reused with different inputs.

To modify the algorithm for outputting numbers strictly between 100 and 200 inclusive, the condition in the pseudocode within the WHILE loop should be changed from 'IF Number > 100 THEN OUTPUT Number' to 'IF Number >= 100 AND Number <= 200 THEN OUTPUT Number'. This ensures that only numbers within this range are considered and output.

Pseudocode provides clarity and helps structure thoughts, making it easier to translate concepts into actual code by focusing on logic without language syntax. It allows early detection of logical errors and facilitates better communication among team members. However, it can become a drawback if too detailed, similar to coding, reducing its abstraction benefit, and if not standardized, it may lead to misinterpretation by team members.

A structure diagram in a satellite navigation system divides the system into subsystems or modules, showing their interactions. For instance, it might show a module for inputting destination details and another for displaying directions either as a map or list. Benefits of using structure diagrams include visual clarity on the system’s architecture, easy modification and maintenance, and improved communication among designers by providing a clear hierarchy of system components.

The given pseudocode has several conceptual errors: Error 1: The initial pseudocode doesn't differentiate between the condition for input ("Numbers") and output ("Number"). Correction: Ensure consistency in variable naming to use either 'Number' or 'Numbers'. Error 2: The condition 'Numbers <> 999' is incorrect due to the incorrect use of the variable name. Correction: It should be 'Number <> 999'. Error 3: The pseudocode outputs 'Number' after the loop, which should not happen. Correction: Remove the 'OUTPUT Number' from outside the WHILE loop. Error 4: '999' is not an appropriate control for number termination if input is the concern. Correction: Ensure the input loop checks for new inputs.

10.00 is chosen as boundary test data to check whether the upper limit condition '< $10.00' is correctly implemented, expecting a failure as it equals the threshold. 9.99 is near-boundary valid test data ensuring typical behavior just below the limit should pass. "ten" tests robustness against invalid input types (text instead of numeric), expecting the algorithm to reject this entry or handle it gracefully.

Using the integer '11' tests how the algorithm handles values beyond the upper boundary limit. This confirms whether values greater than the limit are correctly rejected, ensuring the strictness of the price restriction. It is important to verify that the algorithm does not inadvertently accept values that could compromise the system’s price constraint integrity.

Identifying errors in pseudocode is crucial because it serves as a preliminary step to clarify logic and reduce syntax-based issues encountered during programming. By detecting logical flaws early, developers can prevent potential time-consuming debugging post-coding and ensure accurate translation of intended logic into syntax-specific code, ultimately saving resources and enhancing code quality.

You might also like