slug
type
status
category
date
summary
tags
password
icon
 Let's walk through each of the following code  step by step, explaining what each part does.
Step 1: Variables
Code:
Explanation:
- Variable Declaration:
- DECLARE sum :INTEGER: Declares a variable named- sumof type- INTEGER.
- DECLARE name : STRING: Declares a variable named- nameof type- STRING.
- DECLARE flag : BOOLEAN: Declares a variable named- flagof type- BOOLEAN.
- Variable Initialization:
- sum <-- 100: Assigns the integer value- 100to- sum.
- name <-- "Victor": Assigns the string- "Victor"to- name.
- flag <-- TRUE: Assigns the boolean value- TRUEto- flag.
- Output Statements:
- OUTPUT sum: Displays the value of- sum(which is- 100).
- OUTPUT name: Displays the value of- name(which is- "Victor").
- OUTPUT flag: Displays the value of- flag(which is- TRUE).
Summary: This step demonstrates how to declare variables of different types, assign values to them, and output their values.
Step 2: Simple Loop
Code:
Explanation:
- Variable Declaration and Initialization:
- DECLARE sum :INTEGER: Declares an integer variable- sum.
- sum <-- 0: Initializes- sumto- 0.
- FOR Loop:
- FOR i <-- 1 TO 100: Initiates a loop where the variable- istarts at- 1and increments by- 1each iteration until it reaches- 100.
- sum <-- sum + i: In each iteration, adds the current value of- ito- sum.
- NEXT i: Marks the end of the loop block for the variable- i.
- Output Statement:
- OUTPUT sum: After the loop completes, outputs the final value of- sum.
What It Does:
- This loop calculates the sum of all integers from 1to100. After the loop,sumwill hold the value5050, which is the result of 100*101/2.
Step 3: Loop with Step and While Loop
Code:
Explanation:
Part 1: FOR Loop with STEP
- First Loop: Summing Odd Numbers
- DECLARE sum :INTEGERand- sum <-- 0: Initializes- sumto- 0.
- FOR i <-- 1 TO 100 STEP 2: Starts a loop with- ifrom- 1to- 100, incrementing by- 2each time (i.e.,- 1, 3, 5, ..., 99).
- sum <-- sum + i: Adds the current odd number- ito- sum.
- OUTPUT sum: Outputs the total sum of odd numbers between- 1and- 100. The result is- 2500.
- Second Loop: Summing Even Numbers
- DECLARE sum2 :INTEGERand- sum2 <-- 0: Initializes- sum2to- 0.
- FOR i <-- 0 TO 100 STEP 2: Starts a loop with- ifrom- 0to- 100, incrementing by- 2each time (i.e.,- 0, 2, 4, ..., 100).
- sum2 <-- sum2 + i: Adds the current even number- ito- sum2.
- OUTPUT sum2: Outputs the total sum of even numbers between- 0and- 100. The result is- 2550.
Part 2: WHILE Loop
- Variable Declaration and Initialization:
- DECLARE target, generated, count : INTEGER: Declares three integer variables.
- target <-- 8: Sets the target number to- 8.
- generated <-- INT(RAND(10)): Generates a random integer between- 0and- 9and assigns it to- generated.
- count <-- 1: Initializes- countto- 1.
- WHILE Loop:
- WHILE generated <> target DO: Continues looping as long as- generatedis not equal to- target.
- OUTPUT generated: Outputs the currently generated number.
- generated <-- INT(RAND(10)): Generates a new random integer between- 0and- 9.
- count <-- count + 1: Increments the- countby- 1.
- ENDWHILE: Marks the end of the loop.
- Final Output:
- OUTPUT "Found target number ", target, " in ", count, " random guesses": After finding the target number, outputs a message indicating how many guesses it took to find the target.
What It Does:
- First Part: Demonstrates how to use a FORloop with a step value to sum odd and even numbers separately.
- Second Part: Uses a WHILEloop to repeatedly generate random numbers until the target number (8) is found, keeping track of the number of attempts.
Step 4: PROCEDURE
Code:
Explanation:
- Procedure Declaration:
- PROCEDURE Greetings(): Defines a procedure named- Greetingsthat takes no parameters.
- Procedure Body:
- FOR count <-- 1 TO 90: Initiates a loop that runs- 90times.
- OUTPUT "Hello": In each iteration, outputs the string- "Hello".
- NEXT count: Marks the end of the loop block.
- Procedure Call:
- Greetings(): Calls the- Greetingsprocedure, executing its body.
What It Does:
- This procedure, when called, prints the word "Hello"ninety times. It's a way to encapsulate reusable code; you can callGreetings()wherever you need to perform this action without rewriting the loop each time.
Step 5: FUNCTION 1
Code:
Explanation:
- Function Declaration:
- FUNCTION findArea(radius : REAL) RETURNS REAL: Defines a function named- findAreathat takes one parameter- radiusof type- REAL(floating-point number) and returns a- REALvalue.
- Function Body:
- CONSTANT pi = 3.1415962653: Declares a constant- piwith its approximate value.
- DECLARE area : REAL: Declares a variable- areaof type- REAL.
- area <-- pi * radius ** 2: Calculates the area of a circle using the formula \( \pi r^2 \) and assigns it to- area. Here,- *denotes exponentiation.
- RETURN area: Returns the calculated area.
- Function Call and Output:
- OUTPUT findArea(100): Calls the- findAreafunction with- radiusset to- 100and outputs the returned area.
What It Does:
- The findAreafunction calculates the area of a circle given its radius. In this example, it calculates the area for a circle with a radius of100, resulting in \( \pi \times 100^2 = 31,415.92653 \).
Step 6: FUNCTION 2
Code:
Explanation:
- Function Declaration:
- FUNCTION isPrime(n : INTEGER) RETURNS BOOLEAN: Defines a function named- isPrimethat takes an integer- nand returns a boolean value (- TRUEor- FALSE).
- Function Body:
- DECLARE max : INTEGER: Declares an integer variable- max.
- max <-- INT(n ** 0.5): Calculates the integer part of the square root of- nand assigns it to- max. This is used to optimize the prime checking process.
- FOR count <-- 2 TO max: Starts a loop from- 2to- max.
- IF n MOD count = 0 THEN: Checks if- nis divisible by- countwithout a remainder.
- RETURN FALSE: If- nis divisible by any number between- 2and- max, it is not prime; thus, returns- FALSE.
- NEXT count: Ends the loop.
- RETURN TRUE: If no divisors are found, the number is prime; returns- TRUE.
- Function Call and Output:
- OUTPUT isPrime(18): Calls the- isPrimefunction with- n = 18and outputs the result.
What It Does:
- The isPrimefunction determines whether a given integernis a prime number.
- In the example, isPrime(18)checks if18is prime. Since18is divisible by2,3,6, and9, the function returnsFALSE, indicating that18is not a prime number.
Step 7: Loops & Functions
Code:
Explanation:
- FOR Loop:
- FOR n <-- 2 TO 1000: Initiates a loop where- nranges from 2 to- 1000.
- IF isPrime(n) THEN: Calls the previously defined- isPrimefunction to check if- nis prime.
- OUTPUT "🟢 ", n, " is prime!": If- nis prime, outputs a green circle emoji followed by the message that- nis prime.
- ELSE: If- nis not prime,
- OUTPUT "🔴 ", n, " is not prime...": Outputs a red circle emoji followed by the message that- nis not prime.
- ENDIF: Ends the- IFstatement.
- NEXT n: Moves to the next value of- n.
- Function Definition:
- The isPrimefunction is the same as defined in Step 6. It checks whether a given numbernis prime.
What It Does:
- This code iterates through all numbers from 2 to 1000, checks each number to see if it's prime using theisPrimefunction, and outputs a message indicating whether each umber is prime or not with corresponding emojis.
Example Output:
Step 8: Nested Loops
Code:
Explanation:
- FOR Loop:
- FOR n <-- 3 TO 1000: Starts a loop with- nranging from- 3to- 1000.
- Nested IF Statements:
- IF isPrime(n) THEN: Checks if- nis a prime number.
- IF isPrime(n+2) THEN: If- nis prime, checks if- n + 2is also prime.
- OUTPUT "🟢🟢 ", n," and ",n+2, " are twin primes": If both- nand- n + 2are prime, outputs a message indicating they are twin primes with two green circle emojis.
- ENDIF: Ends the inner- IF.
- ENDIF: Ends the outer- IF.
- NEXT n: Proceeds to the next value of- n.
- Function Definition:
- The isPrimefunction is the same as previously defined in Step 6 and Step 7.
What It Does:
- This code identifies and outputs all twin primes between 3and1000.
- Twin primes are pairs of prime numbers that have a difference of 2(e.g.,3and5,5and7,11and13).
Example Output:
(Note: 
999 is not a prime number, so the last line would not actually appear.)Step 9: Array
Code:
Explanation:
Part 1: Static Array Initialization
- Array Declaration:
- DECLARE scores : ARRAY [1:5] OF INTEGER: Declares an array named- scoreswith indices from- 1to- 5, each element being an integer.
- Assigning Values:
- scores[1] <-- 78
- scores[2] <-- 91
- scores[3] <-- 27
- scores[4] <-- 63
- scores[5] <-- 42
- Assigns specific integer values to each element of the scoresarray.
- FOR Loop to Output Array Elements:
- FOR n <-- 1 TO LENGTH(scores): Loops from- 1to the length of- scores(which is- 5).
- OUTPUT scores[n]: Outputs each element in the array.
- NEXT n: Moves to the next index.
What It Does:
- Initializes an array with predefined scores and outputs each score:
Part 2: Dynamic Array with Random Values
- Array Declaration:
- DECLARE scores : ARRAY [1:18] OF INTEGER: Declares an array named- scoreswith indices from- 1to- 18, each element being an integer.
- FOR Loop to Assign Random Values:
- FOR i <-- 1 TO LENGTH(scores): Loops from- 1to- 18.
- scores[i] <-- INT(RAND(100)): Assigns a random integer between- 0and- 99to each element in the array.
- NEXT i: Moves to the next index.
- FOR Loop to Output Array Elements:
- FOR n <-- 1 TO LENGTH(scores): Loops from- 1to- 18.
- OUTPUT scores[n]: Outputs each randomly assigned score.
- NEXT n: Moves to the next index.
What It Does:
- Creates an array of 18integers, assigns each element a random number between0and99, and then outputs all the scores.
Example Output:
Step 10: 2D Array
Code:
Explanation:
- Initial Output:
- OUTPUT "三阶幻方": Outputs the string- "三阶幻方", which is Chinese for "3x3 Magic Square".
- 2D Array Declaration:
- DECLARE numbers : ARRAY[1:3, 1:3] OF STRING: Declares a two-dimensional array named- numberswith rows and columns both ranging from- 1to- 3. Each element is a- STRING.
- Array Initialization:
- numbers <-- [ ["8", "1", "6"], ["3", "5", "7"], ["4", "9", "2"] ]: Initializes the- numbersarray with the values of a classic 3x3 magic square. In a magic square, the sums of numbers in each row, column, and both main diagonals are equal (here, each sum is- 15).
- FOR Loop to Output the Magic Square:
- FOR n <-- 1 TO LENGTH(numbers): Loops through each row of the- numbersarray. Assuming- LENGTH(numbers)returns the number of rows (- 3).
- OUTPUT numbers[n, 1] & " " & numbers[n, 2] & " " & numbers[n, 3]: Concatenates and outputs the three elements of each row separated by spaces.
- NEXT n: Moves to the next row.
What It Does:
- Prints out a 3x3 magic square. The output will look like:
Visual Representation:
Each row, column, and diagonal sums to 
15.Step 11: Nested Loops with Conditions
Code:
Explanation:
Part 1: Creating a 23x23 Grid with Specific Conditions
- Variable Declaration and Initialization:
- DECLARE MyString : STRING: Declares a string variable- MyString.
- MyString ← "": Initializes- MyStringas an empty string.
- Nested FOR Loops:
- FOR a <-- 1 TO 23: Outer loop iterates- afrom- 1to- 23.
- FOR b <-- 1 TO 23: Inner loop iterates- bfrom- 1to- 23.
- IF Conditions:
- (a = b AND a<>11 AND a<>10 AND a<>1): Checks if the current position is on the main diagonal (- a = b) but not at positions- 1,- 10, or- 11.
- (a + b = 24 AND a<>11 AND a<>10 AND a<>1): Checks if the current position is on the opposite diagonal (- a + b = 24) but not at positions- 1,- 10, or- 11.
- a = 12 OR b = 12: Checks if the current row or column is- 12.
- THEN MyString ← MyString & "❤️": If any of the above conditions are true, appends a heart emoji to- MyString.
- ELSE MyString ← MyString & "🟢": Otherwise, appends a green circle emoji.
- NEXT b: Ends the inner loop.
- MyString ← MyString & "\\n": Adds a newline character after completing each row.
- NEXT a: Ends the outer loop.
- Output Statement:
- OUTPUT MyString: Displays the constructed string representing the grid.
What It Does:
- Constructs and outputs a 23x23 grid where:
- Cells on the main diagonal and the opposite diagonal (excluding specific positions) are marked with heart emojis.
- The entire 12th row and column are also marked with heart emojis.
- All other cells are marked with green circles.
- The result is a pattern of hearts and green circles forming specific shapes within the grid.
Part 2: Creating a 7x7 Grid with Diagonals
- Initial Output:
- OUTPUT "shapes","\\n","\\n": Outputs the string- "shapes"followed by two newline characters.
- 2D Array Declaration:
- DECLARE numbers : ARRAY[1:7, 1:7] OF STRING: Declares a two-dimensional array- numberswith rows and columns from- 1to- 7.
- Nested FOR Loops to Assign Emojis:
- FOR a <-- 1 TO 7: Outer loop for rows.
- FOR b <-- 1 TO 7: Inner loop for columns.
- IF a = b OR a + b = 8 THEN: Checks if the cell is on the main diagonal (- a = b) or the opposite diagonal (- a + b = 8).
- numbers[a, b] <-- "❤️": Assigns a heart emoji.
- ELSE: Otherwise,
- numbers[a, b] <-- "🟢": Assigns a green circle emoji.
- NEXT b: Ends the inner loop.
- NEXT a: Ends the outer loop.
- FOR Loop to Output the 7x7 Grid:
- FOR n <-- 1 TO LENGTH(numbers): Iterates through each row.
- OUTPUT numbers[n, 1] & " " & numbers[n, 2] & " " & numbers[n, 3] & " " & numbers[n, 4] & " " & numbers[n, 5] & " " & numbers[n, 6] & " " & numbers[n, 7]: Concatenates and outputs all seven elements of the current row separated by spaces.
- NEXT n: Moves to the next row.
What It Does:
- Creates and displays a 7x7 grid where:
- Cells on the main diagonal (a = b) and the opposite diagonal (a + b = 8) are marked with heart emojis.
- All other cells are marked with green circles.
Example Output:
This forms an 'X' shape with hearts on both diagonals.
Step 12: Files
Code:
Explanation:
- Variable Declaration:
- DECLARE file : STRING: Declares a string variable- file.
- FOR Loop:
- FOR n <-- 1 TO 6: Initiates a loop that runs six times with- nfrom- 1to- 6.
- File Operations Inside the Loop:
- file <-- n & "a" & ".txt":
- Concatenates the current value of nwith"a"and".txt"to form a filename. For example, whenn = 1,filebecomes"1a.txt".
- OPENFILE file FOR WRITE: Opens the file named- filein write mode. If the file doesn't exist, it is created.
- WRITEFILE file, RANDOM() * 1000:
- Generates a random floating-point number between 0and1withRAND().
- Multiplies it by 1,000to scale it up.
- Writes this number to the file.
- CLOSEFILE file: Closes the file after writing.
- Loop Progression:
- NEXT n: Moves to the next iteration, incrementing- nby- 1.
What It Does:
- Creates six text files named 1a.txt,2a.txt,3a.txt,4a.txt,5a.txt, and6a.txt.
- Each file is written with a random number between 0and1,000.
- Ensures that each file is properly closed after writing to prevent data corruption.
Example Outcome:
- File 1a.txt: Contains a random number like345.67.
- File 2a.txt: Contains a random number like123.45.
- ...
- File 6a.txt: Contains a random number like987.65.
Note:
- The actual random numbers will vary each time the code is executed.
- This code demonstrates basic file I/O operations: creating, writing to, and closing files within a loop.
STEP 1-12 Summary
You've covered a wide range of programming concepts across these steps, including:
- Variables: Declaration, initialization, and output.
- Loops: FORloops with and without step values, andWHILEloops.
- Procedures and Functions: Creating reusable code blocks that perform specific tasks.
- Conditional Statements: Using IF,ELSE, and logical operators to control program flow.
- Arrays: Handling both one-dimensional and two-dimensional arrays.
- Nested Loops and Conditions: Creating complex patterns and performing advanced computations.
- File Operations: Automating file creation and data writing.
Each step builds upon fundamental programming principles, demonstrating how to structure and manipulate data, control program flow, and interact with external resources like files. Understanding these concepts is crucial for developing more complex and efficient programs.
If you have any specific questions or need further clarification on any of these steps, feel free to ask!
STEP 13 Symbols and Keywords
Symbols and Keywords
STEP 14 Past Paper Questions 001
9618/23/M/J/23 Q4:
A function MakeString () will:
- take two parameters:
- a count as an integer
- a character
- generate a string of length equal to the count, made up of the character
- return the string generated, or return "ERROR" if the count is less than 1.
For example, the function call:
MakeString ( 3, ‘Z’)  will return the string " Z Z Z"
Write pseudocode for function MakeString ( ) .
Solution:
STEP 15 Past Paper Questions 002
Step 16 Constructor
Step 17 Inheritance & Polymorphism
STEP 18 Recursive
STEP 19 More Past paper Questions
Q1:
????
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/Pseudocodesv
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章












