slug

type

status

category

summary

date

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`sum`

of type`INTEGER`

.`DECLARE name : STRING`

: Declares a variable named`name`

of type`STRING`

.`DECLARE flag : BOOLEAN`

: Declares a variable named`flag`

of type`BOOLEAN`

.

**Variable Initialization:**`sum <-- 100`

: Assigns the integer value`100`

to`sum`

.`name <-- "Victor"`

: Assigns the string`"Victor"`

to`name`

.`flag <-- TRUE`

: Assigns the boolean value`TRUE`

to`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`sum`

to`0`

.

**FOR Loop:**`FOR i <-- 1 TO 100`

: Initiates a loop where the variable`i`

starts at`1`

and increments by`1`

each iteration until it reaches`100`

.`sum <-- sum + i`

: In each iteration, adds the current value of`i`

to`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
`1`

to`100`

. After the loop,`sum`

will hold the value`5050`

, 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 :INTEGER`

and`sum <-- 0`

: Initializes`sum`

to`0`

.`FOR i <-- 1 TO 100 STEP 2`

: Starts a loop with`i`

from`1`

to`100`

, incrementing by`2`

each time (i.e.,`1, 3, 5, ..., 99`

).`sum <-- sum + i`

: Adds the current odd number`i`

to`sum`

.`OUTPUT sum`

: Outputs the total sum of odd numbers between`1`

and`100`

. The result is`2500`

.

**Second Loop: Summing Even Numbers**`DECLARE sum2 :INTEGER`

and`sum2 <-- 0`

: Initializes`sum2`

to`0`

.`FOR i <-- 0 TO 100 STEP 2`

: Starts a loop with`i`

from`0`

to`100`

, incrementing by`2`

each time (i.e.,`0, 2, 4, ..., 100`

).`sum2 <-- sum2 + i`

: Adds the current even number`i`

to`sum2`

.`OUTPUT sum2`

: Outputs the total sum of even numbers between`0`

and`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`0`

and`9`

and assigns it to`generated`

.`count <-- 1`

: Initializes`count`

to`1`

.

**WHILE Loop:**`WHILE generated <> target DO`

: Continues looping as long as`generated`

is not equal to`target`

.`OUTPUT generated`

: Outputs the currently generated number.`generated <-- INT(RAND(10))`

: Generates a new random integer between`0`

and`9`

.`count <-- count + 1`

: Increments the`count`

by`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`FOR`

loop with a step value to sum odd and even numbers separately.

**Second Part:**Uses a`WHILE`

loop 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`Greetings`

that takes no parameters.

**Procedure Body:**`FOR count <-- 1 TO 90`

: Initiates a loop that runs`90`

times.`OUTPUT "Hello"`

: In each iteration, outputs the string`"Hello"`

.`NEXT count`

: Marks the end of the loop block.

**Procedure Call:**`Greetings()`

: Calls the`Greetings`

procedure, 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 call`Greetings()`

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`findArea`

that takes one parameter`radius`

of type`REAL`

(floating-point number) and returns a`REAL`

value.

**Function Body:**`CONSTANT pi = 3.1415962653`

: Declares a constant`pi`

with its approximate value.`DECLARE area : REAL`

: Declares a variable`area`

of 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`findArea`

function with`radius`

set to`100`

and outputs the returned area.

**What It Does:**

- The
`findArea`

function calculates the area of a circle given its radius. In this example, it calculates the area for a circle with a radius of`100`

, 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`isPrime`

that takes an integer`n`

and returns a boolean value (`TRUE`

or`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`n`

and assigns it to`max`

. This is used to optimize the prime checking process.`FOR count <-- 2 TO max`

: Starts a loop from`2`

to`max`

.`IF n MOD count = 0 THEN`

: Checks if`n`

is divisible by`count`

without a remainder.`RETURN FALSE`

: If`n`

is divisible by any number between`2`

