9.2 AlgDesign.algorithms Flashcards
What is Structured English in algorithm design?
A method of showing logical steps in an algorithm using straightforward English words for commands and mathematical operations.
What is a flowchart?
A diagrammatic representation of an algorithm, using symbols and flow lines to show the steps and their order.
What is an algorithm?
An ordered set of steps to be followed for completing a task.
What is pseudocode?
A representation of the logical steps in an algorithm using keywords, meaningful identifiers, and mathematical operators without adhering to a specific programming language’s syntax.
What is stepwise refinement?
The practice of breaking down a larger problem into smaller, manageable parts, and further subdividing them as needed. In program development, breaking down each step into smaller processes.
What are three common methods for writing algorithms?
- Structured English: Logical steps in plain English.
- Flowchart: Diagrammatic representation of the steps.
- Pseudocode: Detailed logical steps with keywords and operators.
What are the key features of Structured English?
- Uses simple English commands.
- Represents logical steps clearly.
- Often includes numbering for each step.
Why are flowcharts effective in algorithm design?
They visually represent the structure of an algorithm, showing the sequence and relationships between steps.
How does pseudocode help in programming?
- Provides sufficient detail for writing programs in high-level languages.
- Uses meaningful names and logical steps.
- Avoids the syntax constraints of specific programming languages.
Example of Structured English Algorithm:
1 Ask for the number of values
2 Loop that number of times
3 Enter a value in the loop
4 Add the value to the total in the loop
5 Calculate and output the average
How is each step in a pseudocode algorithm typically written?
Each line of pseudocode represents a single step in the algorithm.
How should identifiers in pseudocode be written?
- Use meaningful names, such as
Name
for a person’s name. - Identifiers should only contain characters A–Z, a–z, and 0–9.
- Start identifiers with a letter.
- They are case-insensitive.
What can be used to track identifiers used in pseudocode?
A pseudocode table, with the Identifier name and Description for each identifier. This helps maintain clarity and organisation.
How do you input a value in pseudocode?
Use the INPUT statement:
Syntax:INPUT <Identifier>
Example: INPUT StudentName
How do you output a message, value, or combination in pseudocode?
Use the OUTPUT statement:
Syntax: OUTPUT <…>
Examples:OUTPUT "You have made an error”
OUTPUT StudentName
OUTPUT "Student name is ", StudentName
How do you assign a value to a variable in pseudocode?
Use the ‘<Identifier> ← <value>' statement.</value></Identifier>
Examples:
Counter ← 1
Counter ← Counter + 1
MyChar ← “A”
LetterValue ← ASC(MyChar)
StudentMark ← 40
Percentage ← (StudentMark / 80) * 100
How can you concatenate strings in pseudocode?
Use the &
operator for concatenation.
Example:
OldString ← "Your mark is”
NewString ← OldString & " ninety-seven”
What are the operators used in assignment statements in pseudocode?
+
: addition-
: subtraction*
: multiplication/
: division&
: string concatenation←
: assignment
How do you write an IF statement for a single choice in pseudocode?
IF <Condition> THEN <Action> ENDIF
Example:
IF MyValue > YourValue THEN OUTPUT "I win" ENDIF
How do you write an IF statement with a single choice and an alternative in pseudocode?
IF <Condition> THEN <Action> ELSE <Alternative Action> ENDIF
Example:
IF MyValue > YourValue THEN OUTPUT "I win" ELSE OUTPUT "You win" ENDIF
How do you write a CASE statement for multiple choices in pseudocode?
CASE OF <Variable> "Choice1": <Action1> "Choice2": <Action2> ... ENDCASE
Example:
CASE OF Direction "N": Y ← Y + 1 "S": Y ← Y – 1 "E": X ← X + 1 "W": X ← X – 1 ENDCASE
How do you write a CASE statement with an alternative in pseudocode?
CASE OF <Variable> "Choice1": <Action1> "Choice2": <Action2> ... OTHERWISE: <Alternative Action> ENDCASE
Example:
CASE OF Direction "N": Y ← Y + 1 "S": Y ← Y – 1 "E": X ← X + 1 "W": X ← X – 1 OTHERWISE: OUTPUT "Error" ENDCASE
What are the relational operators in pseudocode?
=
: Equal to<>
: Not equal to>
: Greater than>
: Less than>=
: Greater than or equal to<=
: Less than or equal to
How is a single-choice IF statement constructed in Python?
A colon :
starts the THEN
clause, and all statements in the THEN
clause are indented.
Example:
myValue = int(input("Please enter my value ")) yourValue = int(input("Please enter your value ")) if myValue > yourValue: print("I win")
Example for a FOR loop with a fixed number of repetitions in pseudocode
Total ← 0 FOR Counter ← 1 TO 10 OUTPUT "Enter a number " INPUT Number Total ← Total + Number NEXT Counter OUTPUT "The total is ", Total
What is the purpose of the STEP increment in a FOR loop in pseudocode?
It specifies an optional step size for the counter, which must be a whole number.
Example of a FOR loop with a STEP increment in pseudocode
FOR Counter ← 1 TO 10 STEP 2 OUTPUT Counter NEXT Counter
How does a REPEAT–UNTIL loop function in pseudocode?
A REPEAT loop always executes its statements at least once and repeats until a specified condition is true.
Example:
REPEAT OUTPUT "Please enter a positive number " INPUT Number UNTIL Number > 0
How does a WHILE loop function in pseudocode?
A WHILE loop checks its condition before execution and may not execute at all if the condition is false initially.
Example:
Number ← 0 WHILE Number >= 0 DO OUTPUT "Please enter a negative number " INPUT Number ENDWHILE
Python example of a FOR loop with a step increment
for Counter in range(1, 10, 2): print(Counter)
How does a REPEAT–UNTIL loop ensure a statement is executed at least once?
The condition is evaluated after the execution of the loop’s body.
Example:
REPEAT OUTPUT "Please enter a positive number less than fifty" INPUT Number UNTIL (Number > 0) AND (Number < 50)
What logic operators are used in pseudocode?
AND
: Both conditions must be true.OR
: At least one condition must be true.NOT
: Inverses the truth value of a condition.
What function can be used to return the integer value of a variable in pseudocode?
INT(x)
Write a program in pseudocode for finding the average of a set of integers with input validation
Total ← 0 REPEAT PRINT "Enter the number of values to average" INPUT Number UNTIL (Number > 0) AND (Number = INT(Number)) FOR Counter ← 1 TO Number REPEAT PRINT "Enter an integer value " INPUT Value UNTIL Value = INT(Value) Total ← Total + Value NEXT Counter Average ← Total / Number PRINT "The average of ", Number, " values is ", Average
identifier table for the algorithm:
Total: Running total of integer values entered
Number: Number of integer values to enter
Value: Integer value input
Average: Average of all the integer values entered
Write a program in Python for finding the average of a set of integers with input validation. How is it different from the one in pseudocode?
Find the average of a number of integers input Python
Total = 0 Number = int(input("Enter the number of values to average ")) while Number <= 0: Number = int(input("Enter the number of values to average ")) # An extra input is needed, as a WHILE loop must be used for Counter in range(1, Number + 1): Value = int(input("Enter an integer value ")) Total += Value Average = Total / Number print("The average of", Number, "values is", Average)
- Python lacks a
REPEAT–UNTIL
construct, requiring aWHILE
loop to validate the input before proceeding. - The
range()
function is used, and the loop ends before the final value is reached, sorange(1, Number + 1)
is used to include the last value.
What are the flowchart symbols and their purposes?
Parallelogram: INPUT or OUTPUT operations.
Rhombus: decision-making constructs like IF, CASE, or parts of FOR, REPEAT, and WHILE loops.
Rectangle: assignment, calculations, or predefined processes (e.g., INT).
Obround: (start and end) terminators.
Returning flow line: iterative constructs like FOR, REPEAT, and WHILE loops.