9.2 AlgDesign.algorithms incompl Flashcards

1
Q

What is Structured English in algorithm design?

A

A method of showing logical steps in an algorithm using straightforward English words for commands and mathematical operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a flowchart?

A

A diagrammatic representation of an algorithm, using symbols and flow lines to show the steps and their order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an algorithm?

A

An ordered set of steps to be followed for completing a task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is pseudocode?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is stepwise refinement?

A

The practice of breaking down a larger problem into smaller, manageable parts, and further subdividing them as needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are three common methods for writing algorithms?

A
  1. Structured English: Logical steps in plain English.
  2. Flowchart: Diagrammatic representation of the steps.
  3. Pseudocode: Detailed logical steps with keywords and operators.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the key features of Structured English?

A
  • Uses simple English commands.
  • Represents logical steps clearly.
  • Often includes numbering for each step.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why are flowcharts effective in algorithm design?

A

They visually represent the structure of an algorithm, showing the sequence and relationships between steps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does pseudocode help in programming?

A
  • Provides sufficient detail for writing programs in high-level languages.
  • Uses meaningful names and logical steps.
  • Avoids the syntax constraints of specific programming languages.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example of Structured English Algorithm:

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is each step in a pseudocode algorithm typically written?

A

Each line of pseudocode represents a single step in the algorithm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How should identifiers in pseudocode be written?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What can be used to track identifiers used in pseudocode?

A

A pseudocode table, with the Identifier name and Description for each identifier. This helps maintain clarity and organisation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you input a value in pseudocode?

A

Use the INPUT statement:

Syntax:
INPUT <Identifier>
Example:
INPUT StudentName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you output a message, value, or combination in pseudocode?

A

Use the OUTPUT statement:

Syntax:
OUTPUT <…>
Examples:
OUTPUT "You have made an error”
OUTPUT StudentName
OUTPUT "Student name is ", StudentName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you assign a value to a variable in pseudocode?

A

Use the ‘<Identifier> ← <value>' statement.</value></Identifier>

Examples:

Counter ← 1
Counter ← Counter + 1
MyChar ← “A”
LetterValue ← ASC(MyChar)
StudentMark ← 40
Percentage ← (StudentMark / 80) * 100

17
Q

How can you concatenate strings in pseudocode?

A

Use the & operator for concatenation.
Example:

OldString ← "Your mark is”
NewString ← OldString & " ninety-seven”

18
Q

What are the operators used in assignment statements in pseudocode?

A

+: addition
-: subtraction
*: multiplication
/: division
&: string concatenation
: assignment

19
Q

How do you write an IF statement for a single choice in pseudocode?

A
IF <Condition> 
    THEN
        <Action>
ENDIF

Example:

IF MyValue > YourValue 
    THEN
        OUTPUT "I win"
ENDIF
20
Q

How do you write an IF statement with a single choice and an alternative in pseudocode?

A
IF <Condition> 
    THEN
        <Action>
    ELSE
        <Alternative Action>
ENDIF

Example:

IF MyValue > YourValue 
    THEN
        OUTPUT "I win"
    ELSE
        OUTPUT "You win"
ENDIF
21
Q

How do you write a CASE statement for multiple choices in pseudocode?

A
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
22
Q

How do you write a CASE statement with an alternative in pseudocode?

A
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
23
Q

What are the relational operators in pseudocode?

A

=: Equal to
<>: Not equal to
>: Greater than
>: Less than
>=: Greater than or equal to
<=: Less than or equal to

24
Q

How is a single-choice IF statement constructed in Python?

A

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")
25
Q

Example for a FOR loop with a fixed number of repetitions in pseudocode

A
Total ← 0
FOR Counter ← 1 TO 10
    OUTPUT "Enter a number "
    INPUT Number
    Total ← Total + Number
NEXT Counter
OUTPUT "The total is ", Total
26
Q

What is the purpose of the STEP increment in a FOR loop in pseudocode?

A

It specifies an optional step size for the counter, which must be a whole number.

27
Q

Example of a FOR loop with a STEP increment in pseudocode

A
FOR Counter ← 1 TO 10 STEP 2 
    OUTPUT Counter
NEXT Counter
28
Q

How does a REPEAT–UNTIL loop function in pseudocode?

A

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
29
Q

How does a WHILE loop function in pseudocode?

A

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
30
Q

Python example of a FOR loop with a step increment

A
for Counter in range(1, 10, 2):
    print(Counter)
31
Q

How does a REPEAT–UNTIL loop ensure a statement is executed at least once?

A

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)
32
Q

What logic operators are used in pseudocode?

A

AND: Both conditions must be true.
OR: At least one condition must be true.
NOT: Inverses the truth value of a condition.

33
Q

What function can be used to return the integer value of a variable in pseudocode?

A

INT(x)

34
Q

Write a program in pseudocode for finding the average of a set of integers with input validation

A
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

35
Q

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?

A

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 a WHILE loop to validate the input before proceeding.
  • The range() function is used, and the loop ends before the final value is reached, so range(1, Number + 1) is used to include the last value.