and`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`isPrime`

function with`n = 18`

and outputs the result.

**What It Does:**

- The
`isPrime`

function determines whether a given integer`n`

is a prime number.

- In the example,
`isPrime(18)`

checks if`18`

is prime. Since`18`

is divisible by`2`

,`3`

,`6`

, and`9`

, the function returns`FALSE`

, indicating that`18`

is not a prime number.

Step 7: Loops & Functions

Code:

Explanation:

**FOR Loop:**`FOR n <-- 2 TO 1000`

: Initiates a loop where`n`

ranges from 2 to`1000`

.`IF isPrime(n) THEN`

: Calls the previously defined`isPrime`

function to check if`n`

is prime.`OUTPUT "🟢 ", n, " is prime!"`

: If`n`

is prime, outputs a green circle emoji followed by the message that`n`

is prime.`ELSE`

: If`n`

is not prime,`OUTPUT "🔴 ", n, " is not prime..."`

: Outputs a red circle emoji followed by the message that`n`

is not prime.`ENDIF`

: Ends the`IF`

statement.`NEXT n`

: Moves to the next value of`n`

.

**Function Definition:**- The
`isPrime`

function is the same as defined in**Step 6**. It checks whether a given number`n`

is prime.

**What It Does:**

- This code iterates through all numbers from 2 to
`1000`

, checks each number to see if it's prime using the`isPrime`

function, 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`n`

ranging from`3`

to`1000`

.

**Nested IF Statements:**`IF isPrime(n) THEN`

: Checks if`n`

is a prime number.`IF isPrime(n+2) THEN`

: If`n`

is prime, checks if`n + 2`

is also prime.`OUTPUT "🟢🟢 ", n," and ",n+2, " are twin primes"`

: If both`n`

and`n + 2`

are 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
`isPrime`

function is the same as previously defined in**Step 6**and**Step 7**.

**What It Does:**

- This code identifies and outputs all
**twin primes**between`3`

and`1000`

.

**Twin primes**are pairs of prime numbers that have a difference of`2`

(e.g.,`3`

and`5`

,`5`

and`7`

,`11`

and`13`

).

**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`scores`

with indices from`1`

to`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
`scores`

array.

**FOR Loop to Output Array Elements:**`FOR n <-- 1 TO LENGTH(scores)`

: Loops from`1`

to 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`scores`

with indices from`1`

to`18`

, each element being an integer.

**FOR Loop to Assign Random Values:**`FOR i <-- 1 TO LENGTH(scores)`

: Loops from`1`

to`18`

.`scores[i] <-- INT(RAND(100))`

: Assigns a random integer between`0`

and`99`

to 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`1`

to`18`

.`OUTPUT scores[n]`

: Outputs each randomly assigned score.`NEXT n`

: Moves to the next index.

**What It Does:**

- Creates an array of
`18`

integers, assigns each element a random number between`0`

and`99`

, 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`numbers`

with rows and columns both ranging from`1`

to`3`

. Each element is a`STRING`

.

**Array Initialization:**`numbers <-- [ ["8", "1", "6"], ["3", "5", "7"], ["4", "9", "2"] ]`

: Initializes the`numbers`

array 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`numbers`

array. 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`MyString`

as an empty string.

**Nested FOR Loops:**`FOR a <-- 1 TO 23`

: Outer loop iterates`a`

from`1`

to`23`

.`FOR b <-- 1 TO 23`

: Inner loop iterates`b`

from`1`

to`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`numbers`

with rows and columns from`1`

to`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`n`

from`1`

to`6`

.

**File Operations Inside the Loop:**`file <-- n & "a" & ".txt"`

:- Concatenates the current value of
`n`

with`"a"`

and`".txt"`

to form a filename. For example, when`n = 1`

,`file`

becomes`"1a.txt"`

. `OPENFILE file FOR WRITE`

: Opens the file named`file`

in write mode. If the file doesn't exist, it is created.`WRITEFILE file, RANDOM() * 1000`

:- Generates a random floating-point number between
`0`

and`1`

with`RAND()`

. - Multiplies it by
`1,000`

to 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`n`

by`1`

.

**What It Does:**

- Creates six text files named
`1a.txt`

,`2a.txt`

,`3a.txt`

,`4a.txt`

,`5a.txt`

, and`6a.txt`

.

- Each file is written with a random number between
`0`

and`1,000`

.

- Ensures that each file is properly closed after writing to prevent data corruption.

**Example Outcome:**

**File****1a.txt****:**Contains a random number like`345.67`

.

**File****2a.txt****:**Contains a random number like`123.45`

.

- ...

**File****6a.txt****:**Contains a random number like`987.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:**`FOR`

loops with and without step values, and`WHILE`

loops.

**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 许可协议，转载请注明出处。

相关文章